# How does two sockets on same address and port work?



## riowang (Sep 6, 2011)

For some limit of my design, I create and bind two sockets on same IP address and same port, each of the socket is created and bound in one process, using the socket option of SO_REUSEADDR . And my operating system doesnÂ´t support child process or thread. I am using a IP stack based on BSD. I want the process with the first socket to be a packet sender and the process with the second socket to be a listener with a infinite loop of recv() called.
My assumption is that the both of the sockets can send packets to the remote address and the second bound socket can receive the packets from remote. But the result is that, the second socket can receive packets untill the first socket sends a packet out. After that, it seems that the sockets are switched, the second socket is inactive and canÂ´t receive anything.
I am wondering if this is the correct behavior of BSD socket? And how sendto() function impact the BSD changing the socket configuration/setting for the sockets?


----------



## expl (Sep 6, 2011)

Thats normal behavior. SO_REUSEADDR is not for having several sockets working with same address but for fast socket rebinding in case the first socket was not closed when its process crashed or hanged and you need to attach another socket on the fly for less downtime. The net stack switches the sockets on activity, you cant actually have two sockets binded to same address.


----------



## riowang (Sep 6, 2011)

*Is poll or recvfrom also a kind of activity?*

I am the activity which can switch the sockets in this case.


----------



## riowang (Sep 6, 2011)

I mean,is there a way to switch the socket back to another one after sending a packet?


----------



## expl (Sep 6, 2011)

To be more specific, do not use sockets like that. What is your operating system? If it is POSIX friendly you should be able to share same socket descriptor between different processes and then use either signals or some other IPC method to synchronize.


----------



## riowang (Sep 6, 2011)

I am using ENEA OSE, I don't think it can fork the process...


----------



## expl (Sep 6, 2011)

No, you do not need to fork. Spawn two processes. Make first process create and bind a socket and save the descriptor to file (or put it in shared memory or any other method to share data between processes). Second process can just read descriptor "id", assign it to a local variable and use it normally.


----------



## riowang (Sep 6, 2011)

Thanks! it sounds good! but still have questions for this. The file descriptor is only valid in one process if I understand correctly, how does the "id" from one process work in another independent process? Wish I can have some example if possible.


----------



## expl (Sep 6, 2011)

No, file descriptors are valid for other processes as well.


----------



## riowang (Sep 6, 2011)

Every process has its own file descriptor table, does it mean the id of the FD is only the index in the table and this number won't work in other's table?


----------



## expl (Sep 6, 2011)

FD is an index in kernel's relational array to objects (also known as file descriptor table). So its not something binded to a single process or based on process heap or stack. Or at least it is so on POSIX(like/compatible) operating systems.


----------



## riowang (Sep 6, 2011)

Good to know, I chose the design like this because I thought I can't just give the FD to anther process directly.
I will verify this in my system tommorrow and report the result.
Tack!


----------

