Table Tennis Performance

I wrote two simple programs (tried in C ++ and C #).

This is the pseudo code:

-------- Customer ---------------

for(int i = 0; i < 200.000; i++)  
{  
    socket_send("ping")  
    socket_receive(buff)  
}  

--------- Server -------------

while(1)  
{  
    socket_receive(buff)  
    socket_send("pong")  
}  

I tried this on windows.

Client runtime is about 45 seconds. Can someone explain to me why it takes so long? I understand that if there was a real network connection between the client and the server, then the time of one ping-pong would be: generate_ping + send_via_network + generate_pong + send_via_network but here everything is done in local mode.

Is there a way to make this interprocessor ping pong faster using network sockets (for example, I am not asking about shared memory :))

+3
source share
3

? . .

, /, , . , , , . , . . ( , , , !)

, . .

( , "200 000", " ", "200 000", ", ". , . , .)

, ( ) ? , , - (, 1 , 4 ) , .

+1

? , . TCP , , . TCP_NODELAY .

, linux ( ). , TCP Windows 9 , linux - - .

+1

#:

using System;
using System.Net;
using System.Net.Sockets;


namespace Producer
{    
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Usage:");
                Console.WriteLine("Producer.exe <server_ip> <port>");
                return;
            }
            string ipAddress = args[0];
            string port = args[1];

            Console.WriteLine("IpAddress: " + ipAddress);
            Console.WriteLine("Port     : " + port);

            Socket s = new Socket(AddressFamily.InterNetwork, 
                           SocketType.Stream, ProtocolType.Tcp);
            s.Connect(new IPEndPoint(IPAddress.Parse(ipAddress), 
                           int.Parse(port)));

            Console.WriteLine("Press key to start pinging");
            Console.ReadKey();

            byte[] buff = new byte[1];
            Console.WriteLine("Pinging started");
            DateTime start = DateTime.Now;

            for (int i = 0; i < 200000; i++)
            {
                s.Send(buff);
                s.Receive(buff, 1, SocketFlags.None);
            }
            DateTime end = DateTime.Now;
            Console.WriteLine("Pinging finished in: " 
                               + (end - start).TotalSeconds);

            Console.ReadKey();
        }
    }
}

#:

using System;
using System.Net;
using System.Net.Sockets;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage:");
                Console.WriteLine("Server.exe <port>");
                return;
            }

            string port = args[0];

            //IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            //IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
            IPEndPoint m_LocalEndPoint = new IPEndPoint(ipAddress, 
                                                        int.Parse(port));

            try
            {
                // Create a TCP/IP socket.
                Socket s = new Socket(AddressFamily.InterNetwork, 
                               SocketType.Stream, ProtocolType.Tcp);
                s.Bind(m_LocalEndPoint);
                s.Listen(10);

                Console.WriteLine("IpAddress: " + ipAddress.ToString());
                Console.WriteLine("Port     : " + port);

                byte[] buff = new byte[1];

                Socket s2 = s.Accept();
                Console.WriteLine("connected");

                while (true)
                {
                    s2.Receive(buff, 1, SocketFlags.None);
                    s2.Send(buff);
                }
            }
            catch( Exception )
            {
            }
        }
    }
}

( Windows):

Producer.exe 127.0.0.1 667
IpAddress: 127.0.0.1
Port     : 667
Press key to start pinging
nPinging started
Pinging finished in: 46,617032

, ++, winsock2 ( Linux- )

C Linux:

:

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

int main(int argc, char** argv)
{
  int sockfd, portno, n;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  char buff[1];
  time_t start, end;
  int i;

  if (argc != 3)
  {
  printf("Usage:\n");
  printf("%s <server_ip> <port>\n", argv[0]);
  return;
  }

  char* ipAddress = argv[1];
  char* port = argv[2];

  printf("IpAddress: %s\n", ipAddress);
  printf("Port     : %s\n", port);

  portno = atoi(port);
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) 
  printf("error - socket open\n");

  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = inet_addr(ipAddress);
  serv_addr.sin_port = htons(portno);
  if(0 > connect(sockfd,&serv_addr,sizeof(serv_addr)))
printf("error - connect\n");

  start = time(0);
  printf("ping-pong started\n");
  for(i = 0; i < 200000; i++)
  {
write(sockfd,buff,1);
read(sockfd,buff,1);
  }
  end = time(0);
  printf("finished in %d secs\n", end - start);

  return 0;
}

:   #   #   #   #   #include

int main(int argc, char** argv)
{
  int sockfd, newsockfd, portno, clilen;
  char buffer[1];
  struct sockaddr_in serv_addr, cli_addr;
  int n;

  if (argc < 2)
  {
printf("Usage:\n");
printf("%s <port>\n", argv[0]);
return;
  }

  portno = atoi(argv[1]);
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) 
  printf("error - socket open\n");

  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(portno);


  if (0 > bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))) 
printf("error - bind\n");

  listen(sockfd,5);
  clilen = sizeof(cli_addr);
  printf("waiting for connection...\n");
  newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  if (newsockfd < 0) 
printf("error - accept\n");

  printf("connected\n");

  while(1)
  {
read(newsockfd,buffer,1);
write(newsockfd,buffer,1);
  }  
  return 0;
}

Output from the manufacturer (Linux):

$ ./a.out 127.0.0.1 666
IpAddress: 127.0.0.1
Port     : 666
ping-pong started
finished in 5 secs

My car is an Intel Core Duo

What is going on in Windows?

0
source

Source: https://habr.com/ru/post/1745459/


All Articles