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
b4f6920d
Commit
b4f6920d
authored
6 years ago
by
Ayres, Kevin (CAPT)
Browse files
Options
Downloads
Patches
Plain Diff
Ayres_Server_HW2
parent
03ac7083
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
deliverables/src/MV3500Cohort2018JulySeptember/homework2/Ayres_Server.java
+91
-0
91 additions, 0 deletions
...MV3500Cohort2018JulySeptember/homework2/Ayres_Server.java
with
91 additions
and
0 deletions
deliverables/src/MV3500Cohort2018JulySeptember/homework2/Ayres_Server.java
0 → 100644
+
91
−
0
View file @
b4f6920d
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package
MV3500Cohort2018JulySeptember.homework2.Ayres
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.io.OutputStream
;
import
java.io.PrintStream
;
import
java.net.InetAddress
;
import
java.net.ServerSocket
;
import
java.net.Socket
;
/**
*
* @author kjayr
*/
public
class
Ayres_Server
{
@SuppressWarnings
(
"ConvertToTryWithResources"
)
public
static
void
main
(
String
[]
args
)
{
try
{
// ServerSocket waits for a connection from a client.
// Notice that it is outside the loop; ServerSocket
// needs to be made only once.
//Good reminder
System
.
out
.
println
(
"TcpServer has started..."
);
// it helps debugging to put this on console first. Keep.
ServerSocket
serverSocket
=
new
ServerSocket
(
2317
);
// Server is up and waiting (i.e. "blocked" or paused)
// Loop, infinitely, waiting for client connections.
// Stop the program somewhere else.
while
(
true
)
{
Socket
clientConnection
=
serverSocket
.
accept
();
// block until connected to a client
// Set up the streams, Java style:
OutputStream
os
=
clientConnection
.
getOutputStream
();
PrintStream
ps
=
new
PrintStream
(
os
);
//Message to client
ps
.
println
(
"**Strong connection to Server** Client operating..."
);
//________________________
//Set up input streams/messages from client
InputStream
is
=
clientConnection
.
getInputStream
();
InputStreamReader
isr
=
new
InputStreamReader
(
is
);
BufferedReader
br
=
new
BufferedReader
(
isr
);
String
serverMessage
=
br
.
readLine
();
System
.
out
.
println
(
"=================================================="
);
System
.
out
.
println
(
"The message the client sent was "
+
serverMessage
);
//________________________
// 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
();
// 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?
System
.
out
.
println
(
"Socket pair: (( "
+
localAddress
.
toString
()
+
", "
+
localPort
+
" ), ( "
+
remoteAddress
.
toString
()
+
", "
+
remotePort
+
" ))"
);
// 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
();
// 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