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.

Internet Address IP

With Java you can use the java.net package to find out details of your PC address or somewhere else (like www.google.com) with using the InetAddress class. The InetAddress is a implementation of the Internet Protocol basically.

To gain the local pc address you can use

InetAddress theHost = InetAddress.getLocalHost();
System.out.println("Local host address: "+theHost);

which would get the local PC address and display it to the screen, for me it would something like

Local host address: genux-laptop/127.0.1.1

you can get the individual parts of the above address, e.g. genux-laptop and 127.0.0.1

System.out.println("Local host name : " +theHost.getHostName());
System.out.println("Local address : "+theHost.getHostAddress());

and the output would be

Local host name :  genux-laptop
Local address : 127.0.1.1

you can also use the InetAddress to obtain remote addresses for example www.google.com so to pull details back from google you could use the getByName function that is within the InetAddress class as.

System.out.println("Host address of google.com : "+ InetAddress.getByName("www.google.com"));

Here is a full code that does the above in full

import java.io.*;
import java.net.*;
 
public class address {
		public static void main(String[] args)
		{
			// a inetaddress class holds details of internet protocol (IP) address
 
			InetAddress theHost = null;
			// you can get the local host (your PC) addresss
			try {
				theHost = InetAddress.getLocalHost();
				System.out.println("Local host address: "+theHost);
			}
			catch (UnknownHostException unknow)
			{
				System.out.println("Error : "+ unknow.toString());
				return;
			}
			// if there is no local host then, oppss!!.. you should really have a local host name
			// also you get can the local host name and address individual 
			String theHostName = theHost.getHostName();
			String theHostAddress = theHost.getHostAddress();
 
			System.out.println("The host name "+ theHostName);
			System.out.println("The host address " + theHostAddress);
 
			try {
				// you can a reverse look up as such by passing in the local name to get the IP address
				System.out.println("Host address by host name : "+ InetAddress.getByName(theHostName));
 
				// if you wanted to get a google host name then just pass in www.google.com
				System.out.println("Host address of google.com : "+ InetAddress.getByName("www.google.com"));
			}
			catch (UnknownHostException unknow)
			{
				System.out.println("Error : "+ unknow.toString());
			}
		}
}

and the output would be

Local host address: genux-laptop/127.0.1.1
The host name genux-laptop
The host address 127.0.1.1
Host address by host name : genux-laptop/127.0.1.1
Host address of google.com : www.google.com/216.239.59.105