New to TCP\IP need help....

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

New to TCP\IP need help....

1,192 Views
satinderKhera
Contributor I

Hi All,

 

I am new to TCP\IP and want to develop a server application with MCF52259 i.e my board will be in listening mode and the communication will be initiated by a remote client. For developing this application I am using FNET stack for freescale. To develop my confidence I am trying to recieve a byte from remote client and send the same back to the client but I am unable to do it. I am pasting my test code, can someone help me and suggest if I am doing something wrong.

 

/*******************************/

int main(void)
{

int   server_socket,client_socket;
unsigned int   data_length = 1;
int   data_length1 = 1;
unsigned int   RecvMsgSize=0;
char   TCPIPRxBuffer[256];
 struct sockaddr_in    server_address,client_address;

 

 /*creating a listening socket*/
 server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

 

 /*filling the relevant data */
fnet_memset(&server_address, 0, sizeof(server_address));  
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = 1234;

 /*binding the socket address to the listening socket*/
 if((FNET_OK == bind(server_socket, (struct sockaddr *) &server_address,sizeof(server_address))))
 {
 /*binding successful*/
 }
 else
 {
 fnet_printf("\nbinding error\n");            
 }

 if((FNET_OK == listen(server_socket, 10)))
 {
            /*listening on socket*/
}
else
{
fnet_printf("\nlistening error\n");            
}

 

    while (TRUE)
    {
        /*waiting for a connection and accepting it*/
        data_length1 = sizeof(client_address);
        client_socket = accept(server_socket,(struct sockaddr *) &client_address,
        &data_length1);
    
        /*recieving from accepted connection*/    
        if(FNET_OK == recv(client_socket,(char*)TCPIPRxBuffer,data_length1,0))
        {

       }
        
        send(client_socket, (char*)TCPIPRxBuffer, 256, 0);


        /*closing the socket*/
        if(FNET_OK == closesocket(client_socket))
        {
            
        }
        else
        {
              fnet_printf("\nsocket closing error\n");            
        }
    }

}

 

 

Please suggest whether I am missing something or doing something wrong.

 

Regards,

 

Satinder Singh

Labels (1)
0 Kudos
3 Replies

437 Views
butok
NXP Employee
NXP Employee

Hi Satinder Singh,

 

I see several issues in the code:

- the code is written by the way that accept(), recv() and send() are blocked. They are not blocked.

- the code does not check the accept() return value.

- the code checks recv() for FNET_OK. The recv() never returns FNET_OK: http://fnet.sourceforge.net/manual/group__fnet__socket_ga554ce7ef429f34cc322dff78061d60ab.html#ga554...

 

More info: http://fnet.sourceforge.net/manual/group__fnet__socket.html

 

To avoid most of the issues, as a reference for your server code you can use the sources provided with the FNET project, for example the HTTP, TELNET servers.

 

Best regards,

Andrey Butok

0 Kudos

437 Views
satinderKhera
Contributor I

Dear Sir,

 

I tried after implementing the changes suggested by you, but I am still unable to communictae through TCP\IP. I have done the changes as shown below:

 

    /*waiting for a connection and accepting it*/
        data_length1 = sizeof(client_address);
        if((client_socket = accept(server_socket,(struct sockaddr *) &client_address,
        &data_length1))>0)
        {
            if(FNET_OK == setsockopt(client_socket, SOL_SOCKET, SO_BLOCKING, (char*)&data_length1, sizeof(client_socket)))    
            {
                
                /*recieving from accepted connection*/    
                recv(client_socket,(char*)TCPIPRxBuffer,data_length1,0);
                
                send(client_socket, (char*)TCPIPRxBuffer, 256, 0);
                /*closing the socket*/
            }
        }
       closesocket(client_socket));

 

Please comment if i am still doing something wrong and suggest the correction.

Regards,

Satinder Singh     

0 Kudos

437 Views
satinderKhera
Contributor I

Dear Mr. Butok,

 

Thanks for your reply, from the issues raised by you I understand the following things:

 

- the code is written by the way that accept(), recv() and send() are blocked. They are not blocked.

I understand that I need to set the socket option to blocking mode for the client socket i.e.I need to add the following line in the code after calling accept( )

if(FNET_OK == setsockopt(client_socket, SOL_SOCKET, SO_BLOCKING, (char*)&data_length1, sizeof(client_socket)))

 

- the code does not check the accept() return value.

I need to chek the accept( ) success\failure

 

if((client_socket = accept(server_socket,(struct sockaddr *) &client_address,
        &data_length1))>0)

this is required to be done.

 

- the code checks recv() for FNET_OK. The recv() never returns FNET_OK

this check is to be removed.



I will do the changes suggested by you and will get back to you after trying these changes.


Thanks & Regards,

Satinder Singh


0 Kudos