Thread: problem with send function

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    problem with send function

    i've been working on wrapper class for winsock functions, and i've been tracing down a lag-style issue (the program freeze-lags) and it comes from my send function. if someone could help me out and point out any issues that would really help me out.

    Code:
    //typical call would be ::perSend(crchat, 1024);
    //usually though, the crchat is not 1024 in length. i've tried adding an if(...)break to stop
    //when the delimiter was hit, but it didn't do anything.
    
    INT PEER::perSend(CHAR *databuff, INT len)
    {
    	char *zbuff = databuff;
    
    	while (len > 0)
    	{
    		iSentBytes = send(sckPeer, zbuff, 1, 0);
    		if (iSentBytes == SOCKET_ERROR)
    		{
    			if ((iError = WSAGetLastError()) != WSAEWOULDBLOCK)
    				return -1;
    		}
    
    		iTSentBytes += iSentBytes;
    		zbuff++;
    		len--;
    	}
    
    	return iTSentBytes;
    }
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What delimiter? Have you tried running your program through a debugger to see what's going on? Why are you sending the characters one at a time instead of all at once?
    If you understand what you're doing, you're not learning anything.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if ((iError = WSAGetLastError()) != WSAEWOULDBLOCK)
    And if it is WOULDBLOCK, you skip a character and try and send the next one?

    Do you have something else which you can usefully do for a few mS whilst the network clears some of the backlog?

    How about trying to send as much as possible with each send call, since it returns with the actual amount sent. Using this its possible to advance through the message buffer.
    Code:
    	while (len > 0)
    	{
    		iSentBytes = send(sckPeer, zbuff, len, 0);
    		if (iSentBytes == SOCKET_ERROR)
    		{
    			if ((iError = WSAGetLastError()) != WSAEWOULDBLOCK)
    				return -1;
    		}
    		else
    		{
    			iTSentBytes += iSentBytes;
    			zbuff += iSentBytes;
    			len -= iSentBytes;
    		}
    	}
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    thankyou both for replies. I had originally the function to send everything at once, but I had problems with receiving only the first character 1024 times. I changed it this way and it seemed to work. I will definately try salem's approach, however I did some more debugging, and the lag occurs after the perSend function. So it must be a windows thing. the call is sent from a dialog process function, and there is no lag if the perSend function is not called. But the lag occurs after the send (which nothing else happens, except a return to end the function).
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM