Quantcast
Channel: Programming – Xinyustudio
Viewing all articles
Browse latest Browse all 284

Java Socket handshaking: the simplest example

$
0
0

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.


Filed under: Java, Programming Tagged: example, how to, java, Socket, tutorial

Viewing all articles
Browse latest Browse all 284

Trending Articles