Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is Socket

Tags:

sockets

I don't know exactly what socket means. A server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request. When the server accepts the connection, it 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.

So, socket is some class created in memory? And for every client connection there is created new instance of this class in memory? Inside socket is written the local port and port and IP number of the client which is connected. Can someone explain me more in details the definition of socket?

Thanks

like image 964
Simon Avatar asked Apr 26 '13 09:04

Simon


People also ask

What is a socket explain?

Definition: 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.

What is socket and why we need it?

A socket is an endpoint in communication between networks, and socket programming enables these endpoints to transfer data, thereby supporting communication between networks and programs. Socket programming, for beginners, can play a major role in understanding how networks communicate.

How socket is created?

Socket are generally employed in client server applications. The server creates a socket, attaches it to a network port addresses then waits for the client to contact it. The client creates a socket and then attempts to connect to the server socket. When the connection is established, transfer of data takes place.

Where is socket used?

Sockets are commonly used for client and server interaction. Typical system configuration places the server on one machine, with the clients on other machines. The clients connect to the server, exchange information, and then disconnect. A socket has a typical flow of events.


2 Answers

A socket is effectively a type of file handle, behind which can lie a network session.

You can read and write it (mostly) like any other file handle and have the data go to and come from the other end of the session.

The specific actions you're describing are for the server end of a socket. A server establishes (binds to) a socket which can be used to accept incoming connections. Upon acceptance, you get another socket for the established session so that the server can go back and listen on the original socket for more incoming connections.

How they're represented in memory varies depending on your abstraction level.

At the lowest level in C, they're just file descriptors, a small integer. However, you may have a higher-level Socket class which encapsulates the behaviour of the low-level socket.

like image 83
paxdiablo Avatar answered Sep 19 '22 20:09

paxdiablo


According to "TCP/IP Sockets in C-Practical Guide for Programmers" by Michael J. Doonahoo & Kenneth L. Calvert (Chptr 1, Section 1.4, Pg 7):

A socket is an abstraction through which an application may send and receive data,in much the same way as an open file allows an application to read and write data to stable storage. A socket allows an application to "plug in" to the network and communicate with other applications that are also plugged in to the same network. Information written to the socket by an application on one machine can be read by an application on a different machine, and vice versa.

Refer to this book to get clarity about sockets from a programmers point of view.

like image 45
AJ1993 Avatar answered Sep 20 '22 20:09

AJ1993