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
91b61040
Commit
91b61040
authored
6 years ago
by
Brutzman, Don
Browse files
Options
Downloads
Patches
Plain Diff
improved comments, also report to client what connection number they were
parent
2250b05d
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
projects/TcpExample2/src/tcpexample2/TcpExample2.java
+10
-11
10 additions, 11 deletions
projects/TcpExample2/src/tcpexample2/TcpExample2.java
with
10 additions
and
11 deletions
projects/TcpExample2/src/tcpexample2/TcpExample2.java
+
10
−
11
View file @
91b61040
package
tcpexample2
;
package
tcpexample2
;
import
java.io.*
;
import
java.io.*
;
...
@@ -16,7 +15,7 @@ import java.net.*;
...
@@ -16,7 +15,7 @@ import java.net.*;
* If you're sophisticated you can contact the instructor's computer
* If you're sophisticated you can contact the instructor's computer
* while running this program.
* while running this program.
*
*
* telnet
<
ipOfServersLaptop
>
2317
*
<code>
telnet ipOfServersLaptop 2317
</code>
*
*
* And have him display the socket pairs he got.
* And have him display the socket pairs he got.
* @author mcgredo
* @author mcgredo
...
@@ -34,7 +33,8 @@ public class TcpExample2
...
@@ -34,7 +33,8 @@ public class TcpExample2
int
connectionCount
=
0
;
// state
int
connectionCount
=
0
;
// state
ServerSocket
serverSocket
=
new
ServerSocket
(
2317
);
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.
// Loop, infinitely, waiting for client connections.
// Loop, infinitely, waiting for client connections.
// Stop the program somewhere else.
// Stop the program somewhere else.
...
@@ -49,27 +49,28 @@ public class TcpExample2
...
@@ -49,27 +49,28 @@ public class TcpExample2
ps
.
println
(
"This client response was written by server TcpExample2"
);
// to remote client
ps
.
println
(
"This client response was written by server TcpExample2"
);
// to remote client
System
.
out
.
println
(
"This server response was written by server TcpExample2"
);
// to server console
System
.
out
.
println
(
"This server response was written by server TcpExample2"
);
// to server console
ps
.
println
(
"You were connection #"
+
connectionCount
+
", by my count"
);
// Print some information locally about the Socket
// Print some information locally about the Socket
// connection. This includes the port and IP numbers
// connection. This includes the port and IP numbers
// on both sides (the socket pair.)
// on both sides (the socket pair.)
InetAddress
localAddress
=
clientConnection
.
getLocalAddress
();
InetAddress
localAddress
=
clientConnection
.
getLocalAddress
();
InetAddress
remoteAddress
=
clientConnection
.
getInetAddress
();
InetAddress
remoteAddress
=
clientConnection
.
getInetAddress
();
int
localPort
=
clientConnection
.
getLocalPort
();
int
localPort
=
clientConnection
.
getLocalPort
();
int
remotePort
=
clientConnection
.
getPort
();
int
remotePort
=
clientConnection
.
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 ))
// 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 ))
// Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 ))
//
//
// Why is the first IP/port the same, while the second set has
// Why is the first IP/port the same, while the second set has
// different ports?
// different ports?
System
.
out
.
println
(
"Socket pair: (( "
+
localAddress
.
toString
()
+
", "
+
localPort
+
" ), ( "
+
System
.
out
.
println
(
"Socket pair: (( "
+
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
...
@@ -85,7 +86,5 @@ public class TcpExample2
...
@@ -85,7 +86,5 @@ public class TcpExample2
{
{
System
.
out
.
println
(
"problem with networking: "
+
e
);
System
.
out
.
println
(
"problem with networking: "
+
e
);
}
}
}
}
}
}
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