Solved

Slow Winsock Issue

Posted on 2007-03-29
3
1,078 Views
Last Modified: 2013-11-13
The recv function in my program seems to be taking a large amount of time.  This is the code I'm using to append the received data into an std::string

char tempBuffer[8192];
while((ret = recv(wsaSocket, tempBuffer, sizeof(tempBuffer), 0)) > 0){  
            strHTML.append(tempBuffer, ret);
            ZeroMemory(tempBuffer, sizeof(tempBuffer));
}

This part of my program takes between 300 and 700ms.  I wrote the same program using the winsock control in vb6 and it took between 200 and 300ms. I assume that my lack of knowledge using c++ sockets is causing the problem, but I'm not sure what I would need to change. I googled and came up with using TCP_NODELAY and changing the timeout of the recv function, but both had very little affect on my programs speed.  Any suggestions would be appreciated.
0
Comment
Question by:cx323
3 Comments
 
LVL 6

Accepted Solution

by:
bijopuli earned 350 total points
ID: 18821579
Hi

         int RecvTimeout ( int s, char *buf, int len, int timeout )
         {
                 fd_set fds ;
                 int n ;
                 struct timeval tv ;

                 // Set up the file descriptor set.
                 FD_ZERO(&fds) ;
                 FD_SET(s, &fds) ;

                 // Set up the struct timeval for the timeout.
                 tv.tv_sec = timeout ;
                 tv.tv_usec = 0 ;

                 // Wait until timeout or data received.
                 n = select ( s+1, &fds, NULL, NULL, &tv ) ;
                 if ( n == 0  ) return -2 ;                      // Timeout
                 if ( n == -1 ) return -1 ;                      // Error

                 // data must be here, so do a normal recv
                 return recv ( s, buf, len, 0 ) ;
         }

Regards
Bijo.
0
 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 150 total points
ID: 18821677
Yes, as Bijo said, select() is what you need to use.

Don't forget to trap timeout(-2) or error (-1) (return values from RecvTimeout() )

Have a look at this thread:
http://www.governmentsecurity.org/archive/t2817.html

Also event based methods 'WSAEvent' are really effective.

Best Regards,
DeepuAbrahamK
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18822028
I would also try to avoid copying from one buffer to the other - those are wasted cycles. Try to put it directly into the string instead.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now