Difference between port number and socket

Viewed 3272

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.

2 Answers

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).

Related