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
b4841804
Commit
b4841804
authored
9 months ago
by
owner
Browse files
Options
Downloads
Patches
Plain Diff
Homework 1
parent
c7e47740
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
assignments/src/MV3500Cohort2024JulySeptember/homework1/Lennon/LennonNetworkHW1.java
+112
-0
112 additions, 0 deletions
...t2024JulySeptember/homework1/Lennon/LennonNetworkHW1.java
with
112 additions
and
0 deletions
assignments/src/MV3500Cohort2024JulySeptember/homework1/Lennon/LennonNetworkHW1.java
0 → 100644
+
112
−
0
View file @
b4841804
package
MV3500Cohort2024JulySeptember.homework1.Lennon
;
import
java.io.*
;
import
java.net.*
;
/**
* <p>
* The simplest possible TCP network program, opening a socket and waiting for a reply.
* It listens for any socket connection response, either from telnet (telnet localhost 2317)
* or another program that you write (which we will do later).
* Once a socket connection is established, TcpExample1Telnet simply
* writes a string in response to that connection and finishes, exiting.
*
* <p>
* As an alternative to running the Windows (or other operating system) console,
* you can instead run the NetBeans terminal window. If you are on Windows,
* NetBeans is looking for Cygwin installation (for Unix-like compatibility)
* with details at <a href="https://savage.nps.edu/Savage/developers.html#Cygwin" target="blank">Savage Developers Guide: Cygwin</a>.
* Modifying this program is the basis for Assignment 1.
* </p>
*
* <p>
* Testing the running server program from telnet looks like this:
* </p>
* <pre>
* it154916:projects mcgredo$ <b>telnet localhost 2317</b>
* Trying ::1...
* Connected to localhost.
* Escape character is '^]'.
* This was written by the server
* Connection closed by foreign host.
* </pre>
* <p>
* Notice that "This was written by the server" matches
* what is written by the code below, over the output stream.
* </p>
*
* <p>
* After this first connection the program below drops to
* the bottom of the method, and does not repeat itself.
* In other words, the program exits.
* </p>
*
* @see <a href="../../../src/TcpExamples/TcpExample1TerminalLog.txt" target="_blank">TcpExample1TerminalLog.txt</a>
* @see <a href="../../../src/TcpExamples/TcpExample1NetBeansConsoleTelnet.png" target="_blank">TcpExample1NetBeansConsoleTelnet.png</a>
* @see <a href="../../../src/TcpExamples/TcpExample1NetBeansConsoleTelnet.pdf" target="_blank">TcpExample1NetBeansConsoleTelnet.pdf</a>
* @see <a href="../../../src/TcpExamples/TcpExample1ScreenshotNetcat.png" target="_blank">TcpExample1ScreenshotNetcat.png</a>
* @see <a href="../../../src/TcpExamples/TcpExample1ScreenshotTelnet.png" target="_blank">TcpExample1ScreenshotTelnet.png</a>
* @see <a href="https://savage.nps.edu/Savage/developers.html#Cygwin" target="blank">Savage Developers Guide: Cygwin</a>
* @see <a href="https://savage.nps.edu/Savage/developers.html#telnet" target="blank">Savage Developers Guide: telnet</a>
*
* @author mcgredo
* @author brutzman@nps.edu
*/
public
class
LennonNetworkHW1
{
/** Default constructor */
public
LennonNetworkHW1
()
{
// default constructor
}
/**
* Program invocation, execution starts here
* @param args command-line arguments
*/
public
static
void
main
(
String
[]
args
)
{
try
{
System
.
out
.
println
(
LennonNetworkHW1
.
class
.
getName
()
+
" Server has started and is waiting for a connection."
);
System
.
out
.
println
(
" help: https://savage.nps.edu/Savage/developers.html#telnet"
);
System
.
out
.
println
(
" terminal: enter (telnet localhost 2317) or, for macOS (nc localhost 2317)..."
);
// The ServerSocket waits for a connection from a client.
// It returns a Socket object when the connection occurs.
ServerSocket
serverSocket
=
new
ServerSocket
(
2317
);
// Use Java io classes to write text (as opposed to
// unknown bytes of some sort) to the client
// The Socket object represents the connection between
// the server and client, including a full duplex connection
try
(
Socket
clientConnection
=
serverSocket
.
accept
())
// listen, wait here for a client to connect
{
// OK we got something, time to respond!
// Use Java io classes to write text (as opposed to
// unknown bytes of some sort) to the client
OutputStream
os
=
clientConnection
.
getOutputStream
();
PrintStream
ps
=
new
PrintStream
(
os
);
ps
.
println
(
"Welcome to server "
+
LennonNetworkHW1
.
class
.
getName
());
// to remote client
System
.
out
.
println
(
"This server response was written by server "
+
LennonNetworkHW1
.
class
.
getName
());
// to server console
// "flush()" in important in that it forces a write
// across what is in fact a slow connection
ps
.
flush
();
}
System
.
out
.
println
(
LennonNetworkHW1
.
class
.
getName
()
+
" completed successfully."
);
}
catch
(
IOException
ioe
)
{
System
.
err
.
println
(
"Exception with "
+
LennonNetworkHW1
.
class
.
getName
()
+
" networking:"
);
// describe what is happening
System
.
err
.
println
(
ioe
);
// Provide more helpful information to user if exception occurs due to running twice at one time
// brute force exception checking, can be brittle if exception message changes
// if (e.getMessage().equals("Address already in use: NET_Bind"))
if
(
ioe
instanceof
java
.
net
.
BindException
)
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