Click here to Skip to main content
16,016,712 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make tcp client and server. When client recv() for second time the following error occures: "Resource temporary unavalible".
This is my client code:
int CreateSocket(int fd)
{
    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		printf("\n Socket creation error \n");
		return -1;
	}

    //make socket non blocking
    //fcntl(fd, F_SETFL, O_NONBLOCK);
  
   

    return fd;
}

int ConnectToServer(int sock)
{
    int res;
	struct sockaddr_in serv_addr;
    // Convert IPv4 and IPv6 addresses from text to binary
	// form
	if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)
		<= 0) {
		printf(
			"\nInvalid address/ Address not supported \n");
		return -1;
	}
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(PORT);
	while ((res= connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) < 0) 
    {
		//printf("\nConnection Failed \n");
        //perror("connect()");
        //if (errno == EINPROGRESS)
        //{
        //   close(sock);
        //   CreateSocket(sock);
        //}
        sleep(1);
	}

    return res;
}


int client()
{
   

    int socketId;
    int fd;
    uint8_t buffer[500];

    struct sockaddr_in server ;

    // Socket Initialization
    socketId = CreateSocket(fd);//Small function to create socket
   
    ConnectToServer(socketId);
    printf("After connect to server\n");

    uint8_t response;
    int send_ret;
    int recv_ret;

   
    while(true)
    {

        recv_ret = recv(socketId,buffer,500,0);
        if(recv_ret < 0)
        {
            perror("recv()\n");
        }
        else if(recv_ret == 0)
        {
            printf("ZERO on recv");
        }
        else
        {
            printf("Received %d bytes \n",recv_ret);
        }

        response = Response(0);

        send_ret = send(socketId,&response,1,0);
        if(send_ret <0)
        {
            printf("Sent: %hhu\n",response);
        }
        if(send_ret == 0)
        {
            printf("Zero return\n");
        }
        else
        {
            printf("Returned : %d bytes",send_ret);
        }


    }

    close(socketId);
}

int main(int argc, char const* argv[])
{
    client();
	return 0;
}


And the server if needed:

RelayServer::RelayServer(char* ip, short port)
    {
        this->ip = ip;
        this->port = port;
    }

int RelayServer::SetupServer(short port) 
{ 
    
    server_socket = socket(AF_INET , SOCK_STREAM , 0); //initialize the address struct 
    server_addr.sin_family = AF_INET; 
    server_addr.sin_addr. s_addr = INADDR_ANY; 
    server_addr.sin_port = htons(port); 
    
    bind(server_socket,(SA*)&server_addr, sizeof(server_addr)); 
    listen(server_socket, BACKLOG);
    printf("Relay server listen at port: %d \n", port);
    return server_socket; 
}

int RelayServer::AcceptNewConnection(int server_socket) 
{ 
    addr_size = sizeof(SA_IN);

    client_socket = accept(server_socket, (SA*)&client_addr, (socklen_t*)&addr_size); 
    if(client_socket < 0)
    {
        perror("Perror()\n");
    }
    printf("Accepted new connection on socket %d\n", client_socket);
    struct timeval tv;
    tv.tv_sec = 2;
    tv.tv_usec = 500000;
    setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);

    //fcntl(client_socket, F_SETFL, O_NONBLOCK); /* Change the socket into non-blocking state	*/
    return client_socket; 
}


int RelayServer::HandleConnection(int client_socket, uint8_t* payload, size_t len)
{
    if((send(client_socket,payload,len, 0 ))<0)
    {
        perror("Send()\n");
    };
    size_t recv_ret = recv(client_socket, buffer, sizeof(BUFFER_SIZE), 0);

    if ( (recv_ret < 0 ) || ( recv_ret == EAGAIN) || ( recv_ret == EWOULDBLOCK ) ) // EAGAIN or EWOULDBLOCK should be applicable only for non-blocking sockets
    {
        return -1;
    }
    else
    {
        printf("Received %lu bytes from the board...\n", recv_ret);
        printf( "<--- buff[0] = %hhu\n", buffer[0] );
        return buffer[0];
    }
}

int RelayServer::SendTcpPacket(uint8_t* payload, size_t len)
{
    int max_socket_so_far = 0;
    if(! server_is_set)
    {
        server_socket = SetupServer(port);
        server_is_set = true;
        cout<<"Relay server socket is set to: "<<server_socket<<endl;
    }

    FD_ZERO(¤t_sockets);
    FD_SET(server_socket,¤t_sockets);
    
    if(FD_ISSET(server_socket, ¤t_sockets))
    {
        cout<<"Current socket is set with value of server_socket"<<endl;
    };

    max_socket_so_far = server_socket + 2;

    while(true)
    {
        //beacause select is destructive
        ready_sockets = current_sockets;
        cout<<"Before select"<<endl;
        //timeval tv;
        //tv.tv_sec = 2;
        //tv.tv_usec = 0;
        int numb_of_desc;
        numb_of_desc = select(max_socket_so_far,&ready_sockets, NULL, NULL, NULL);
        if(numb_of_desc < 0)
        {
            perror("select error");
        }
        //cout<<"Number of descriptors ready to be read from is: "<<numb_of_desc<<endl;
        //cout<<"After select"<<endl;
        for(int i=0; i < max_socket_so_far;i++)
        {
            if(FD_ISSET(i, &ready_sockets))
            {
                if(i == server_socket){
                    //this is a new connection
                    cout<<"FOund server socket"<<endl;
                    client_socket = AcceptNewConnection(server_socket);
                    FD_SET(client_socket, ¤t_sockets);// add client socket to sockets which will be watching
                    if(client_socket > max_socket_so_far) {
                        max_socket_so_far = client_socket;
                    }  
                }
                else
                {
                    //do whatever we do with connections
                    int payload_ret =  HandleConnection(i,payload,len);
                    if(payload_ret != -1)
                    {
                     
                        FD_CLR(i,¤t_sockets);
                        return payload_ret;
                    }
                    else
                    {
                        //close(client_socket);
                        //i = -1;
                        //continue;
                    }
                }
            }
        }
    }


What I have tried:

I am newly to socket programming and I just try to read the man pages for all the functions I was using but don't get whats going on.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900