Click to See Complete Forum and Search --> : send() and recv() and the same time through 1 socket


connie_liyang
March 4th, 2004, 10:48 PM
Hi,

Can anyone tell me how to send() and recv() through 1 socket?

It is a TCP server, when accept a client connection, it create a thread, recv() the data, and will send() in a while (true) loop. The problem is, during the send() process, I can not recv() anything from this socket.

It is in VC++, I used SOCKET s. send() and recv().

Any links or code will be much thankful.


connie

Mathew Joy
March 5th, 2004, 02:36 AM
A socket is full duplexed, meaning you can send and recieve simultaneously. But practically, you won't get it, simply because there cannot be 2 packets of data in opposite direction on a single wire. So if you are testing your app on a lan, you may not get the full-duplexed nature of the socket. However, you may get on WAN because of the storage facilities (like routers for instance). Are you sending and recving on 2 threads?

connie_liyang
March 8th, 2004, 12:15 AM
Thanks for your reply.

I send and receive in one thread. And the app will run on LAN. Is there any workaround? How to make it work in my situation?

Your suggestion will be much appreciated.


connie

Mathew Joy
March 8th, 2004, 04:42 AM
Well...how do you send()/recv() in a single thread simultaneously? And how do you know that a packet is being send from the peer when you are actively sending data (or when the peer is busy recv()ing )?

connie_liyang
March 11th, 2004, 09:36 PM
Thanks for your reply.

I know the client ip address information from accept(), is that I should create a new socket for sending?



connie

Mathew Joy
March 11th, 2004, 11:37 PM
Originally posted by connie_liyang
Thanks for your reply.

I know the client ip address information from accept(), is that I should create a new socket for sending?I don't understand by what you mean. Anyway if you want to send and recv simultaneously, you need to do on multiple threads. But I think your requirement is different. Are you sure the client is sending data when the server is sending data to the client? Maybe it will help if you can describe your problem more clearly.

connie_liyang
March 12th, 2004, 01:27 AM
Thanks for your response.

My tcp server application supports many clients, there will be a seperate thread to handle each client connection. After 1 of the clients connects, tcp server will send data to it every minute, and at the same time, that client may send data to server. So I need to send() and recv() in the same thread.

The problem is when I send() data, I can not receive.

Is there any better way to handle this?


connie

Mathew Joy
March 12th, 2004, 01:49 AM
OK now we are getting somewhere. So basically you do not wan't to send and recv at the same time, rather you want to recv after a send. or you may not know when you may send or recv.

I'll suggest simple solutions.

if you know you have a recv after a send, then put the recv inside the loop. So you have a while loop, then you have a send and then a recv. Make sure you send/recv all before going to the next recv/send.

if you do not know if the next is send or recv, timeout the recv, using setsockopt() and SO_RCVTIMEO, after waiting for sometime.

if you have a long send or recv and if you think you need to recv or send at the time of send/recv you have to use 2 threads one for send, other for recv. So in other words 2 threads per client.

connie_liyang
March 12th, 2004, 03:38 AM
Thanks for your suggestions. I will have a try.


connie