Available at:
https://cpsc.rodd.us/sockets/tcpserv.java
/* 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);
System.out.println("Local addr " + serv1);
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);
if (capdata.equals("QUIT"))
{
System.out.println("Client sent quit so ending..");
System.exit(0);
}
}
}
}
Available at:
https://cpsc.rodd.us/sockets/tcpcli.java
/* 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. */
System.out.println("Enter IP address to connect to...");
String servip = inFromUser.readLine();
Socket clisock = new Socket(servip,6543);
System.out.println("clisock = " + clisock);
/* 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();
}
}