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
e901ae57
Commit
e901ae57
authored
6 years ago
by
Brutzman, Don
Browse files
Options
Downloads
Patches
Plain Diff
reformat, improve exception to silence warning
parent
190cdc73
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/TcpExample3/Server/TcpServer/src/tcpserver/TcpServer.java
+61
-68
61 additions, 68 deletions
...TcpExample3/Server/TcpServer/src/tcpserver/TcpServer.java
with
61 additions
and
68 deletions
projects/TcpExample3/Server/TcpServer/src/tcpserver/TcpServer.java
+
61
−
68
View file @
e901ae57
...
@@ -4,80 +4,73 @@ import java.io.*;
...
@@ -4,80 +4,73 @@ import java.io.*;
import
java.net.*
;
import
java.net.*
;
/**
/**
* Very slightly more complex than example1. A complete copy of
* Very slightly more complex than example1. A complete copy of example 2. The
* example 2. The only thing this does
* only thing this does differently is introduce a loop into the response, so
* differently is introduce a loop into the response, so you don't
* you don't have to restart the program after one response. Also, it prints out
* have to restart the program after one response. Also, it prints
* the socket pair the server sees. Run the program via telnet several times and
* out the socket pair the server sees. Run the program via telnet
* compare the socket pairs.
* several times and compare the socket pairs.
*
*
* telnet localhost 2317
* telnet localhost 2317
*
*
* If you're sophisticated you can contact the instructor's computer
* If you're sophisticated you can contact the instructor's computer
while
*
while
running this program.
* running this program.
*
*
* telnet
<
ipOfServer
s
Laptop
>
2317
* telnet
[
ip
Number
OfServerLaptop
]
2317
*
*
* And have him display the socket pairs he got.
* And have him display the socket pairs he got.
*
* @author mcgredo
* @author mcgredo
*/
*/
public
class
TcpServer
public
class
TcpServer
{
{
public
static
void
main
(
String
[]
args
)
@SuppressWarnings
(
"ConvertToTryWithResources"
)
{
public
static
void
main
(
String
[]
args
)
try
{
{
try
{
// ServerSocket waits for a connection from a client.
// ServerSocket waits for a connection from a client.
// Notice that it is outside the loop; ServerSocket
// Notice that it is outside the loop; ServerSocket
// needs to be made only once.
// needs to be made only once.
ServerSocket
serverSocket
=
new
ServerSocket
(
2317
);
// Loop, infinitely, waiting for client connections.
ServerSocket
serverSocket
=
new
ServerSocket
(
2317
);
// Stop the program somewhere else.
while
(
true
)
{
Socket
clientConnection
=
serverSocket
.
accept
();
// block until connected
OutputStream
os
=
clientConnection
.
getOutputStream
();
PrintStream
ps
=
new
PrintStream
(
os
);
ps
.
println
(
"This was written by the server"
);
// Loop, infinitely, waiting for client connections.
// Stop the program somewhere else.
// Print some information locally about the Socket
while
(
true
)
// connection. This includes the port and IP numbers
{
// on both sides (the socket pair.)
Socket
clientConnection
=
serverSocket
.
accept
();
// block until connected
OutputStream
os
=
clientConnection
.
getOutputStream
();
InetAddress
localAddress
=
clientConnection
.
getLocalAddress
();
PrintStream
ps
=
new
PrintStream
(
os
);
InetAddress
remoteAddress
=
clientConnection
.
getInetAddress
();
int
localPort
=
clientConnection
.
getLocalPort
();
int
remotePort
=
clientConnection
.
getPort
();
// 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, 54881 ))
//
// Why is the first IP/port the same, while the second set has
// different ports?
ps
.
println
(
"This was written by the server"
);
System
.
out
.
println
(
"Socket pair: (( "
+
localAddress
.
toString
()
+
", "
+
localPort
+
" ), ( "
+
// Print some information locally about the Socket
remoteAddress
.
toString
()
+
", "
+
remotePort
+
" ))"
);
// connection. This includes the port and IP numbers
// on both sides (the socket pair.)
// Notice the use of flush() and close(). Without
InetAddress
localAddress
=
clientConnection
.
getLocalAddress
();
// the close() to Socket object may stay open for
InetAddress
remoteAddress
=
clientConnection
.
getInetAddress
();
// a while after the client has stopped needing this
// connection. Close() explicitly ends the connection.
int
localPort
=
clientConnection
.
getLocalPort
();
ps
.
flush
();
int
remotePort
=
clientConnection
.
getPort
();
clientConnection
.
close
();
// like it or not, you're outta here!
}
// 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 ))
catch
(
Exception
e
)
// Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 ))
{
//
System
.
out
.
println
(
"problem with networking"
);
// Why is the first IP/port the same, while the second set has
}
// different ports?
System
.
out
.
println
(
"Socket pair: (( "
+
localAddress
.
toString
()
+
", "
+
localPort
+
" ), ( "
}
+
remoteAddress
.
toString
()
+
", "
+
remotePort
+
" ))"
);
}
// Notice the use of flush() and close(). Without
\ No newline at end of file
// 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
();
// like it or not, you're outta here!
}
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
"problem with networking"
);
}
}
}
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