Windows和Linux之间的套接字性能

时间:2013-10-25 04:10:19

标签: linux windows performance sockets tcp

我写了一个关于windows和linux之间通信的演示。

千兆网络环境下的性能仅 40MB / s

有没有办法升级性能?我不知道为什么这么慢。


Windows中的代码作为客户端

#include "stdafx.h"


#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>


// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")


#define DEFAULT_BUFLEN 65536
#define DEFAULT_PORT "27015"

int tcpnodelay(int sock) {
    int yes=1;
    return setsockopt(sock,IPPROTO_TCP,TCP_NODELAY,(char*)&yes,sizeof(int));
}

int __cdecl main(int argc, char **argv) 
{
    WSADATA wsaData;
    SOCKET ConnectSocket = INVALID_SOCKET;
    struct addrinfo *result = NULL,
                    *ptr = NULL,
                    hints;
    char sendbuf[DEFAULT_BUFLEN];
    char recvbuf[32];
    unsigned long no;
    unsigned long iResult;
    unsigned long sent;
    int recvbuflen = 32;
    int recv_len, n;
    unsigned long recv_no;

    // Validate the parameters
    if (argc != 2) {
        printf("usage: %s server-name\n", argv[0]);
        return 1;
    }

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed with error: %d\n", iResult);
        return 1;
    }

    ZeroMemory( &hints, sizeof(hints) );
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    // Resolve the server address and port
    iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
    if ( iResult != 0 ) {
        printf("getaddrinfo failed with error: %d\n", iResult);
        WSACleanup();
        return 1;
    }

    // Attempt to connect to an address until one succeeds
    for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {

        // Create a SOCKET for connecting to server
        ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, 
            ptr->ai_protocol);
        if (ConnectSocket == INVALID_SOCKET) {
            printf("socket failed with error: %ld\n", WSAGetLastError());
            WSACleanup();
            return 1;
        }

        // Connect to server.
        iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
        if (iResult == SOCKET_ERROR) {
            closesocket(ConnectSocket);
            ConnectSocket = INVALID_SOCKET;
            continue;
        }
        break;
    }

    freeaddrinfo(result);

    if (ConnectSocket == INVALID_SOCKET) {
        printf("Unable to connect to server!\n");
        WSACleanup();
        return 1;
    }

    if (tcpnodelay(ConnectSocket)<0) {
        printf("Can't set TCP_NODELAY!\n");
    }

    recv_len = sizeof(no);
    printf("recv_len: %d\n", recv_len);

    // Send an initial buffer
    memset(sendbuf, 0, DEFAULT_BUFLEN);
    for (no=0; no<1024*1024; no++) {
        sent = 0;
        while (sent < DEFAULT_BUFLEN) {
            iResult = send(ConnectSocket, sendbuf+sent, DEFAULT_BUFLEN-sent, 0);

            if (iResult == SOCKET_ERROR) {
                printf("send failed with error: %d\n", WSAGetLastError());
                closesocket(ConnectSocket);
                WSACleanup();
                return 1;
            }

            sent += iResult;
        }


        n = recv(ConnectSocket, (char *)(&recv_no), recv_len, 0);
        if (recv_len!=n || recv_no!=no) {
            printf("recv len: %d, recv no %ld", n, recv_no);
            exit(1);
        }

    }


    printf("Bytes Sent: %ld\n", iResult);

    // shutdown the connection since no more data will be sent
    iResult = shutdown(ConnectSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf("shutdown failed with error: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    // cleanup
    closesocket(ConnectSocket);
    WSACleanup();

    return 0;
}

linux中的代码作为服务器

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>

int main( int argc, char *argv[] )
{
  int sockfd, newsockfd, portno, clilen;
  char buffer[65536];
  unsigned int no;
  unsigned long read_size;
  struct sockaddr_in serv_addr, cli_addr;
  int  n;
  int send_len;

  /* First call to socket() function */
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) 
  {
    perror("ERROR opening socket");
    exit(1);
  }
  /* Initialize socket structure */
  bzero((char *) &serv_addr, sizeof(serv_addr));
  portno = 27015;
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(portno);

  /* Now bind the host address using bind() call.*/
  if (bind(sockfd, (struct sockaddr *) &serv_addr,
           sizeof(serv_addr)) < 0)
  {
    perror("ERROR on binding");
    exit(1);
  }

  /* Now start listening for the clients, here process will
   * go in sleep mode and will wait for the incoming connection
   */
  listen(sockfd,5);
  clilen = sizeof(cli_addr);

  /* Accept actual connection from the client */
  newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, 
                     &clilen);
  if (newsockfd < 0) 
  {
    perror("ERROR on accept");
    exit(1);
  }

  send_len = sizeof(no);
  printf("send_len: %d\n", send_len);

  /* If connection is established then start communicating */
  bzero(buffer,65536);
  for (no=0; no<1024*1024; no++) {
    read_size = 0;
    while (read_size < 65536) {
      n = read( newsockfd,buffer+read_size,65536-read_size );
      if (n < 0)
      {
        perror("ERROR reading from socket");
        exit(1);
      }
      read_size += n;
    }


    n = write(newsockfd, &no, send_len);
    if (n < send_len) {
      printf("send len: %d\n", n);
      exit(1);
    }
  }
  printf("the message size: %d\n", n);

  /* Write a response to the client */
  n = write(newsockfd,"I got your message",18);
  if (n < 0)
  {
    perror("ERROR writing to socket");
    exit(1);
  }

  close(newsockfd);

  return 0; 
}

2 个答案:

答案 0 :(得分:2)

您自己回答了问题:

  

是的,如果我发送整个缓冲区,性能将达到90MB / s。但我的工作需要发送每个64KB的数据并接收反馈,然后发送下一个64KB。

您可以在千兆位以太网上每秒发送大约81,200个全尺寸帧,大约120MiB / s(包括TCP和IP的标头,因此有效地可以预期的更少)。
对于几个“可调”参数(MTU,窗口大小),TCP将以次优默认值(对于您的千兆位LAN而言是次优的,但对于“一般”未知网络是安全的)开始,并将自适应地调整这些值。这很快发生,但不是立即发生的。因此,在一个批量发送所有数据时看到的有效90MiB / s是绝对现实的。

现在的问题是,这是你可以实现的实际(或多或少理论)最大值,如果你总是保持线路繁忙

等待服务器回复恰恰相反,它让电线在两者之间“闲置”(至于您发送的数据)至少直到收到并确认回复(其中,特别是Windows可能需要非零时间,默认情况下,ACK仅每200毫秒发送一次,或者至少有2个时间排队 - 谷歌TcpDelAckTicks获取更多信息)。 虽然这个“空闲时间”相当短,但这对您可以实现的最大吞吐量产生了非常严重的影响。它相当于在其他领域中所谓的“管道停滞”。

如果您的协议允许,请尝试重叠您的发送和回复。也就是说,发送至少两个(更好的是三个)65kiB块,然后才从服务器接收答案。这样,TCP堆栈始终可以发送数据以保持线路忙碌并利用可用带宽。

答案 1 :(得分:0)

也许尽可能长时间发送我的数据。

它可以帮助减少数据包,并提高速度。