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
de7eb223
Commit
de7eb223
authored
5 years ago
by
Terry D. Norbraten
Browse files
Options
Downloads
Patches
Plain Diff
use try w/ resources
parent
48e44e5d
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
+42
-42
42 additions, 42 deletions
examples/src/TcpExamples/TcpExample2ConnectionCounting.java
with
42 additions
and
42 deletions
examples/src/TcpExamples/TcpExample2ConnectionCounting.java
+
42
−
42
View file @
de7eb223
...
...
@@ -33,7 +33,7 @@ public class TcpExample2ConnectionCounting
// ServerSocket waits for a connection from a client.
// Notice that it is outside the loop; ServerSocket needs to be made only once.
int
connectionCount
=
0
;
// state variable
int
connectionCount
=
0
;
// state variable
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.
...
...
@@ -42,53 +42,53 @@ public class TcpExample2ConnectionCounting
// Stop the program somewhere else.
while
(
true
)
{
Socket
clientConnection
=
serverSocket
.
accept
();
// blocks! then proceeds once a connection is "accept"ed
connectionCount
++;
// got another one!
OutputStream
os
=
clientConnection
.
getOutputStream
();
PrintStream
ps
=
new
PrintStream
(
os
);
ps
.
println
(
"This client response was written by server TcpExample2ConnectionCounting"
);
// to remote client
System
.
out
.
println
(
"This server response was written by server TcpExample2ConnectionCounting"
);
// to server console
ps
.
println
(
"You were connection #"
+
connectionCount
+
", by my count"
);
// Print some information locally about the Socket connection.
// This includes the port and IP numbers on both sides (the socket pair.)
InetAddress
localAddress
=
clientConnection
.
getLocalAddress
();
InetAddress
remoteAddress
=
clientConnection
.
getInetAddress
();
int
localPort
=
clientConnection
.
getLocalPort
();
int
remotePort
=
clientConnection
.
getPort
();
// remember the prior question, why are 2 ports different?
// 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, 54881 ))
//
// Why is first IP/port the same, while the second set has different ports?
System
.
out
.
println
(
"Socket pair: (( "
+
localAddress
.
toString
()
+
", "
+
localPort
+
" ), ( "
+
remoteAddress
.
toString
()
+
", "
+
remotePort
+
" ))"
);
System
.
out
.
println
(
"got another connection, #"
+
connectionCount
);
// report progress
// Notice the use of flush() and close(). Without
// the close() to Socket object may stay open for
// a while after the client has stopped needing this
// connection. Close() explicitly ends the connection.
ps
.
flush
();
clientConnection
.
close
();
// blocks! then proceeds once a connection is "accept"ed
try
(
Socket
clientConnection
=
serverSocket
.
accept
())
{
connectionCount
++;
// got another one!
OutputStream
os
=
clientConnection
.
getOutputStream
();
PrintStream
ps
=
new
PrintStream
(
os
);
ps
.
println
(
"This client response was written by server TcpExample2ConnectionCounting"
);
// to remote client
System
.
out
.
println
(
"This server response was written by server TcpExample2ConnectionCounting"
);
// to server console
ps
.
println
(
"You were connection #"
+
connectionCount
+
", by my count"
);
// Print some information locally about the Socket connection.
// This includes the port and IP numbers on both sides (the socket pair.)
InetAddress
localAddress
=
clientConnection
.
getLocalAddress
();
InetAddress
remoteAddress
=
clientConnection
.
getInetAddress
();
int
localPort
=
clientConnection
.
getLocalPort
();
int
remotePort
=
clientConnection
.
getPort
();
// remember the prior question, why are 2 ports different?
// 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, 54881 ))
//
// Why is first IP/port the same, while the second set has different ports?
System
.
out
.
println
(
"Socket pair: (( "
+
localAddress
.
toString
()
+
", "
+
localPort
+
" ), ( "
+
remoteAddress
.
toString
()
+
", "
+
remotePort
+
" ))"
);
System
.
out
.
println
(
"got another connection, #"
+
connectionCount
);
// report progress
// Notice the use of flush() and close(). Without
// the close() to Socket object may stay open for
// a while after the client has stopped needing this
// connection. Close() explicitly ends the connection.
ps
.
flush
();
}
// got another one!
}
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
"Problem with TcpExample2ConnectionCounting networking:"
);
// describe what is happening
System
.
out
.
println
(
"Error: "
+
e
);
System
.
err
.
println
(
"Problem with TcpExample2ConnectionCounting networking:"
);
// describe what is happening
System
.
err
.
println
(
"Error: "
+
e
);
// Provide more helpful information to user if exception occurs due to running twice at one time
if
(
e
instanceof
java
.
net
.
BindException
)
System
.
out
.
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!"
);
}
}
}
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