用于linux的socket程序中的recvfrom api

时间:2014-02-24 09:48:34

标签: c linux sockets udp

int acceptSocket;
char buf[100];
long sentbytes;
socklen_t len;
int port = 18227;

int CreateSocket()
{
    long rc;
    struct sockaddr_in addr, client;

       // Socket creation for UDP
       acceptSocket=socket(AF_INET,SOCK_DGRAM,0);

       if(acceptSocket==-1)
       {
         printf("Failure: socket creation is failed, failure code\n");
         return 1;
       }
       else
       {
         printf("Socket started!\n");
       }

     memset(&addr, 0, sizeof(struct sockaddr_in));
     addr.sin_family=AF_INET;
     addr.sin_port=htons(port);
     addr.sin_addr.s_addr=htonl(INADDR_ANY);
     rc=bind(acceptSocket,(struct sockaddr*)&addr,sizeof(addr));

     if(rc== -1)
     {
       printf("Failure: listen, failure code:\n");
       return 1;
     }
     else
     {
       printf("Socket an port %d \n",port);
     }

     while(rc!=-1)
         {
         len =sizeof(client);
         rc=recvfrom(acceptSocket,buf, sizeof(buf), 0, (struct sockaddr*) &client, &len);
         if(rc==0)
         {
           printf("Server has no connection..\n");
           break;
         }
         if(rc==-1)
         {
             printf("something went wrong with data %s", strerror(errno));
           break;
         }

         XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port );

            makeTimer("First Timer", &firstTimerID, 2, 2);   //2ms
                makeTimer("Second Timer", &secondTimerID, 10, 10);    //10ms
                makeTimer("Third Timer", &thirdTimerID, 100, 100);  //100ms
          }

       close(acceptSocket);
       return 0;
     }

int main()
{
     Xcp_Initialize();
     CreateSocket();
     return 0;
}

//API for sending the data to the client.
void XcpApp_IpTransmit( uint16 XcpPort,  Xcp_StatePtr8 pBytes, uint16 numBytes )
{
    struct sockaddr_in client;

        if ((long)XcpPort==port){
                sentbytes = sendto(acceptSocket,(char*)pBytes,(long)numBytes,0, (struct sockaddr*)&client, sizeof(client));
        }
        XcpIp_TxCallback(port,(uint16)sentbytes);
}

我创建了一个服务器端程序,用于从客户端接收数据并将响应发送回客户端。我不知道上面程序中的错误在哪里,我能够从客户端接收数据,在这种情况下rc值是zer0。然后我正在检查rc的值,它正在从程序中出来。以后没有回复客户的回复。客户端是一种将数据发送到指定端口和IP地址的工具。

可能是什么问题以及为什么在recvfrom api之后rc值为零? 注意:客户端将继续发送数据。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

根据recvfrom手册页

  

成功完成后,recvfrom()将返回该长度   消息以字节为单位如果没有可以收到的消息和   peer已执行有序关闭,recvfrom()应返回0。   否则,函数应返回-1并设置errno以指示   错误。

所以我认为在你的情况下当另一个主机关闭连接时,recvfrom()的返回值为0(这种断开连接是否优雅无关紧要)

此外,我已更新您的代码,请在最后尝试

#define BUFLEN 100  //Max length of buffer
#define PORT 18227   //The port on which to listen for incoming data

void die(char *s)
{
    perror(s);
exit(1);
}

int acceptSocket;
char buf[BUFLEN];
long sentbytes;

int CreateSocket()
{
    struct sockaddr_in si_me, si_other, si_other2;

    int s, i, slen = sizeof(si_other) , recv_len;

    //create a UDP socket
    if ((acceptSocket=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        die("socket");
    }

    // zero out the structure
    memset((char *) &si_me, 0, sizeof(si_me));

    si_me.sin_family = AF_INET;
    si_me.sin_port = htons(PORT);
    si_me.sin_addr.s_addr = htonl(INADDR_ANY);

    //bind socket to port
    if( bind(acceptSocket , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
    {
        die("bind");
    }

    //keep listening for data
    while(1)
    {
        printf("Waiting for data...");
        fflush(stdout);

        //try to receive some data, this is a blocking call
        if ((recv_len = recvfrom(acceptSocket, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1)   // read datagram from server socket
        {
            die("recvfrom()");
        }

        //print details of the client/peer and the data received
        printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));         printf("Data: %s\n" , buf);

        XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port );

        makeTimer("First Timer", &firstTimerID, 2, 2);   //2ms
        makeTimer("Second Timer", &secondTimerID, 10, 10);    //10ms
        makeTimer("Third Timer", &thirdTimerID, 100, 100);  //100ms
    }

    close(s);
 }