Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
NetworkedGraphicsMV3500
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Savage
NetworkedGraphicsMV3500
Commits
bfa6e988
Commit
bfa6e988
authored
9 months ago
by
Brutzman, Don
Browse files
Options
Downloads
Patches
Plain Diff
improved comments and variable naming
parent
47484356
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/src/TcpExamples/TcpExample2ConnectionCounting.java
+36
-23
36 additions, 23 deletions
examples/src/TcpExamples/TcpExample2ConnectionCounting.java
with
36 additions
and
23 deletions
examples/src/TcpExamples/TcpExample2ConnectionCounting.java
+
36
−
23
View file @
bfa6e988
...
@@ -63,7 +63,7 @@ public class TcpExample2ConnectionCounting
...
@@ -63,7 +63,7 @@ public class TcpExample2ConnectionCounting
try
try
{
{
// welcome aboard messages
// welcome aboard messages
System
.
out
.
println
(
"
TcpExample2ConnectionCounting has started and is waiting for a connection."
);
System
.
out
.
println
(
TcpExample2ConnectionCounting
.
class
.
getName
()
+
"
has started and is waiting for a connection."
);
System
.
out
.
println
(
" help: https://savage.nps.edu/Savage/developers.html#telnet"
);
System
.
out
.
println
(
" help: https://savage.nps.edu/Savage/developers.html#telnet"
);
System
.
out
.
println
(
" Windows ipconfig (or Mac ifconfig) indicates current IPv4_Address (for example local wireless IP address)"
);
System
.
out
.
println
(
" Windows ipconfig (or Mac ifconfig) indicates current IPv4_Address (for example local wireless IP address)"
);
System
.
out
.
println
(
" Windows enter (telnet localhost 2317) or Mac enter (nc localhost 2317) for loopback operation, or"
);
System
.
out
.
println
(
" Windows enter (telnet localhost 2317) or Mac enter (nc localhost 2317) for loopback operation, or"
);
...
@@ -76,18 +76,22 @@ public class TcpExample2ConnectionCounting
...
@@ -76,18 +76,22 @@ public class TcpExample2ConnectionCounting
int
connectionCount
=
0
;
// state variable
int
connectionCount
=
0
;
// state variable
ServerSocket
serverSocket
=
new
ServerSocket
(
2317
);
// server decides here what port to listen on.
ServerSocket
serverSocket
=
new
ServerSocket
(
2317
);
// server decides here what port to listen on.
// of interest: often client doesn't care what port it uses locally when connecting to that server port.
// of interest: often client doesn't care what port it uses locally when connecting to that server port.
OutputStream
outputStream
;
// can declare these early, but...
PrintStream
printStream
;
// note initial values are null, they still need to be intialized
// Loop, infinitely, waiting for client connections.
// Loop, infinitely, waiting for client connections.
//
Stop
the program somewhere else.
//
If you want to stop or pause
the program
, go
somewhere else.
while
(
true
)
while
(
true
)
{
{
// serverSocket.accept() blocks! then proceeds once a connection is "accept"ed
// note that serverSocket.accept() first blocks! then after waiting, execution proceeds once a connection is "accept"ed
try
(
Socket
clientConnection
=
serverSocket
.
accept
())
{
// the accept() method blocks here until a client connects
// you can check this behavior by using NetBeans Debugger
try
(
Socket
clientConnectionSocket
=
serverSocket
.
accept
())
{
// the accept() method blocks here until a client connects
connectionCount
++;
// got another one! a client has connected
connectionCount
++;
// got another one! a client has connected
O
utputStream
os
=
clientConnection
.
getOutputStream
();
o
utputStream
=
clientConnection
Socket
.
getOutputStream
();
// clientConnectionSocket is new, every time through, so reset the streams
P
rintStream
ps
=
new
PrintStream
(
o
s
);
p
rintStream
=
new
PrintStream
(
o
utputStream
);
// if (connectionCount == 1) // first time through, report connection
// if (connectionCount == 1) // first time through, report connection
// {
// {
...
@@ -104,19 +108,19 @@ public class TcpExample2ConnectionCounting
...
@@ -104,19 +108,19 @@ public class TcpExample2ConnectionCounting
// "(telnet " + localHostAddress + " 2317)..." );
// "(telnet " + localHostAddress + " 2317)..." );
// }
// }
p
s
.
println
(
"This client response was written by server "
+
TcpExample2ConnectionCounting
.
class
.
getName
());
// to remote client
p
rintStream
.
println
(
"This client response was written by server "
+
TcpExample2ConnectionCounting
.
class
.
getName
());
// to remote client
System
.
out
.
println
(
"This server response was written by server "
+
TcpExample2ConnectionCounting
.
class
.
getName
());
// to server console
System
.
out
.
println
(
"This server response was written by server "
+
TcpExample2ConnectionCounting
.
class
.
getName
());
// to server console
p
s
.
println
(
"You were connection #"
+
connectionCount
+
", by my count"
);
p
rintStream
.
println
(
"You were connection #"
+
connectionCount
+
", by my count"
);
// Print some information locally about the Socket connection.
// Print some information locally about the Socket connection.
// This includes the port and IP numbers on both sides (the socket pair.)
// This includes the port and IP numbers on both sides (the socket pair.)
InetAddress
localAddress
=
clientConnection
.
getLocalAddress
();
InetAddress
localAddress
=
clientConnection
Socket
.
getLocalAddress
();
InetAddress
remoteAddress
=
clientConnection
.
getInetAddress
();
InetAddress
remoteAddress
=
clientConnection
Socket
.
getInetAddress
();
int
localPort
=
clientConnection
.
getLocalPort
();
int
localPort
=
clientConnection
Socket
.
getLocalPort
();
int
remotePort
=
clientConnection
.
getPort
();
// remember the prior question, why are 2 ports different?
int
remotePort
=
clientConnection
Socket
.
getPort
();
// remember the prior question, why are 2 ports different?
// My socket pair connection looks like this, to localhost:
// My socket pair connection looks like this, to localhost:
// Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 )) note IPv6
// Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 )) note IPv6
...
@@ -125,25 +129,34 @@ public class TcpExample2ConnectionCounting
...
@@ -125,25 +129,34 @@ public class TcpExample2ConnectionCounting
// Why is first IP/port the same, while the second set has different ports?
// Why is first IP/port the same, while the second set has different ports?
System
.
out
.
println
(
"Socket pair (server, client): (( "
+
localAddress
.
toString
()
+
", "
+
localPort
+
" ), ( "
+
System
.
out
.
println
(
"Socket pair (server, client): (( "
+
localAddress
.
toString
()
+
", "
+
localPort
+
" ), ( "
+
remoteAddress
.
toString
()
+
", "
+
remotePort
+
" ))"
);
remoteAddress
.
toString
()
+
", "
+
remotePort
+
" ))"
);
System
.
out
.
println
(
"got another connection, #"
+
connectionCount
);
// report progress
System
.
out
.
println
(
"got another connection, #"
+
connectionCount
);
// report progress
// Notice the use of flush() and close(). Without
// Notice the use of flush() to ensure that messages are sent immediately,
// the close() to Socket object may stay open for
// rather than possibly getting buffered for the program's self-perceived efficiency.
// a while after the client has stopped needing this
outputStream
.
flush
();
// connection. Close() explicitly ends the connection.
ps
.
flush
();
// Notice the use of optional close() when complete. Without a close() invocation,
// the clientConnectionSocket object may stay open for a while after the client
// has stopped needing this connection. Close() explicitly ends the connection.
// Try hiding this and see if anything different occurs.
clientConnectionSocket
.
close
();
}
}
}
}
}
}
catch
(
IOException
e
)
catch
(
IOException
io
e
)
{
{
System
.
err
.
println
(
"Problem with "
+
TcpExample2ConnectionCounting
.
class
.
getName
()
+
" networking:"
);
// describe what is happening
System
.
err
.
println
(
"Problem with "
+
TcpExample2ConnectionCounting
.
class
.
getName
()
+
" networking:"
);
// describe what is happening
System
.
err
.
println
(
"Error: "
+
e
);
System
.
err
.
println
(
"Error: "
+
io
e
);
// Provide more helpful information to user if exception occurs due to running twice at one time
// Provide more helpful information to user if exception occurs due to running twice at one time
if
(
e
instanceof
java
.
net
.
BindException
)
if
(
io
e
instanceof
java
.
net
.
BindException
)
System
.
err
.
println
(
"*** Be sure to stop any other running instances of programs using this port!"
);
System
.
err
.
println
(
"*** Be sure to stop any other running instances of programs using this port!"
);
}
}
finally
{
// given the infinite-loop logic above, this step is "hard to reach" but a good practice nevertheless
System
.
out
.
println
(
TcpExample2ConnectionCounting
.
class
.
getName
()
+
" has finished, adios!"
);
}
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment