ERODD HOME Malone Home
CPSC 341 - TCP sockets JAVA example
Earl Rodd erodd@malone.edu Extension 8546 www.malone.edu/erodd

Overview

Below are shown TCP client and socket programs. They are available for the lab at:
R:/erodd/sockets
or LINUX
/home/square/tdrive/erodd/sockets
They are coded with the host "localhost" which means both can run on the same machine. To trace the activity with Ethereal, set the interface to "any" and the filter to "port 6543".

TCP client

/* TCP sockets client. * Reads from the user and sends data to a server and gets the answer. */ import java.io.*; import java.net.*; class tcpcli { public static void main(String argv[]) throws Exception { String mydata; String moddata; /* Create a BufferedReader to read the console. */ BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in)); /* Create a socket and open a TCP connection. * localhost means my own machine. */ Socket clisock = new Socket("localhost",6543); /* Rather than send one byte at a time, create * a PrintWriter from a DataOutputStream from the * OutputStream of the socket. Note that if we used * the writeBytes() method of the DataOutputStream, * each packet will have only 1 byte! */ DataOutputStream toserv = new DataOutputStream( clisock.getOutputStream()); PrintWriter pout = new PrintWriter(toserv, true); /* Create a BufferedReader from the socket's * InputStream to read a whole line. */ BufferedReader fromserv = new BufferedReader(new InputStreamReader(clisock.getInputStream())); /* Get some data from the user. */ System.out.println("Enter a string to send to the server."); mydata = inFromUser.readLine(); /* And send it on the socket to the server. */ pout.println(mydata); /* And wait for an answer */ moddata = fromserv.readLine(); /* And display it on the console. */ System.out.println("From the Server: " + moddata); clisock.close(); } }

TCP Sockets server

/* TCP Sockets server. */ /* Listens on port 6543 and returns the data it gets in upper case. */ import java.io.*; import java.net.*; class tcpserv { public static void main(String argv[]) throws Exception { String clidata; String capdata; /* Establish a port number for the server */ ServerSocket serv1 = new ServerSocket(6543); while (true) { /* "Listen" for a connection request from a client */ Socket consock = serv1.accept(); /* Get a reader so we can read the whole piece of * data, not just one character, from the client. */ BufferedReader inFromCli = new BufferedReader( new InputStreamReader(consock.getInputStream())); /* Setup to send a whole string back to the client, * not just a character. We could use the writeBytes() * method of DataOutputStream, but this sends one * character per TCP packet! So we get a PrintWriter. */ DataOutputStream tocli = new DataOutputStream( consock.getOutputStream()); PrintWriter pout = new PrintWriter(tocli, true); /* Read a line of data from the client */ clidata = inFromCli.readLine(); System.out.println("Received from client: " + clidata); /* Make it upper case so we can see that something * happened. */ capdata = clidata.toUpperCase() ; /* Use println method so there is a newline at the end * so client can use the readLine() method to read.*/ pout.println(capdata); } } }