Simon
Simon

Reputation: 2035

What exactly is Socket

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

Upvotes: 25

Views: 11646

Answers (5)

Noobcprogrammer
Noobcprogrammer

Reputation: 43

Socket definition:

A communication between two processes running on two computer systems can be completely specified by the association: {protocol, local-address, local-process, remote-address, remote-process} We also define a half association as either {protocol, local-address, local-process} or {protocol, remote-address, remote-process}, which specify half of a connection. This half association is also called socket, or transport address. The term socket has been popularized by the Berkeley Unix networking system, where it is "an end point of communication", which corresponds to the definition of half association.

Upvotes: 3

Jin Lim
Jin Lim

Reputation: 2152

I found this article in online.

So to put it all back together, a socket is the combination of an IP address and a port, and it acts as an endpoint for receiving or sending information over the internet, which is kept organized by TCP. These building blocks (in conjunction with various other protocols and technologies) work in the background to make every google search, facebook post, or introductory technical blog post possible.

https://medium.com/swlh/understanding-socket-connections-in-computer-networking-bac304812b5c

Upvotes: 2

Amit Sharma
Amit Sharma

Reputation: 83

A network socket is one endpoint in a communication flow between two programs running over a network.

A socket is the combination of IP address plus port number

This is the typical sequence of sockets requests from a server application in the connectionless context of the Internet in which a server handles many client requests and does not maintain a connection longer than the serving of the immediate request:

Steps to implement

At Server side

initilize socket() 
--
bind() 
-- 
recvfrom() 
--
(wait for a sendto request from some client) 
-- 
(process the sendto request) 
--
sendto (in reply to the request from the client...for example, send an HTML file)

A corresponding client sequence of sockets requests would be:

socket() 
--
bind() 
--
sendto() 
--
recvfrom()

so that you can make a pipeline connection .. for more http://www.steves-internet-guide.com/tcpip-ports-sockets

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 882716

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.

Upvotes: 23

AJ1993
AJ1993

Reputation: 121

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.

Upvotes: 8

Related Questions