Solved

Slow Winsock Issue

Posted on 2007-03-29
3
1,085 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WPF issue with Trigger 2 106
FMX TCameraComponent Problem 2 70
Installshield for Embarcadero EX 10.1 Berlin 4 42
Math Equation 23 88
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

809 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