Solved

Slow Winsock Issue

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Re-position sub-options beneath the TAB 7 99
Problem to open text file 11 131
Message not shown 5 67
How do I get Window Title of all opened process? 4 15
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

821 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