数据包在Linux中实时接收但在Windows中速度较慢(UDP协议)

时间:2016-06-16 03:28:49

标签: c# c sockets network-programming udp

我有一台服务器使用UDP协议向客户端发送计数器。服务器和客户端的代码片段在下面..

我打印我在服务器端发送的计数,并打印我在客户端收到的计数

当服务器是Linux而客户端是Linux时,我会毫不拖延地实时获取数据(计数)。然而,当我使用Windows机器测试客户端时,客户端的计数器远远落后于服务器端的计数器。

任何人都可以提供任何有关问题的指示

/////////
//SERVER.c (C Language)
//////// 
 portno = 11000;
  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sockfd < 0) 
    error("ERROR opening socket");
  optval = 1;
  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, 
         (const void *)&optval , sizeof(int));


//set server address
    bzero((char *) &serveraddr, sizeof(serveraddr));
     serveraddr.sin_family = AF_INET;
  inet_aton("192.168.139.248", &serveraddr.sin_addr);
  serveraddr.sin_port = htons((unsigned short)portno);

  //get data from client and store clientaddr
   bzero(buf, BUFSIZE);
    n = recvfrom(sockfd, buf, BUFSIZE, 0,
         (struct sockaddr *) &clientaddr, &clientlen);

 //keep sending a counter to client using &clientaddr in infinite loop
int ct = 0;
  while (1) {
    ct++;
    sprintf(buf,"%d",ct);
    n = sendto(sockfd, buf, strlen(buf), 0, 
           (struct sockaddr *) &clientaddr, clientlen);
    printf("server sent %s\n",  buf);
  }


/////////
//CLIENT.cs  (CSHARP)
//////// 

main()
{
    done = false;
    listener = new UdpClient(listenPort);
    IPAddress sender = IPAddress.Parse("192.168.139.248");
    groupEP = new IPEndPoint(sender,listenPort);
    robotCommThread = new Thread (receiveThread);
    if (!robotCommThread.IsAlive)
        robotCommThread.Start ();
}


void receiveThread(){
    int ct = 0;
    Debug.Log("Received broadcast Start");
    byte[] data = Encoding.UTF8.GetBytes(ct.ToString());
    listener.Send(data,data.Length, groupEP);
//listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 100);
    while(true){

        byte[] bytes = listener.Receive( ref groupEP);
        Debug.Log("Received broadcast" +System.Text.Encoding.UTF8.GetString(bytes));
    }
}

0 个答案:

没有答案