Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Slow Winsock Issue

Posted on 2007-03-29
3
Medium Priority
?
1,108 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
[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
3 Comments
 
LVL 6

Accepted Solution

by:
bijopuli earned 1400 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:Deepu Abraham
Deepu Abraham earned 600 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 …
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

704 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