Skip to content

Latest commit

 

History

History
111 lines (66 loc) · 4.52 KB

21. Socket Programming.md

File metadata and controls

111 lines (66 loc) · 4.52 KB

Socket

The Internet was designed to be entirely packet switched.

What is a packet?

It is protocol data unit (PDU) of network layer.

Screen Shot 2022-05-06 at 5 19 14 PM

Each and every packet has to have all the necessary information it needs to get to the destination. Like a letter, a packet must include source and destination addresses.

Every computer connected to the Internet has an Internet Protocol (IP) address Without the uniqueness, there is no way to know the proper destination for packets. TCP/IP takes the addressing one step further with the concept of ports.

Every layer in the protocol adds its own signature and other data (wrapper) to the transmission packet. When transmitted, the wrapper helps the receiver forward the message to the appropriate layer to await reading.

The packet switches from one computer to the next along the connections (or links). If the network loses a link while passing a message, the packet finds another **route **(switching), or the router bounces an error back to the originator if it fails to reach the host.

This ensures a form of data reliability.

Socket is an abstraction built on top of packet switching.

A network socket is a software structure within a network node of a computer network that serves as an endpoint for sending and receiving data across the network. (WebSockets are built on top of network sockets).

Each client interfaces with the operating system by making several calls in succession in order to to connect to a server.

image

The socket() system call does nothing more than create the queues for sending and receiving data.

Only when your program executes a bind() system call does the operating system connect the queue to the network.

connect() connects to peer/server. Reach out and establish a bidirectional channel between your program and another network program.

read() / write() send/receive messages.

close() closes the connection.

Socket programming differs from typical application or tool programming, because you work with concurrently running programs and systems. This means that you need to consider synchronization, timing, and resource management.

Socket API

Most programming languages provide higher-level socket API.

For example,

Py4J

Py4J relies on socket API in both Python and Java.

Py4J enables Python programs running in a Python interpreter to dynamically access Java objects in a Java Virtual Machine. Py4J also enables Java programs to call back Python objects.

class GatewayConnection(object):
    """Default gateway connection (socket based) responsible for communicating
       with the Java Virtual Machine."""

    def start(self):
        """Starts the connection by connecting to the `address` and the `port`
        """
        try:
            self.socket.connect((self.address, self.port))
	...

See more https://github.com/py4j/py4j/blob/master/py4j-python/src/py4j/java_gateway.py.

	/**
	 * <p>
	 * Starts the ServerSocket.
	 * </p>
	 *
	 * @throws Py4JNetworkException
	 *         If the port is busy.
	 */
	protected void startSocket() throws Py4JNetworkException {
		try {
			sSocket = sSocketFactory.createServerSocket();
			sSocket.setSoTimeout(connectTimeout);
			sSocket.setReuseAddress(true);
			sSocket.bind(new InetSocketAddress(address, port), -1);
		} catch (IOException e) {
			throw new Py4JNetworkException(e);
		}
	}

See more https://github.com/py4j/py4j/blob/master/py4j-java/src/main/java/py4j/GatewayServer.java.

*References

https://en.wikipedia.org/wiki/Network_socket

https://github.com/py4j/py4j