Skip to content
Snippets Groups Projects
Commit 23d8f515 authored by Brutzman, Don's avatar Brutzman, Don
Browse files

TcpExample3 server, client and terminal log

parent 5f6ed4cf
No related branches found
No related tags found
No related merge requests found
package TcpExamples;
import java.io.*;
import java.net.*;
/**
* Before, we always used telnet to connect to the server. Here we are now
* writing our own program to do the connection.
*
* As you will see, when we run this after we start the server we will see the
* same string telnet printed, sent by the server. The output at the server will
* show different socket pairs for each time we ran it.
*
* @author mcgredo
*/
public class TcpExample3Client {
// IPv6 String constant for localhost address, similarly IPv4 127.0.0.1
public final static String LOCALHOST = "0:0:0:0:0:0:0:1";
public static void main(String[] args) {
try {
while (true) {
System.out.println("TcpExample3Client creating socket...");
// We request an IP to connect to ("localhost") and
// port number at that IP (2317). This establishes
// a connection to that IP in the form of the Socket
// object; the server uses a ServerSocket to wait for
// connections.
Socket socket = new Socket(LOCALHOST, 2317); // locohost?
// Now hook everything up (i.e. set up the streams), Java style:
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
// Read the single line written by the server. We'd
// do things a bit differently if many lines to be read
// from the server, instead of one only.
String serverMessage = br.readLine();
System.out.println("==================================================");
System.out.println("Now we're talking!");
System.out.println("The message the server sent was " + serverMessage);
// socket gets closed, either automatically/silently this code (or possibly by server)
} // end while(true)
}
catch (IOException e) {
System.out.println("Problem with client: "); // describe what is happening
System.out.println(e);
}
// program exit: tell somebody about that
System.out.println("client exit");
}
}
package TcpExamples;
import java.io.*;
import java.net.*;
/**
* Very slightly more complex than example1. A complete copy of example 2. The
* only thing this does differently is introduce a loop into the response, so
* you don't have to restart the program after one response. Also, it prints out
* the socket pair the server sees. Run the program via telnet several times and
* compare the socket pairs.
*
* telnet localhost 2317
*
* If you're sophisticated you can contact the instructor's computer while
* running this program.
*
* telnet [ipNumberOfServerLaptop] 2317
*
* And have him display the socket pairs he got.
*
* @author mcgredo
*/
public class TcpExample3Server {
@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.
System.out.println("TcpExample3Server has started..."); // it helps debugging to put this on console first
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
// Now hook everything up (i.e. set up the streams), Java style:
OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println("This was written by the server"); // this goes back to client!
// 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");
}
}
}
run:
TcpExample3Server has started...
Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 49239 ))
Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 49240 ))
Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 49241 ))
[etc.]
run:
TcpExample3Client creating socket...
==================================================
Now we're talking!
The message the server sent was This was written by the server
TcpExample3Client creating socket...
==================================================
Now we're talking!
The message the server sent was This was written by the server
TcpExample3Client creating socket...
==================================================
Now we're talking!
The message the server sent was This was written by the server
TcpExample3Client creating socket...
==================================================
[etc.]
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