What Is a Socket?
Server runs on a particular laptop and incorporates a socket that’sabsolute to a particular port variety. The server simply waits, taking note of the socket for a consumer to create a association request.
On the client-side: The client knows the hostname of the machine on which the server is running and the port number on which the server is listening. To make a connection request, the client tries to rendezvous with the server on the server’s machine and port. The client also needs to identify itself to the server so it binds to a local port number that it will use during this connection. This is usually assigned by the system.
If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client. It needs a new socket so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client.
On the client side, if the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server.
The client and server can now communicate by writing to or reading from their sockets.
A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to.
An endpoint is a combination of an IP address and a port number. Every TCP connection can be uniquely identified by its two endpoints. That way you can have multiple connections between your host and the server.
A socket is a communications connection point (endpoint) that you can name and address in a network. Socket programming shows how to use socket APIs to establish communication links between remote and local processes.
The processes that use a socket can reside on the same system or different systems on different networks. Sockets are useful for both stand-alone and network applications. Sockets allow you to exchange information between processes on the same machine or across a network, distribute work to the most efficient machine, and they easily allow access to centralized data. Socket application program interfaces (APIs) are the network standard for TCP/IP. A wide range of operating systems support socket APIs. IBM® i sockets support multiple transport and networking protocols. Socket system functions and the socket network functions are threadsafe.
Programmers who use Integrated Language Environment® (ILE) C can refer to this topic collection to develop socket applications. You can also code to the sockets API from other ILE languages, such as RPG.
The Java™ language also supports a socket programming interface.
Sockets share some common characteristics.
- A socket is represented by an integer. That integer is called a socket descriptor.
- A socket exists as long as the process maintains an open link to the socket.
- You can name a socket and use it to communicate with other sockets in a communication domain.
- Sockets perform the communication when the server accepts connections from them, or when it exchanges messages with them.
- You can create sockets in pairs (only for sockets in the AF_UNIX address family).
The connection that a socket provides can be connection-oriented or connectionless. Connection-oriented communication implies that a connection is established, and a dialog between the programs follows. Optionally, the server can assign a name to the service that it supplies, which allows clients to identify where to obtain and how to connect to that service. The client of the service (the client program) must request the service of the server program. The client does this by connecting to the distinct name or to the attributes associated with the distinct name that the server program has designated.
Connectionless communication implies that no connection is established, over which a dialog or data transfer can take place. Instead, the server program designates a name that identifies where to reach it (much like a post-office box). If you send a letter to a post office box, you cannot be absolutely sure that the receiver got the letter. You might need to wait for a response to your letter. There’s no active, period of time association, during which information is changed.
How Socket Work
Sockets square measureunremarkably used for consumer and server interaction. Typical system configuration places the server on one machine, with the shoppers on alternative machines. The shoppershook up with the server, exchange data, so disconnect. A socket incorporates a typical flow of events. in a very connection-oriented client-to-server model, the socket on the server method waits for requests from a consumer. To do this, the server 1st establishes (binds) associate degree address that shopperswill use to search out the server. once the address is established, the server waits for shoppers to request a service. The consumer-to-server information exchange takes place once a client connects to the server through a socket. The server performs the consumer’s request and sends the reply back to the client. the subsequent figure shows the standard flow of events (and the sequence of issued APIs) for a connection-oriented socket session. a proofof every event follows the figure
A socket has a typical flow of events. In a connection-oriented client-to-server model, the socket on the server process waits for requests from a client. To do this, the server first establishes (binds) an address that clients can use to find the server. When the address is established, the server waits for clients to request a service. The client-to-server data exchange takes place when a client connects to the server through a socket. The server performs the client’s request and sends the reply back to the client.
The following figure shows the typical flow of events (and the sequence of issued APIs) for a connection-oriented socket session. An explanation of each event follows the figure.
This is a typical flow of events for a connection-oriented socket:
- The socket() API creates an endpoint for communications and returns a socket descriptor that represents the endpoint.
- When an application has a socket descriptor, it can bind a unique name to the socket. Servers must bind a name to be accessible from the network.
- The listen() API indicates a willingness to accept client connection requests. When a listen() API is issued for a socket, that socket cannot actively initiate connection requests. The listen() API is issued after a socket is allocated with a socket() API and the bind() API binds a name to the socket. A listen() API must be issued before an accept() API is issued.
- The client application uses a connect() API on a stream socket to establish a connection to the server.
- The server application uses the accept() API to accept a client connection request. The server must issue the bind() and listen() APIs successfully before it can issue an accept() API.
- When a connection is established between stream sockets (between client and server), you can use any of the socket API data transfer APIs. Clients and servers have many data transfer APIs from which to choose, such as send(), recv(), read(), write(), and others.
- When a server or client wants to stop operations, it issues a close() API to release any system resources acquired by the socket.