1

I am implementing in C on Windows : A channel of communication between a sender and a receiver.

The channel has to receive packages from sender, transfer them to the receiver and then back from receiver to the sender.

The Channel connects to sender (maybe multiple senders) on port 1 and connects to the receiver on port 2

What is a good way of doing it? Both ports 1 and 2 have to be able to send and receive. how to I use select in such situation?

I want to use a single threaded environment.

1
  • to make question more clear, how do i use select to transfer sender-->receiver and after back from receive-->sender ? if i use select with same sockets in read and write, can a port be in both read and write state after the call to select ? confusing
    – Michael
    Commented Mar 1, 2011 at 20:21

2 Answers 2

2

Not sure either about your exact question, but indeed TCP sockets are bidirectional. You can put the same file descriptor in both the read and write (and except) FD_SETs in the same select call.

2
  • thanks ! so i put the same FD_SET both in read and write. then for one way (sender to receiver) i check if sender fd is ready to send and also check if receiver fd is ready to receive ? then on the way back i do select again ? this is a bit confusing since i have to transfer one way first and then transfer back after.
    – Michael
    Commented Mar 1, 2011 at 20:20
  • 1
    you put all the FDs you need to be able to read in the read FD_SET, and all those you need to write from in the write FD_SET. A socket can be put in both sets, and indeed both sets can contain the same sockets, but the FD_SETs need to be different. When select returns (without error) you check which FDs were ready to read or write (with FD_ISSET), and do whatever needs to be done. Don't forget to close all the sockets properly when done!
    – Mat
    Commented Mar 1, 2011 at 20:35
0

The question isn't very clear but TCP connections are bi directional - and there's no need to open a separate socket for each direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.