BSD Socket: Send Data but never Receive

Moderator
Posts: 613
Joined: 2004.09
Post: #1
Ok guys this is the last thing i need to do before i can move back to my own programming. I have 2 Apps Client/Server. The client connects to the server fine. It will let me write net data fine no problems. The issue is the data is never received by the server.

I know this stuff can be a huge pain, so if anyone can help me fix this I will paypal you $30. I know im bribing now but im sick of trying to get it to work and im comming up quick on a deadline.

Here is the code that is being used. I will also post the whole source on my site (link at end) if anyone wants to look at it.

CLIENT
Code:
int WriteNetData(int socket, void *buffer, int numBytes)
{
    Ptr bytes = buffer;  //ptr to buffer
    int count, n;
    
    n = count =0;
        
    while(count< numBytes)  //loop until we have sent all bytes
    {
        n = send(socket, bytes, numBytes - count, 0); //send..... CRASHES HERE
    
        if(n>0)
        {
            count += n;
            bytes += n;
        }
        
        else
            if(n<0) //error
                return(-1);
    }
    
    return(count);
}

Code:
static Boolean IsSocketReadyForWrite(int socket)
{
    struct fd_set connectionSet;
    struct timeval timeout;
    int result;
    
    //set timeout values
    timeout.tv_sec =0;
    timeout.tv_usec =0;
    
    FD_ZERO(&connectionSet);
    FD_SET(socket, &connectionSet);
    
    //call select to see if data is waiting on the socket
    result = select(socket+1, nil, &connectionSet, nil, &timeout);
    
    //check results
    if (result < 0 )
        return (false);
    if (result>0)
    {
        if(FD_ISSET(socket, &connectionSet))
            return(true);
        else
            return(false);
    }
    
    return(false);
}

SERVER
Code:
Boolean IsDataWaitingToBeRead(int socket)
{
    struct fd_set connectionSet;
    struct timeval timeout;
    int result;
    
    //Set timeout and set values
    timeout.tv_sec = 0;    //no timeout
    timeout.tv_usec = 0;
    
    FD_ZERO(&connectionSet);   //use our macro to clear the set
    FD_SET(socket, &connectionSet); //use our macro to set socket
    
    //Call Select to see if there is data waiting on this socket
    
    result = select(socket+1, &connectionSet, nil, nil, &timeout);
    
    //check results
    if (result < 0)  //no data
        return(false);
    if (result > 0)
    {
        if (FD_ISSET (socket, &connectionSet))
            return(true); //data avaiable
        else
            return(false);
    }
    
    //we can never get here
    return(false);
}

Code:
int ReadNetData(int socket, void *buffer, int numBytes)
{
    Ptr        bytes = buffer;                                // Ptr to buffer
    int        count, n;

    n = count = 0;

    while (count < numBytes)                            // loop until we've got all the bytes
    {
        n = recv(socket, bytes, numBytes - count, 0);        // read data
        
        if (n > 0)                                        // we got some bytes
        {
            count += n;                                    // inc byte count
            bytes += n;                                    // inc ptr to buffer
        }
        else
        if (n < 0)                                            // error
            return(-1);
    }
        
    return(count);
}

Basicly all im trying to do is send a string over a network connection, I dont know why im having so much trouble. Ive had this work in the past, i suspect i messed up some small annoying detail.

Full Code at http://www.dragon-forged.com/netcode.zip

Thanks Guys!

PS. Once i get all the networking going im going to open source a real barebones version of it to make things easier for people in the future.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Member
Posts: 116
Joined: 2002.04
Post: #2
Kyle,

Your problem is in your wait() routine of the server.

You're trying to read the data off the socket contained in gHostListenerSocket. However, that's no longer the correct socket since it's the listening socket.

Once you call accept on a pending connection, accept returns a new socket which is the connected socket. The listening socket remains listening. You seemed to understand this in your code when you returned the new socket from CheckForConnectionRequestFromClient(), but you then discarded that and called ReadNetData() on the listening socket, which won't work.

Your wait routine should be modified like this:

Code:
- (IBAction)wait:(id)sender
{
    int connectedSocket = 0;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    while ((connectedSocket = CheckForConnectionRequestFromClient()) < 0)
    {
        //we wait for the connection to be established
        //since this is a server we will always be waiting
        //so this loop will run for a long time
        //if the code is modified make sure there
        //are no memory leaks in here or it will
        //result in a nasty crash.
    }
    printf("Connected!\n");
    
    if (ReadNetData(connectedSocket, &number, 8) <= 0)
    {
        printf("Failed to read any data\n");
    }
    
    
    printf("Net Data: %i\n", number);
    close(connectedSocket);    

    
    [pool release];
}

(Actually, rather than just closing the socket, you should probably be calling shutdown() first.)

(PS. If you really want to give money, please donate it to iDevGames)

Wade
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #3
I will make a donation in your name, thanks for the help again wade.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Socket Library on OSX? MacFiend 16 12,473 Apr 15, 2010 05:44 AM
Last Post: Kirienko
  How to check if data come in from the socket without hanging there!!! KingdomHeart 2 5,928 Nov 23, 2007 09:25 AM
Last Post: KittyMac
  OpenGL + Socket!!! Flush socket problem!!! KingdomHeart 4 4,870 Nov 21, 2007 10:14 AM
Last Post: Zekaric
  serializing C++ into Cocoa objects to send over DO wizumwalt 2 4,688 Apr 7, 2007 12:55 AM
Last Post: wizumwalt
  NSStream Send &amp; Receive Handler jasonjohnson 3 7,158 Sep 29, 2005 09:14 AM
Last Post: jasonjohnson