开发者

Two-way communication in socket programming using C

开发者 https://www.devze.com 2023-03-22 19:47 出处:网络
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

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开发者_StackOverflow 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.


Check out Beej's Network Programming Guide first of all.

The basic screenplay of a server/client connection goes like this:

  • Server listen()s on a fixed port, with a given socket.
  • Client connect()s to a the server port; client obtains a socket.
  • Server accept()s the connection, and accept() returns a new socket for the connection.
  • (Server continues listening on the original port with the original socket.)

For the specific connection with the client, the server write()s to the new socket it obtained when accept()ing the incoming connection. A busy server will have many, many sockets, but it will only ever need to bind() to one port. All connections come in to that one port, but the OS's networking protocol stack separates the data and makes it available at the connection-specific socket.


You don't need a new socket.

A socket is a duplex connection you can send data in both directions and you can even close the socket from one direction (don't want to write anymore) but still send data from the other direction.


Your socket is bi-directional, so there is no need to create another socket. Unless you are using some sort of middleware, such as Pub/Sub, there is no need to create another socket to enable bi-directional communication.


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.


(Late answer, so mainly for anyone else who comes here looking for help)

I recently put up an example client/server application that closely follows Beej's Guide to Network Programming (which was also recommended by Kerrek SB in his answer). If you're looking for a simple working example of client/server communication, maybe this will help:

https://github.com/countvajhula/dummyclientserver

In particular, no, your client does not need to set up a separate listening socket to receive data from the server -- after the server has accepted the connection from the client, the server can simply send data back to the client on the same socket.

0

精彩评论

暂无评论...
验证码 换一张
取 消