1

I am newbie in sockets. I am writing a clent-server application and I got the following issue.

Clent:

void * msgDispatcher(...){
   ...
   while(isRunning){
      read(rxSocket, buf, ...);
   }
}

int main(...){
   ...
   connect(txSocket, ...);
   connect(rxSocket, ...);

   createThread(..., msgDispatcher, ...);
   while(isRunning){
      fgets(buf, ...);
      write(txSocket, buf, ...);
  }
}

Server:

int main(...){
   ...
   listenerSocket = socket(...);
   bind(listenerSocket,...);

   listen(listenerSocket, 10);
   rxSocket = accept(listenerSocket, (sockaddr*) &clientAddr, &addrLength);

   listen(listenerSocket, 10);
   txSocket = accept(listenerSocket, (sockaddr*)  &clientAddr, &addrLength);

   while(isRunning){
      read(rxSocket, ...);
      write(txSocket, ...);
   }
}

All messages that I send from the client are received on the server, but all responses from the server are not received on the client. Server sends messages using write(txSocket, ...); and there are no issues here. But client is waiting all time on the read(rxSocket, ...) and there are no msgs are received.

What is incorrect in this code? How to connect/accept two sockets from one client on the server or is there the best way to do it?

Thanks

6
  • 1
    Why you use two socket, one for RX and another for TX? connection oriented sockets are bidirectional!
    – BigBoss
    Oct 14, 2012 at 12:57
  • How do you ensure that the servers's txSocket is the same as the client's rxSocket? Can't see it from your code.
    – Zane
    Oct 14, 2012 at 13:04
  • BigBoss, I use two sockets because I need to send new requests from the client without waiting for a response from the server. But also I need to receive responses from the server and I've created new thread to read socket. If I use the same socket, the reading blocks the socket(I am planning the response can be sent over 1 min for example) and I cannot send a new request.
    – johanga
    Oct 14, 2012 at 13:26
  • Zane, I think that the txSocket on the server is not corresponded to rxSocket on the client and it causes the issue). I don't know how to check it. And how to do that txSocket on the server will be corresponded to the clinet socket. I supposed my code do it (the second connect on the client is for rxSocket and the second accept on the server is for txSocket).
    – johanga
    Oct 14, 2012 at 13:34
  • You don't need two sockets for that. The client can send as much as it wants to the server before checking for data from the server. There's no reason why the client must wait for a response before sending another request on the same socket. Read about HTTP pipelining, for example.
    – Wyzard
    Oct 14, 2012 at 15:41

1 Answer 1

0

I digged some hours today and return the code as in the question. Two sockets work fine now. RxSocket on client receives all messages from the server and TxSocket sends messages to the server. Looks like it was a typo in the code.

P.S. listen() function can be invoked only one time, but calling twice does not affect anything.

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