Here rough code of producer in C#:
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();
}
}
}
And here is server in C#:
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 )
{
}
}
}
}
And here is output from producer (run on 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
I've got very similar results for program written in C++ using winsock2 (code similar to Linux sockets below)
The same written in C and started on Linux:
Producer:
#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;
}
And Server:
#include
#include
#include
#include
#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 Producer (Linux):
$ ./a.out 127.0.0.1 666
IpAddress: 127.0.0.1
Port : 666
ping-pong started
finished in 5 secs
My machine is Intel Core Duo
What is going on on Windows??