TCP Client

As from the TCP Server, this is the client that will connect to the server and output the response from the server.

Since we are the client, we need to know where the server is to connect to, other wise we would not connect to anywhere!!!.. since within this tutorial I am using the local host for running the server as well (you could alter this to where you want to run the server on)

InetAddress theHost = InetAddress.getLocalHost();
// get the local hostname as well
String theHostName = theHost.getHostName();

and with this information you can create a new Socket to connect to the server on the port 9999 (which is the same port number that the server is listening on)

// open the socket connection with the server on port 9999
Socket theSocket = new Socket(theHostName, 9999);

to gain the server data, I am using the ObjectInputStream, with passing the socket from above connection to the server, then just read a line from the ObjectInputStream into a String local variable.

ObjectInputStream ois = new ObjectInputStream(theSocket.getInputStream());
String response = (String)ois.readLine();
// just output the response.
System.out.println("Response was : "+response);

and that is it, all very simple since most of the “interesting” stuff over the networking connections happening under the hood of java, which is just great.

Here is the full source code.

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
 
public class TCPClient {
	InetAddress theHost;
	Socket theSocket;
	int port = 9999;
 
	public TCPClient() {
		try {
			// the local host IP address - where you want to connect to.. 
			theHost = InetAddress.getLocalHost();
			// get the local hostname as well
			String theHostName = theHost.getHostName();
			// open the socket connection with the server on port 9999
			theSocket = new Socket(theHostName, port);
 
			System.out.println("Server connection opened, going to listen");		
 
			ObjectInputStream ois = new ObjectInputStream(theSocket.getInputStream());
			String response = (String)ois.readLine();
 
			System.out.println("Response was : "+response);
			theSocket.close();
		} catch (UnknownHostException ExecHost)
		{
			System.out.println("Unknown Host Error");
		}
		catch (IOException ExceIO)
		{
			System.out.println("Error creatin socket : "+ExceIO.getMessage());
		}
	}
 
	public static void main(String[] args) {
		new TCPClient();
	}
 
 
}

If you save as TCPClient.java and then to compile and run (of course you need to be running in another console the TCPServer since the client will have nothing to connect to !!!)

javac TCPClient.java
java TCPClient

and the output would be

Server connection opened, going to listen
Response was : Hi from the server

you could use wireshark to watch what is happening.

TCP Server

As a follow on from the IP Address in java, the next thing would be a server/client over the internet. This is a small demo on how to create TCP connections over IP Address (TCP basically means that the data sent over the connection will fully be sent, e.g. a linux cd image), shall do one in a UDP which is more of a send data and do not really care if data is sent across the network like radio, if you do miss part of a song it does not really matter.

So to start with, I am using the port number 9999, lets say that you are in a office with different phones running on different extension numbers but using the main telephone number to make calls on, so in this setup the IP address would be main office telephone number (one number to talk on) and the port number would be the different telephones extensions that you can talk on.

To create a server socket you need to pass in the port number as well.

ServerSocket theServerSocket = new ServerSocket(9999);

then to just wait for a connection to try and connect you

Socket sock = theServerSocket.accept();

which waits for a client connection (accept) , since the socket also has the clients IP address you can use that to make sure that you want to accept connections from that IP address if you wanted to, but to output the InetAddress you could use the “sock” from the above code

System.out.println("The client IP address is " + sock.getInetAddress());

the only other thing is that you can send data to the client which using a ObjectOutputStream with passing in the “sock” from the above code

ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
oos.writeChars("Hi from the server");
oos.close();

Here is the full code

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
 
public class TCPServer {
	// the IP class as such
	InetAddress theTCPServer;
	// port to talk over
	int portNumber = 9999;
	// the socket for the communication to happen on
	ServerSocket theServerSocket;
 
	/* 
	 * Setup the server socket communication 
	 */
	public TCPServer() {
		try {
			// create the server socket on the port number
			theServerSocket = new ServerSocket(portNumber);
			System.out.println("Server created on port : "+portNumber);
		} catch (IOException ExecIO)
		{
			System.out.println("Error creating the server socket : "+ExecIO.getMessage());
		}
	}
 
 
	public void connections()
	{
		try {
			// accept a connection 
			Socket sock = theServerSocket.accept();
			System.out.println("Server accepted connection, send to handler");
 
			// print out the clients IP Address
			System.out.println("The client IP address is " + sock.getInetAddress());
 
			// send the message to the client
			ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
			System.out.println("Server socket opened");
			oos.writeChars("Hi from the server");
			oos.close();
 
			// close the socket
			sock.close();
 
		} catch  (IOException ExecIO)
		{
			System.out.println("Error creating connection : "+ExecIO.getMessage());
		}
	}
 
	public static void main(String[] args) {
		TCPServer theServer = new TCPServer();
		theServer.connections();
	}
}

save as TCPServer.java and then to compile, and run

javac TCPServer.java
java TCPServer

and the output would be something like

Server created on port : 9999
Server accepted connection, send to handler
The client address is /127.0.1.1
Server socket opened

you could use wireshark to watch what is happening.