?
Solved

Window socket simple problem...

Posted on 2005-03-16
5
Medium Priority
?
249 Views
Last Modified: 2010-04-15
Hi, everybody!
      I am using the "recv()" function to receive stream data. The "recv()" is called by multi-threads.

The case is:
          The "recv()" sometimes receives an empty stream and if I called it again, it can read the right stream again. Does this function need to synchronize? (All the buffer I use in this function is local, not share)

thanks.
potato
0
Comment
Question by:small_potato
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13562087
>The "recv()" is called by multi-threads.
On the same socket???

Make sure that your implementation of recv is thread safe ... refer to documentation of recv on your platform. And yes you should synchronize access to socket between all your threads
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13562090

It sounds like you have more than one recv() listening on the same connection.

Bad move.  Each task/thread can execute its own recv(), but each recv() should be responsible for only one connection.


Anything else and you have to manage the handshake yourself.

Kent
0
 

Author Comment

by:small_potato
ID: 13562161
The program structure is like this:

/*
The working thread to receive the data from socket.
when a new connection is accepted, a thread will be created to handle this connection.
*/
void TCPL_ThreadRecv(void* lpvThreadlParm)
{
...
iRtn=pThis->TcpRecv(pBuf,DEF_MINI_USER_BUF,pSock,&pBufRtn);
...
}


TCPLisnFL::TcpRecv(char *pBuf,const unsigned long bufflen,sockTCP* pSock,
                            char** ppBufRtn)
{
...
 /*
 this function can retrieve a stream but the stream is empty  (all the value is zero), the iRtn value is NON zero
*/
  iRtn =  recv(pSock, pBuf, bufflen,0);

/*
 if I read it again, then it can be read.
*/
  if ( *(pBuf) == 0 )
     iRtn =  recv(pSock, pBuf, bufflen,0);
...
}

thanks
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 750 total points
ID: 13562953
/*
The working thread to receive the data from socket.
when a new connection is accepted, a thread will be created to handle this connection.
*/

In that case you have different threads calling recv on different sockets ... no worries

I would believe that your sockets are blocking so recv() returns only when it has something to show

> this function can retrieve a stream but the stream is empty  (all the value is zero), the iRtn value is NON zer
You mean iRtn is positive value ... It could be that you have been sent a string of 0s ... Run a sniffer on your machine and confirm ... You can donwload and use either ethereal or tcpdump ... googling for them should show sufficient information
0
 
LVL 22

Expert Comment

by:grg99
ID: 13564530
Check errno,  the recv call may be returning some kind of error.  In particular, if you have any timer alarms going off, recv and most other system calls return EAGAIN.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question