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.

Leave a Reply

Your email address will not be published.