Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Server-Client Socket


An Android App which implements the client-side socket.

NetworkConnection is used to manage the socket connection:

  1. Connect to server: public void connect(final String serverIp, final int serverPort)
  2. Disconnect to server: public void disconnect(final String disconnectReason)
  3. Send data to server: public void sendData(final String data)
  4. Receive data from server through a thread.

It defines a callback for UI to update:

public interface ConnectionListener {
    void onConnected(final boolean isConnected, final String disconnectReason);
    void onDataReceived(final String data);
    void onDataSent(final String data, final boolean succeeded);

It has a boolean flag mIsConnected to record connection status. When connection status changed, et: open/close socket, network disconnect, it updates this flag, and invokes the callback to notify UI.

So UI don't need to care about the details of connection (socket, input/output stream), just register this callback and use the public method to connect/disconnect/send data to server. commit #2f452ea


A Java Project which implements the server-side socket. When server is running, it waits new client connection, receive data from client, send response to this client, and broadcast the data to other clients. To run the server:

$ ./

To support multiple clients socket connection, create a thread pool (through ExecutorService), commit #aac37a0.


  • Server can only receive the first message from client. commit #65796b8
  • Java socket API: How to tell if a connection has been closed? We have to close stream and socket when disconnect. The right way to detect disconnect is to catch the IOException. commit #dee5bce



Client2, 2017-01-14,

You can’t perform that action at this time.