返回   cpper编程论坛 > Blog > 永远的Rockets
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

为这篇文章评分

急问!udp传输速率

发表于 2008-08-14 12:58 PM 作者: 永远的Rockets
wywajlj 794 113 2 365

发信人: wywajlj(五台兄), 信区: NetPRG
标 题: 急问!!udp传输速率
发信站: 瀚海星云 (2008年08月14日10:19:43 星期四), 站内信件 WWWPOST

鄙人在100Mb/s的局域以太网测试时得到tcp的传输速率为:89Mb/s,而udp只有88Mb/s.
按理说这是不正常的,udp的传输速率应该高于tcp,请哪位大虾指教一下。代码如下

代码功能:从客户方向服务方传送数据,测试局域以太网路的传输速率

客户方:
代码:
#include <stdio.h>
#include "winsock2.h"
#pragma  comment(lib,"Ws2_32.lib")

#define  RECV_IP    "192.168.168.13"
#define  BLOCK       65504
#define  NUM         10000*BLOCK

void main() {
  
  WSADATA wsaData;
  SOCKET SendSocket;
  sockaddr_in RecvAddr;
  int Port = 27015;
  char SendBuf[BLOCK];
  int BufLen = BLOCK;

  //---------------------------------------------
  // Initialize Winsock
  WSAStartup(MAKEWORD(2,2), &wsaData);

  //---------------------------------------------
  // Create a socket for sending data
  SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

  //---------------------------------------------
  // Set up the RecvAddr structure with the IP address of
  // the receiver (in this example case "192.168.168.13")
  // and the specified port number.
  RecvAddr.sin_family = AF_INET;
  RecvAddr.sin_port = htons(Port);
  RecvAddr.sin_addr.s_addr = inet_addr(RECV_IP);
  
  //---------------------------------------------
  // Set up the buffer of SendSocket
  int nSendBuf=512*1024;//设置为32K
  int nret =setsockopt(SendSocket,SOL_SOCKET,SO_SNDBUF,(const char*)
&nSendBuf,sizeof(int));
  if (nret ==SOCKET_ERROR){
      printf("Set Buffer with error:%d\n",GetLastError());
      return;
  }
  //---------------------------------------------
  // Send a datagram to the receiver
  printf("Sending a datagram to the receiver...\n");
  int num =NUM,sendbytes;
  
  // Get the time beginning to send a datagram
  SYSTEMTIME systime;
  GetLocalTime(&systime);  //Get local time 
  printf("发送时间是:\n");
  printf("systime.wHour:%d\nsystime.wMinute:%d\nsystime.wSecond:%
d\nsystime.wMilliseconds:%d\n",
             
systime.wHour,systime.wMinute,systime.wSecond,systime.wMilliseconds);
        
  float t1 =(float)(3600*systime.wHour +60*systime.wMinute +systime.wSecond 
+0.001*systime.wMilliseconds);
  while (num >=0){
      sendbytes =sendto(SendSocket, SendBuf, BufLen, 0, 
      (SOCKADDR *) &RecvAddr, sizeof(RecvAddr));
      if (sendbytes <0){
          printf("send data with error:%d\n",GetLastError());
      }
      num =num -sendbytes;
  }

  //---------------------------------------------
  // When the application is finished sending, close the socket.
  printf("Finished sending. Closing socket.\n");

  // Get the time when sending is over
  GetLocalTime(&systime);       //local time 
  printf("接收时间:\n");
  printf("systime.wHour:%d\nsystime.wMinute:%d\nsystime.wSecond:%
d\nsystime.wMilliseconds:%d\n",
             
systime.wHour,systime.wMinute,systime.wSecond,systime.wMilliseconds);


  float t2 =(float)(3600*systime.wHour +60*systime.wMinute +systime.wSecond 
+0.001*systime.wMilliseconds);
  float t =t2-t1;
  float v = (float)((float)(NUM*8.00)/(1024.00*1024.00*t));
  printf("Time spended in sending datagram:%fseconds\n",t);
  printf("传输速率:%fMb/s\n",v);
  closesocket(SendSocket);

  //---------------------------------------------
  // Clean up and quit.
  printf("Exiting.\n");
  WSACleanup();
  return;
}
服务方代码:
代码:
#include <stdio.h>
#include "winsock2.h"
#pragma  comment(lib, "Ws2_32.lib")

#define BLOCK  65504
#define NUM    10000*BLOCK

void main() {

  WSADATA wsaData;
  SOCKET RecvSocket;
  sockaddr_in RecvAddr;
  int Port = 27015;
  char RecvBuf[BLOCK];
  int  BufLen = BLOCK;
  sockaddr_in SenderAddr;
  int SenderAddrSize = sizeof(SenderAddr);
  int RecvAddrSize = sizeof(RecvAddr);

  //-----------------------------------------------
  // Initialize Winsock
  WSAStartup(MAKEWORD(2,2), &wsaData);

  //-----------------------------------------------
  // Create a receiver socket to receive datagrams
  RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

  //-----------------------------------------------
  // Bind the socket to any address and the specified port.
  RecvAddr.sin_family = AF_INET;
  RecvAddr.sin_port = htons(Port);
  RecvAddr.sin_addr.s_addr = inet_addr("192.168.168.13");

  bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr));


  // Initialize the SendAddr struct
 
  SenderAddr.sin_family = AF_INET;
  SenderAddr.sin_port = htons(Port);
  SenderAddr.sin_addr.s_addr = inet_addr("192.168.168.44");

  

  //-----------------------------------------------
  //设置接受缓冲区
  int nRecvBuf =512*1024 ;   //设置缓冲区
  int nret =setsockopt(RecvSocket, SOL_SOCKET, SO_RCVBUF,(const char *)
&nRecvBuf, sizeof (int));
  if (nret ==SOCKET_ERROR){
      printf("set receive buffer with error:%d\n",GetLastError());
  }

  //-----------------------------------------------
  // Call the recvfrom function to receive datagrams
  // on the bound socket.
  printf("Receiving datagrams...\n");
  int num =NUM;
  int recvbytes;
  while (num >=0){
      recvbytes =recvfrom(RecvSocket, RecvBuf, BufLen, 0, 
      (SOCKADDR *)&SenderAddr, &SenderAddrSize);
      if (recvbytes <0){
          printf("receive data with error:%d\n",GetLastError());
      }
      //printf("%d\n",recvbytes);
      num =num -recvbytes;
  }

  //-----------------------------------------------
  // Close the socket when finished receiving datagrams
  printf("Finished receiving. Closing socket.\n");
  closesocket(RecvSocket);

  //-----------------------------------------------
  // Clean up and exit.
  printf("Exiting.\n");
  WSACleanup();
  return;
}
评论 1 Email文章
评论总数 1

评论

旧
polyrandom 的头像
你没有按照UDP最佳尺寸发送,这样的话,即使使用UDP,额外开销还是挺大的。
而TCP的话,虽然你没有按照最佳尺寸发送,它内部帮你流了一下。
发表于 2008-08-14 02:05 PM 作者: polyrandom polyrandom 当前离线
发表评论 发表评论
作者为 永远的Rockets 的最新文章

所有时间均为格林尼治时间 +9。现在的时间是 08:23 PM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2008,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0