How do I create a client-server socket communication?

Category: java.net, viewed: 140K time(s).

In this example you'll see how to create a client-server socket communication. The example below consist of two main classes, the ServerSocketExample and the ClientSocketExample. The server application listen to port 7777 at the localhost. When we send a message from the client application the server receive the message and send a reply to the client application.

The communication in this example using the TCP socket, it means that there is a fixed connection line between the client application and the server application.

001.package org.kodejava.example.net;
002. 
003.import java.io.IOException;
004.import java.io.ObjectInputStream;
005.import java.io.ObjectOutputStream;
006.import java.lang.ClassNotFoundException;
007.import java.lang.Runnable;
008.import java.lang.Thread;
009.import java.net.ServerSocket;
010.import java.net.Socket;
011. 
012.public class ServerSocketExample {
013.    private ServerSocket server;
014.    private int port = 7777;
015. 
016.    public ServerSocketExample() {
017.        try {
018.            server = new ServerSocket(port);
019.        } catch (IOException e) {
020.            e.printStackTrace();
021.        }
022.    }
023. 
024.    public static void main(String[] args) {
025.        ServerSocketExample example = new ServerSocketExample();
026.        example.handleConnection();
027.    }
028. 
029.    public void handleConnection() {
030.        System.out.println("Waiting for client message...");
031. 
032.        //
033.        // The server do a loop here to accept all connection initiated by the
034.        // client application.
035.        //
036.        while (true) {
037.            try {
038.                Socket socket = server.accept();
039.                new ConnectionHandler(socket);
040.            } catch (IOException e) {
041.                e.printStackTrace();
042.            }
043.        }
044.    }
045.}
046. 
047.class ConnectionHandler implements Runnable {
048.    private Socket socket;
049. 
050.    public ConnectionHandler(Socket socket) {
051.        this.socket = socket;
052. 
053.        Thread t = new Thread(this);
054.        t.start();
055.    }
056. 
057.    public void run() {
058.        try
059.        {
060.            //
061.            // Read a message sent by client application
062.            //
063.            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
064.            String message = (String) ois.readObject();
065.            System.out.println("Message Received: " + message);
066. 
067.            //
068.            // Send a response information to the client application
069.            //
070.            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
071.            oos.writeObject("Hi...");
072. 
073.            ois.close();
074.            oos.close();
075.            socket.close();
076. 
077.            System.out.println("Waiting for client message...");
078.        } catch (IOException e) {
079.            e.printStackTrace();
080.        } catch (ClassNotFoundException e) {
081.            e.printStackTrace();
082.        }
083.    }
084.}
085. 
086.package org.kodejava.example.net;
087. 
088.import java.io.IOException;
089.import java.io.ObjectInputStream;
090.import java.io.ObjectOutputStream;
091.import java.lang.ClassNotFoundException;
092.import java.net.InetAddress;
093.import java.net.Socket;
094.import java.net.UnknownHostException;
095. 
096.public class ClientSocketExample {
097.    public static void main(String[] args) {
098.        try {
099.            //
100.            // Create a connection to the server socket on the server application
101.            //
102.            InetAddress host = InetAddress.getLocalHost();
103.            Socket socket = new Socket(host.getHostName(), 7777);
104. 
105.            //
106.            // Send a message to the client application
107.            //
108.            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
109.            oos.writeObject("Hello There...");
110. 
111.            //
112.            // Read and display the response message sent by server application
113.            //
114.            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
115.            String message = (String) ois.readObject();
116.            System.out.println("Message: " + message);
117. 
118.            ois.close();
119.            oos.close();
120.        } catch (UnknownHostException e) {
121.            e.printStackTrace();
122.        } catch (IOException e) {
123.            e.printStackTrace();
124.        } catch (ClassNotFoundException e) {
125.            e.printStackTrace();
126.        }
127.    }
128.}

To test the application you need to start the server application. Each time you run the client application it will send a message "Hello There..." and in turns the server reply with a message "Hi...".



Books Recommendation

Free eBooks Free Oracle Magazine

Sponsored Links

Jobs
Junior PHP Developer
New York, NY
Kargo
Network Administrator
Japan
Nexus Corporation
Software Test Engineer (SDET)
Redmond, WA
Microsoft
Experienced Game Programmer - Online
United Kingdom
Interactive Selection
SharePoint Developer
San Antonio, TX
GlobalNow Inc.
Software Development Manager --...
Toronto, ON, Canada
undisclosed
Powered by JobThread