RSS

Java Socket handshaking: the simplest example

20 Aug

In an earlier post, I have discussed how to implement socket communication with python. I recently was asked to implement similar functions, but in Java. Here is how I did that.

  • Create a new Java project, let’s name it JSocketClient;
  • Create another Java project, let’s name it JSocketServer;
  • In both projects, create an App class, and add a static Main function respectively:

    image

  • In the Client project, add a class JSocketClient:

           private Socket _client;
        private String _ServerName = “localhost”;
        private int _port = 8089;
       
        _client = new Socket(_ServerName, _port);

     public void SendString(String data) {
            if (_client == null) {
                System.out.println(“Client:\tEmpty Socket client. Please retry”);
                return;
            }
            OutputStream oStream;
            try {
                System.out.println(“Client:\tSending data:” + data);
                oStream = _client.getOutputStream();
                DataOutputStream out = new DataOutputStream(oStream);
                out.writeUTF(data);           

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

  • In the server project, add a class JSocketServer:

    import java.io.DataInputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.SocketTimeoutException;

    public class JSocketServer extends Thread {
        private ServerSocket serverSocket;
        public JSocketServer(int port) {
            try {
                serverSocket = new ServerSocket(port);
                serverSocket.setSoTimeout(10000);

            } catch (IOException e) {           
                e.printStackTrace();
            }
        }

        public void run() {
            while (true) {
                try {
                    System.out.println(“Server:\tWaiting for client on port “
                            + serverSocket.getLocalPort() + “…”);               
                    Socket server = serverSocket.accept();               
                    System.out.println(“Server:\tJust connected to “
                            + server.getRemoteSocketAddress());               
                    DataInputStream in = new DataInputStream(
                            server.getInputStream());
                    System.out.println(“Server:\t\t Got Data: ” + in.readUTF());                           
                    server.close();               
                } catch (SocketTimeoutException s) {
                    System.out.println(“Socket timed out!”);
                    break;
                } catch (IOException e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
    }

  1. Run the server project, where in the main() function:

    public class App {
        public static void main(String[] args) {
            JSocketServer t = new JSocketServer(8089);
            t.start();
        }
    }

  2. Run the client project, where in the main() function:

    public class App {
        static JSocketClient client;
        public static void main(String[] args) {
              client=new JSocketClient();        
        }
    }

That is it! Now let’s see the result in console of Eclipse:

image

That is it. Happy coding.

 
Leave a comment

Posted by on August 20, 2013 in Java, Programming

 

Tags: , , , ,

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

 
%d bloggers like this: