Skip to content

Instantly share code, notes, and snippets.

@goodhamgupta
Created September 14, 2022 10:33
Show Gist options
  • Save goodhamgupta/1ddc891721e576482119123e017f6575 to your computer and use it in GitHub Desktop.
Save goodhamgupta/1ddc891721e576482119123e017f6575 to your computer and use it in GitHub Desktop.

Sockets

  • Two-sided communication links between two programs in a network.
  • Connection: Machines are aware of the each other's network location(IP) and TCP port
  • Can be TCP or UDP based.

Socket Types

Two kinds of sockets: Server(wait for request) and Client(receive and send data)

Server socket can be created as follows:

try {
  ServerSocket s = new ServerSocket(port);
  while (true) {
    Socket incoming = s.accept();
    // Handle a client
    incoming.close();
  }
  s.close();
  } catch (IOException e) {
    // Handle exception
  }
}

Screenshot 2022-09-14 at 5 54 44 PM

Commnuication between sockets can be implemented using:

  • TCP
    • Slower than UDP because of multiple connections, but provides better reliability.
    • Introduces overhead.
  • UDP
    • UDP adds nothing to IP. Hence, the communication over UDP is faster and has less protocol and implementation overhead as TCP has

Multithreading

Supports multithreading out-of-the-box, and can be setup using the relevant class(MultithreadedHttpServer for a HTTP Server):

try {
       ServerSocket serverSocket = new ServerSocket(80);
       while(true) {
             System.out.println("listening ...");
             Socket socket = serverSocket.accept();
             System.out.println("accepted connection !");
             (new MultithreadedHttpServer(socket)).start();
       }
} catch(IOException e) { }

// Now a new thread processes each request. The result is a new socket connection for each request on various ports.

Pros vs Cons

Pros

  • Language independent. Servers/clients can be implemented in any Language
  • Provides low-level access to establish and maintain connection between points in a network, and hence has less execution overhead. If high performance is a must for the system, sockets can be considered.

Cons

  • Almost everything that happens after the connection has been established between the machines is upto the user.
  • Message handling, message formatting, retries, etc. are not built-in, and custom code is required to implement these features. However, there are some flags that can be used to improve the performance of network connections during setup(such as SO_TIMEOUT, TCP_NODELAY, etc).
  • Programs built using sockets directly will tend to be verbose, making it difficult to understand and debug.

RMI

  • RMI is a Java implementation for RPC.
  • It is an abstraction built on top of sockets, making it easier for the developer to write and understand programs.

Pros:

  • Hides much of the network specific code, you don't have to worry about specific ports used (but you can if you want),
  • RMI handles the formatting of messages between client and server. However, this option is really only for communication between Java programs.
  • built-in support for retries and error handling.
  • Has a bunch of properties that improve logging, making it easier to debug large programs.

Cons:

  • Specific to Java.
  • Introduces some overhead since there is an additional server running(RMI Register), and also because of the abstraction.
  • Need to ensure that the implementation is thread-safe, since remote method invocations on the same remote object may execute concurrently. (This is more of a implementation detail than a disadvantage).
  • RMIRegister has some problems on MacOS, which is specific to the JDK version. From the assignment:
When running on certain MacOS versions, certain java (JDK) versions may experience long delay when get- ting replies from rmiregistry. This is a problem with JDK and MacOS, and is not related to our assignment (or StressTest). While people have worked out fixes, to simplify things, we suggest that you avoid running your pro- gram using JDK over MacOS.

Conclusion

  • Based on the projects we've seen, and the fact that we're going to implement our assignment in Java, I think we can use RMI instead of Sockets.
  • The overhead introduced by RMI will be compensated by the improved debugging and out-of-the-box features provided.
  • The problem specific to MacOS can be avoided by possibly developing the solution on Ubuntu/Docker.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment