Thread: sock receive fdset problem

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    sock receive fdset problem

    Code:
    exStr sock_receive()
    {
    	engine::fdset = engine::fds;
    	engine::sr = select(1, &engine::fdset, NULL, NULL, &engine::selectTime);
    	
    	if (engine::sr == 1)
    	{
    		if (FD_ISSET(engine::esock.sock, &engine::fdset))
    		{
    
    			ofstream fout;
    			fout.open("errors.txt", ios::binary | ios::app);
    			fout << "+";
    			fout.close();
    
    			char 
    			buffer[1027] = "", 
    			flag[6]      = "";
    
    			int nRcvd  = 0;
    
    			unsigned int iLen = 2;
    
    			bool gotLen = 0;
    
    			engine::exbuff[0] = '\0';
    
    			do
    			{
    				int rc = recv(engine::esock.sock, &buffer[nRcvd], iLen, 0);
    
    				if (rc > 0)
    				{
    					nRcvd += rc;
    
    					if ((!gotLen) && (nRcvd > 1))
    					{
    						iLen   = unpack(buffer[0], buffer[1]);
    						gotLen = true;
    					}
    				}
    				else
    				{
    					if (rc == -1)
    					{
    						engine::sockRcvErr = -1;
    					}
    					if (rc == 0)
    					{
    						engine::sockRcvErr = 0;
    					}
    				}
    			}
    			while ((!gotLen) || (nRcvd < iLen));
    
    			engine::sockRcvErr = 0;
    
    			process_instruction(buffer, iLen);
    
    			return (engine::exbuff);
    		}
    		else
    		{
    			engine::sockRcvErr = 2;
    		}
    	}
    	else
    	{
    		engine::sockRcvErr = 3;
    	}
    	return ("");
    }
    This function will receive the first message it gets perfectly fine. After that it won't receive any other messages. Can anyone tell me whats wrong?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Why doesn't it receive any other messages? Is recv() failing? Are you sure that there is more data in the socket buffer to be received?

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    The fd is never set after it is set once. I am positive I am sending data to the socket. I send two messages to the same socket and only the first one gets received. No matter how many messages I send only the first one gets received. And no its not receiving two messages at once. I can send them 10 minutes apart and it will only receive the first message.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    How is your code supposed to work? Are the two messages supposed to be received in the do...while loop, or is the sock_receive() function called for each message?

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    Well if something is there to be received in engine::esock.sock shouldn't it be set? I just call the function once every pass through the game loop, and if the sock is set then it should receive a message. But if (FD_ISSET(engine::esock.sock, &engine::fdset)) is only returning true the first time I send a message. Every other time its returning false.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The first parameter you are passing to select is wrong. On unix systems this will fail, on Windows the first parameter is ignored, so you are OK.

    Also, where is your code where you set up the fd_set structure that is passed to select?

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    Incase any other questions arise I'll just post the whole code since there isn't very much. I think it will just save time. Once again thank you for your help so far.

    PHP Code:
    #include "dll.h"
    #include <windows.h>
    #include <cstdio>
    #include <to/crypt.h>
    #include <fstream>

    using namespace std;

    unsigned int unpack(char byteAchar byteB)
    {
        return ((
    static_cast<unsigned int>(byteA) << 8) | byteB);
    }

    char *pack(unsigned int valuechar buffer[2])
    {
        
    buffer[0] = (value >> 8);
        
    buffer[1] = (value 255);

        return (
    buffer);
    }

    struct sockData
    {
          
    SOCKET sock;
          
    sockaddr_in addr;
    };

    namespace 
    engine
    {
        
    sockData esock;

        
    fd_set  fdsetfds;
        
    timeval selectTime;
        
        
    char exbuff[1027];

        
    int srsockRcvErrsockSndErr;
    }
        
    exVal sock_init()
    {
        
    WSADATA w;
        
    int error WSAStartup(0x0202, &w);

        if (
    error)
        {
            
    WSACleanup();
            return (
    0);
        }

        if (
    w.wVersion != 0x0202)
        {
            
    WSACleanup();
            return (
    0);
        }

        return (
    1);
    }

    exVal sock_connect(double portchar *ipaddr)
    {
        
    SOCKET sock socket(PF_INETSOCK_STREAM0);

        
    sockaddr_in addr;
        
    addr.sin_family      AF_INET;
        
    addr.sin_port        htons(static_cast<int>(port));
        
    addr.sin_addr.s_addr inet_addr(ipaddr);

        
    engine::esock.sock sock;
        
    engine::esock.addr addr;

        
    engine::selectTime.tv_sec  0;
        
    engine::selectTime.tv_usec 0;

        
    FD_ZERO(&engine::fdset);
        
    FD_SET(engine::esock.sock, &engine::fds);
        
        if (
    connect(sock, (sockaddr *)&addrsizeof(addr)) == SOCKET_ERROR)
        {
            return (
    0);
        }
        else
        {
            return (
    1);
        }
    }

    void send_gnt(int sockfdchar *buffint len
    {
        while (
    len 0)
        {
            
    int nSent send(sockfdbufflen0);

            if (
    nSent 0)
            {
                
    len  -= nSent;
                
    buff += nSent;
            }
            else
            {
                
    engine::sockSndErr = -1;
            }
        }
    }

    exVal sock_send(double sizechar *buff)
    {
        if (
    size 1023)
        {
            return (
    0);
        }
        else
        {
            
    char buffer[2];
            
    pack(static_cast<int>(size), buffer);

            
    unsigned int keyval unpack(buff[0], buff[1]);

            
    char key[6] = "";
            
    sprintf(key"%05d"keyval);

            
    crypt_comp(buffstatic_cast<int>(size), 2);
            
    crypt_xor(buffkeystatic_cast<int>(size), 62);

            
    send_gnt(engine::esock.sockbuffer2);
            
    send_gnt(engine::esock.sockbuffstatic_cast<int>(size));
            
            return (
    1);
        }
    }

    void process_instruction(char *bufferint iLen)
    {
        
    unsigned int value unpack(buffer[2], buffer[3]);
        
    char flag[6] = "";
        
    sprintf(flag"%05d"value);

        
    crypt_xor(bufferflagiLen 264);
        
    crypt_comp(bufferiLen 24);

        switch (
    value)
        {
            case(
    0): // creation accept
            
    case(1): // login request
            
    {
                
    char instruction[2];

                
    instruction[0] = buffer[4];
                
    instruction[1] = '\0';

                
    strcat(engine::exbuffflag);
                
    strcat(engine::exbuffinstruction);
                break;
            }
            case(
    2): // chat
            
    {
                
    char instruction[iLen 4];

                
    int i 0;

                while (
    iLen 3)
                {
                    
    instruction[0] = buffer[4];
                    
    i++;
                }

                
    strcat(engine::exbuffflag);
                
    strcat(engine::exbuffinstruction);
                break;
            }
            default: 
    int i 0; while (== 0) {;}
        }
    }

    exStr sock_receive()
    {
        
    engine::fdset engine::fds;
        
    engine::sr select(1, &engine::fdsetNULLNULL, &engine::selectTime);
        
        if (
    engine::sr == 1)
        {
            if (
    FD_ISSET(engine::esock.sock, &engine::fdset))
            {

                
    ofstream fout;
                
    fout.open("errors.txt"ios::binary ios::app);
                
    fout << "+";
                
    fout.close();

                
    char 
                buffer
    [1027] = ""
                
    flag[6]      = "";

                
    int nRcvd  0;

                
    unsigned int iLen 2;

                
    bool gotLen 0;

                
    engine::exbuff[0] = '\0';

                do
                {
                    
    int rc recv(engine::esock.sock, &buffer[nRcvd], iLen0);

                    if (
    rc 0)
                    {
                        
    nRcvd += rc;

                        if ((!
    gotLen) && (nRcvd 1))
                        {
                            
    iLen   unpack(buffer[0], buffer[1]);
                            
    gotLen true;
                        }
                    }
                    else
                    {
                        if (
    rc == -1)
                        {
                            
    engine::sockRcvErr = -1;
                        }
                        if (
    rc == 0)
                        {
                            
    engine::sockRcvErr 0;
                        }
                    }
                }
                while ((!
    gotLen) || (nRcvd iLen));

                
    engine::sockRcvErr 0;

                
    process_instruction(bufferiLen);

                return (
    engine::exbuff);
            }
            else
            {
                
    engine::sockRcvErr 2;
            }
        }
        else
        {
            
    engine::sockRcvErr 3;
        }
        return (
    "");


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM