Network - socket (1)
Tags: osi, http, tcp, websocket
Published: 2024-02-26 23:45:54

## Socket

### How / Why socket is appeared?

To make programs to operate on the network easier, system divide the network in to OSI 7 layers and manage it.

![OSI-7-layer-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uopslf108cs9v84ivmhd.png)


![OSI-7-Layer-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1hfic7emw4r633ow1627.png)


BUT, there are limitations to dividing the layers. WHY? Each protocol at each layer is a kind of communication protocol, but specific implementation functions are needed inside for protocol implementation.

Sockets provide the body of these functions. We can use sockets without separate implementations. In other words, instead of defining the detailed specifications of the protocol one by one, we can use sockets.

### What is Socket?

A connector, typically utilizing the TCP/IP protocol, is created to enable programs to connect to the network environment for sending and receiving data.


![TCP-IP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/afdzyvube26ty8h2jmr8.png)


To summarize, a Socket:

- Is placed on top of the transport layer in the TCP/IP 4-layer model.

- Provides code for protocol control of the transport layer above the transport layer.

- In essence, a socket is an endpoint. It exists at both ends of communication, akin to entrances and exits.

### What Socket do?


![What-Socket-do](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2r7bvtsaodl7wg8fb1oi.png)


1.Connecting software to software

- Connects via IP and service ports.

2.Data communication between software

- Locates sockets on the internet, established connections, and transmits data.


### What is Socket Programming?

The communication programming using Sockets, which are used for inter-process communication, is called Socket Programming.

It is divided into Client Socket and Server Socket. The socket that accepts communication connection requests is called the "Server Socket", and the socket that sends communication connection requests is called the "Client Socket".

🔥 Although they have the same structure of sockets, they differ slightly in their flow of processing based on their roles.


### Socket API Execution Flow (Client, Server)

![API-Execution-Flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b940lxz488plkvpq265e.png)

- Client: Socket creation, connection request, data transmission/reception, closing the socket.

- Server: Socket creation, binding, listening, accepting, data transmission/reception, closing the socket.

#### Client Flow

1.Create Client Socket
- Create a socket (Empty box socket) with out information about the connection target. When creating the socket, we need to specify the type of socket. For TCP socket, we can specify a stream type, and for UDP sockets, we can specify a datagram type.

2.Connection Request
- Send a request to the target we want to connect to, asking to establish a connection. Specify the target destination you want to connect to using the IP address and service port number.

The execution of "Connect" is not completed simply by sending the request; it is completed only when the result of the request comes back.

> What is service port?
> - Service Identification Number
> - Used in the form of 'IP address: service port'
> - Enables conversations like "What is the port number for connecting to XX service?"
> - Default service ports (e.g., port 22 for initial SSH connection, port 80 for HTTP setup)

3.Data Transmission (Send, Receive)

Similar to connection requests, execution is not completed just by sending a request; it is completed only when a response (signal) to the request is received

However, when sending data, you know when and how much data you are sending, but when receiving, there is a difference in that you do not know when and how much data the other party will send. Therefore, the receiving API is typically executed in a separate thread.

4.Closing the Socket

When it is determined that there will be no further data transmission, the socket is closed

#### Server Flow

0.When I try to send data, the receiving end should not blindly accept data; it should only accept the appropriate process identified by the port number.

1.Create Server Socket
Similar to the client socket, create a shell socket without information about the connection target.

2.Binding (Bind)
When using a computer, numerous services are utilized simultaneously, meaning many processes are running concurrently. If the port numbers of these processes are the same, confusion may arise when the server socket needs to send data back. Therefore, it's necessary to bind the socket with a unique port number to ensure the server socket can create its unique port number.


![Binding-Client socket to system socket](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hqeeb8bkf8zo2puhpk63.png)



If the port number used by a socket overlaps with the port number of another socket, what would happen? If all sockets use the same port number, say 10000, then there would be a problem determining which socket should handle data received on port 10000. For this reason, operating systems internally manage port numbers and socket connection information to ensure that sockets do not use duplicate port numbers.

By the way, a single process can bind multiple sockets with the same port number. This means a host can create multiple sockets with the same port and exchange data with other hosts.

Thanks to this, we can chat with many people simultaneously using a single chat app.

So, imagine a scenario where a specific port opens multiple sockets: the first socket for mom, the second for dad, the third for chatting with siblings on Chat app.

3.Client Connection Request Waiting

Once the server socket has completed the binding with the port number, it is ready to accept connection requests from clients.

It waits until a connection request comes from the client and, upon receiving the request, exits the waiting state and returns.

4.Client Connection Establishment
The actual connection begins here!

The server socket, upon accepting a connection request, creates a new socket.

The main role of the server socket is to wait for client connection requests. Therefore, when it receives a connection request from a client, it opens a new socket, maps it with the client socket, and passes it on.

5.Data Transmission (Send, Receive)
Same as for the client.

6.Closing the Socket
Same as for the client. However, the server socket also needs to manage the sockets it creates!