Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two-way communication in socket programming using C

I have a small doubt in socket programming. i am able to send my data from client to server and my server processes the data. The o/p of the data processed, I want to send back to my client. So can we "write" the data back to the client using the same socket. I mean a server listens on a port before accepting connection and receiving data, so similarly, do i need to make my client listen to some other port (bind it some other socket) and make my server connect to that socket and transfer the data back. Any kind of example or explanation or references would be appreciated. Thanks a lot in advance.

like image 362
user537670 Avatar asked Jul 24 '11 23:07

user537670


1 Answers

Technically it is right, the socket is duplex and you can send the data to the same socket you read from:

   SOCKET s = socket()
   ... //Connect
   int size = receive(s,...);
   //make response 
   send(s, ...);

But in practice it depends on what are you going to do. It is possible to hang out socket if you have the following situation:

  1. Process 1 sends very big data (<100K) over the socket by one send operation

  2. Process 2 receives data from 1 by portions and sends small packets to 1 (~20b). It is not a

    confirmations, but some external events. The situation goes into hangout, where the sending buffer of the 2 is full and it stops sending confirmations to 1. 2 and 1 are hanging in their send operations making a deadlock. In this case I'd recommend using two sockets. One for read, one for write.

like image 139
Andrey Borisov Avatar answered Oct 01 '22 07:10

Andrey Borisov