Sockets in Java

java

TCP provides a reliable, point-to-point communication channel that client-server applications on the Internet use to communicate with each other. To communicate over TCP, a client program and a server program establish a connection to one another. Each program binds a socket to its end of the connection. To communicate, the client and the server each reads from and writes to the socket bound to the connection.

Server

We want to have a client and a server program that can communicate with each other through a socket. The server binds itself to a port number (in our case 1234) and waits for a client connection.

import java.io.*;
import java.net.*;

public class SocketServer {

   int port;

   ServerSocket server = null;
   Socket client = null;

   BufferedReader in = null;
   PrintWriter out = null;

   String input;

   public SocketServer(int port) {
       this.port = port;
   }

   public void listenSocket() {
      try {
         server = new ServerSocket(port);
      } catch (IOException e) {
         System.err.println("Could not listen on port " + String.valueOf(port));
         e.printStackTrace();
         System.exit(-1);
      }

      System.out.println("Opened port " + server.getLocalPort());
      try {
         client = server.accept();
         in = new BufferedReader(new InputStreamReader(client.getInputStream()));
         out = new PrintWriter(client.getOutputStream(), true);
      } catch (IOException e) {
         System.err.println("Could not accept on port " + String.valueOf(port));
         e.printStackTrace();
         System.exit(-1);
      }

      InetSocketAddress clientAddress = (InetSocketAddress)client.getRemoteSocketAddress();
      System.out.println("Connection with client " + clientAddress.getHostName() + " on port " + clientAddress.getPort());
      try {
         input = in.readLine();
         out.println("Pong");
         System.out.println("Input from client: " + input);
      } catch (IOException e) {
         System.err.println("Could not read from port " + String.valueOf(port));
         e.printStackTrace();
         System.exit(-1);
      }

      try {
         client.close();
         server.close();
      } catch (IOException e) {
         System.err.println("Could not close port " + String.valueOf(port));
         e.printStackTrace();
         System.exit(-1);
      }
      System.out.println("Closed port " + String.valueOf(port));
   }

   public static void main(String[] args) {
      SocketServer socketServer = new SocketServer(1234);
      socketServer.listenSocket();
   }
}

Client

The client connects to the server by specifying the hostname and port number of the server (in our case localhost with port 1234).

import java.io.*;
import java.net.*;

public class SocketClient {

   String host;
   int port;

   Socket socket = null;

   BufferedReader in = null;
   PrintWriter out = null;

   String input;

   public SocketClient(String host, int port) {
      this.host = host;
      this.port = port;
   }

   public void listenSocket() {
      try {
         socket = new Socket(host, port);
         out = new PrintWriter(socket.getOutputStream(), true);
         in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      } catch (Exception e) {
         System.err.println("Could not connect to server " + host + " on port " + String.valueOf(port));
         e.printStackTrace();
         System.exit(-1);
      }

      try {
         out.println("Ping");
         input = in.readLine();
         System.out.println("Input from server: " + input);
      } catch (IOException e) {
         System.err.println("Could not write to or read from server " + host + " on port " + String.valueOf(port));
         e.printStackTrace();
         System.exit(-1);
      }
   }

   public static void main(String[] args) {
      SocketClient socketClient = new SocketClient("localhost", 1234);
      socketClient.listenSocket();
   }
}
Advertisement
This entry was posted in programming and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s