繁体   English   中英

适用于Linux操作系统的udp中的sendto api?

[英]sendto api in udp for linux operating system?

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);
}

我创建了一个服务器端程序,用于从客户端接收数据并将响应发送回客户端。 我能够通过recvfrom api从客户端接收数据,但是稍后我必须通过XcpApp_IpTransmit(uint16 XcpPort,Xcp_StatePtr8 pBytes,uint16 numBytes)中的sendto api将响应发送回客户端。 (这是我的项目中支持传输数据的api)。 客户端是用于将数据发送到指定端口和IP地址的工具。 我可以再次使用struct sockaddr_in客户端吗? 在Transmit API中? 有人可以帮我吗??

这将起作用。 使用SOCK_DGRAM ,您始终可以使用接收到的地址(通常是其大小)将数据发送回去。 当然,例外情况包括对等套接字不可用的情况。

或者,如果只有一个客户端,则可以使用connect设置默认的远程对等地址,然后send将使用它:

sockaddr_in addr;
socklen_t len;

len = sizeof(addr);
x = recvfrom(socket,buf, sizeof(buf), 0, (struct sockaddr*) &addr, &len);
connect(socket, (struct sockaddr*)addr, len);
...
send(socket, buf, bufsize, 0);
send(socket, buf, bufsize, 0);
send(socket, buf, bufsize, 0);

不过,有一条评论。 您正在使用AF_INET,您的代码将正常运行。 但是您也可以使其与其他协议兼容。 为了那个原因:

sockadd_storage addr;
socklen_t len;

len = sizeof(addr);
x = recvfrom(socket,buf, sizeof(buf), 0, (struct sockaddr*) &addr, &len);
...
sendto(socket,buf,bufsize,0,(struct sockaddr*)addr, len);

这种类型的代码可与任何数据报协议一起使用,包括AF_LOCAL和AF_INET6。

struct sockaddr_in client; 您是否在传输API中使用了它初始化位置错误的地方?

因此,您必须使用初始化的套接字结构。 从上面的代码中,您可以从客户端接收成功的数据,但是无法发送,因为您的传输功能中的client结构为NULL什么都没有初始化,所以要去哪里?

你可以做这样的事情

rc=recvfrom(acceptSocket,buf, sizeof(buf), 0, (struct sockaddr*) &client, &len);

sendto(acceptSocket,buf,rc,0,(struct sockaddr *)&client,sizeof(client));

或按如下所示将client结构传递给您的传输函数

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM