Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Window socket simple problem...

Posted on 2005-03-16
5
Medium Priority
?
255 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
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

578 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