1

I have a weird query. I have learned that a socket is a combination of IP and Port. So what is socket descriptor? Is it just an integer? What does it do?

Can I have to different socket descriptors on the same port? If yes, then can those be of different types (TCP/UDP)?

I know these are silly questions; I have been blindly using SD for quite a time now :P

Ry-
  • 199,309
  • 51
  • 404
  • 420
Ajit
  • 443
  • 2
  • 6
  • 17
  • See almost identical question: http://stackoverflow.com/questions/6437383/tcp-and-udp-sockets-on-same-port – Nicholas Wilson Dec 20 '12 at 09:36
  • @NicholasWilson : It is the exact link. The only doubt now is. I feel in this case there will be 2 different File descriptors for same IP and Port which is logically confusing. If I assume that there are 2 pointers to same socket and then it is ok but what is the need for those as the client request itself will indicate the kind of data transfer (TCP/UDP). – Ajit Dec 20 '12 at 11:06

2 Answers2

3

TCP and UDP are independent, so you can have TCP and UDP sockets on the same port.

A socket descriptor is to a socket as a file descriptor is to a file.

A TCP connection is actually defined by the tuple: local IP, local port, remote IP, remote port. You can have multiple connections with the same local IP and port, as long as they have different remote IP and/or port.

For instance, a web server uses its local port 80 for all the connections. But each client connection will either come from a different machine (and hence a different remote IP) or different sockets on the same machine (so they'll have the same remote IP but different remote ports).

Barmar
  • 596,455
  • 48
  • 393
  • 495
  • @Barmar..Thanks for the reply but I think there is some ambiguity in the asnwer provided by you and by stamhany (below). Please check. – Ajit Dec 20 '12 at 08:01
  • @Barmar is right, there can be two sockets on the same port, but you will get two different socket descriptors. I have corrected my answer above – stamhaney Dec 20 '12 at 08:28
  • @Barmar..thanks for the update. I think I am understanding it now.. :-) – Ajit Dec 20 '12 at 09:43
0

A socket descriptor is a unique integer returned by the system when you ask it to create a socket with the socket call. Each socket is identifiable by its socket descriptor.

As regards the second part of your question, You will get a different socket descriptor for the same IP+PORT+PROTOCOL, so yes, you can have tcp and udp sockets on the same port, but you will get two different socket descriptors

You should read network programming tutorials like these first: Beej's Network Programming Tutorial

stamhaney
  • 1,196
  • 8
  • 18
  • Thanks, Its clear now. So if I want multiple connection established then all those needs to be connected to same socket. Like when we connect to gmail (ignore server cluster). There is a single socket descriptor on port 80/443 serving all the connection's. – Ajit Dec 20 '12 at 07:59
  • @Guys, the following link say we can have multiple sockets for same port : http://stackoverflow.com/questions/6148174/can-mutiple-sockets-be-associated-with-same-port-for-udp . Does multiple socket mean multiple socket descriptor ? – Ajit Dec 20 '12 at 08:05
  • stamhaney, I don't think you've quite got it. What sockets or connections you can have and on which ports is totally unrelated to what socket descriptors you have to refer to the connection in userspace. @Ajit No, for UDP it means multiple sockets. – Nicholas Wilson Dec 20 '12 at 09:36
  • @NicholasWilson : Bro every time I get near to it there a wrong turn. So is it like this ? : and number identifies a socket(IP+Port). There can be a different number for same (IP+Port) providing different kind of connection. – Ajit Dec 20 '12 at 09:51
  • @NicholasWilson, Didnt quite understand your point. Do you mean a socket descriptor is not what is returned by a socket call for a particular protocol and port? – stamhaney Dec 20 '12 at 09:52