Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between port number and socket

I started reading UNIX network programming by W. Richard Stevens and I am very confused between a port and a socket . when I read on internet it said that socket is an endpoint for a connection and for port number it was written that , IP address and port no form a unique pair . So now my question is that :

  • (1) What is the difference between these two ?

  • (2)How are sockets and ports internally manipulated. Are sockets a file ?

  • (3) How is data sent when we send it using an application ?

  • (4) If sockets are there then why do we use port numbers ?

Sorry for my English.. Thanks in advance for the reply.

like image 361
tkhurana96 Avatar asked Mar 06 '16 15:03

tkhurana96


People also ask

What is port and socket addressing?

A socket port is one endpoint in a statement flow in the middle of two programs running over a network, also it is maintaining and allow communication between two different processes on the same or different machines. socket address is the combination of an IP address and a port number.

What is socket number?

The combination of an IPv4 address and a port number is known as the socket number. A pair of sockets, one socket at the client side and other socket at the server side, define the TCP/UDP connection end points. A socket number can uniquely identify a network resource in the whole internet.

How many sockets can a port have?

For most socket interfaces, the maximum number of sockets allowed per each connection between an application and the TCP/IP sockets interface is 65535.


2 Answers

(1) What is the difference between these two ?

A computer running IP networking always has a fixed number of ports -- 65535 TCP ports and 65535 UDP ports. A network packet's header contains a 16-bit unsigned-short field in it specifying which of those ports the packet should be delivered to.

Sockets, on the other hand, are demand-allocated by each program. A socket serves as a handle/interface between the program and the OS's networking stack, and is used to build and specify a context for a particular networking task. A socket may or may not be bound to a port, and it's also possible (and common) to have more than one socket bound to a particular port at the same time.

(2)How are sockets and ports internally manipulated. Are sockets a file ?

That's totally up to the OS; and different OS's do it different ways. It's unclear what you mean by "a file" in this question, but in general sockets do not have anything to do with the filesystem. On the other hand, one feature of Unix-style OS's is that socket descriptors are also usable in the much same way that filesystem file descriptors are -- i.e. you can pass them to read()/write()/select(), etc and get useful results. Other OS's, such as Windows, do not support that feature and for them you must use a completely separate set of function calls for sockets vs files.

(3) How is data sent when we send it using an application ?

The application calls the send() function (or a similar function such as sendto()), passes in the relevant socket descriptor along with a pointer to the data it wants to send, and then it is up to the network stack to copy that data into a packet and deliver it to the appropriate networking device for transmission.

(4) If sockets are there then why do we use port numbers ?

Because you need a way to communicate with particular programs on other computers, and computer A has no way of knowing what sockets are present (if any) on computer B. But port numbers are fixed, so it is possible for programmers to use them as a rendezvous point for communication -- for example, your web browser knows that a web server is almost certain to be listening for incoming HTTP requests on port 80 whenever the server is running, so it can send its requests to port 80 with a reasonable expectation of getting a useful response back. If it had to specify a socket as a target instead, what would it specify? The server's socket numbers are arbitrary and likely to be different every time the server runs.

like image 63
Jeremy Friesner Avatar answered Sep 29 '22 17:09

Jeremy Friesner


1) What is the difference between these two ? (2)How are sockets and ports internally manipulated. Are sockets a file ?

A socket is (IP+Port):

  • A socket is like a telephone (i.e. end to end device for communication)
  • IP is like your telephone number (i.e. address of your socket)
  • Port is like the person you want to talk to (i.e. the service you want to order from that address)

A socket is part of a process. A process in linux is a file.

(3) How is data sent when we send it using an application ?

Data is sent by converting it to bytes. There is little/big endian problem regarding the ordering in bytes so you have to take this into consideration when coding.

(4) If sockets are there then why do we use port numbers ?

A socket is (address + port) that means the person you want to talk to (port) can be reachable from many telephone numbers (IPs) and thus from many sockets (that does not mean that the person on one telephone number will reply to you the same as the one in the other telephone number because his job here/there may be different).

like image 36
Mosab Shaheen Avatar answered Sep 29 '22 16:09

Mosab Shaheen