簡體   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