Solved

Slow Winsock Issue

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which Linux flavors will this run on? 6 116
Android development question 2 111
C++ help/ Toy problem 19 71
One named event, multiple event handlers 2 60
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…
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

752 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