Skip to content
Snippets Groups Projects
Commit 594f76bc authored by brutzman's avatar brutzman
Browse files

update example and terminal log

parent b555debc
No related branches found
No related tags found
No related merge requests found
...@@ -30,19 +30,20 @@ import java.net.*; ...@@ -30,19 +30,20 @@ import java.net.*;
public class TcpExample1Telnet public class TcpExample1Telnet
{ {
@SuppressWarnings("ConvertToTryWithResources")
public static void main(String[] args) public static void main(String[] args)
{ {
try try
{ {
System.out.println("TcpExample1Telnet has started and is waiting for a connection: telnet localhost 2317" ); System.out.println("TcpExample1Telnet has started and is waiting for a connection:");
System.out.println("Enter (telnet localhost 2317) or (nc localhost 2317)..." );
// The ServerSocket waits for a connection from a client. // The ServerSocket waits for a connection from a client.
// It returns a Socket object when the connection occurs. // It returns a Socket object when the connection occurs.
ServerSocket serverSocket = new ServerSocket(2317); ServerSocket serverSocket = new ServerSocket(2317);
// The Socket object represents the connection between // The Socket object represents the connection between
// the server and client, including a full duplex // the server and client, including a full duplex connection
// connection
Socket clientConnection = serverSocket.accept(); Socket clientConnection = serverSocket.accept();
// Use Java io classes to write text (as opposed to // Use Java io classes to write text (as opposed to
...@@ -50,16 +51,17 @@ public class TcpExample1Telnet ...@@ -50,16 +51,17 @@ public class TcpExample1Telnet
OutputStream os = clientConnection.getOutputStream(); OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os); PrintStream ps = new PrintStream(os);
ps.println("This client response was written by server TcpExample1"); // to remote client ps.println("This client response was written by server TcpExample1."); // to remote clientnc
System.out.println("This server response was written by server TcpExample1"); // to server console System.out.println("This server response was written by server TcpExample1."); // to server console
// "flush()" in important in that it forces a write // "flush()" in important in that it forces a write
// across what is in fact a slow connection // across what is in fact a slow connection
ps.flush(); ps.flush();
clientConnection.close(); clientConnection.close();
System.out.println("TcpExample1 completed successfully.");
} }
catch(Exception e) catch(IOException e)
{ {
System.out.println("problem with networking: " + e); System.out.println("problem with networking: " + e);
} }
......
Invocation instructions: Invocation instructions:
1. run/debug TcpExample1Telnet.java 1. run/debug TcpExample1Telnet.java
2. console: telnet localhost 2317 2. console: nc localhost 2317
alternate: telnet localhost 2217
Program responses: Program responses:
=================================================== ===================================================
run: run:
TcpExample1Telnet has started and is waiting for a connection: telnet localhost 2317 TcpExample1Telnet has started and is waiting for a connection:
This server response was written by server TcpExample1 Enter (telnet localhost 2317) or (nc localhost 2317)...
BUILD SUCCESSFUL (total time: 13 seconds) This server response was written by server TcpExample1.
TcpExample1 completed successfully.
BUILD SUCCESSFUL (total time: 5 seconds)
Telnet window: ===================================================
netcat window:
brutzman@DESKTOP-2S09UKA /cygdrive/c/Program Files/NetBeans_11.0/bin
$ nc localhost 2317
This client response was written by server TcpExample1.
=================================================== ===================================================
Telnet window:
don@it154928 /cygdrive/c/Program Files/NetBeans 8.2 don@it154928 /cygdrive/c/Program Files/NetBeans 8.2
$ telnet localhost 2317 $ telnet localhost 2317
Trying ::1... Trying ::1...
Connected to localhost. Connected to localhost.
Escape character is '^]'. Escape character is '^]'.
This client response was written by server TcpExample1 This client response was written by server TcpExample1.
Connection closed by foreign host. Connection closed by foreign host.
===================================================
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment