1.服务端
#include <stdio.h>
#include <WinSock2.h>
#include <stdlib.h>
#pragma comment(lib, "ws2_32.lib")
LARGE_INTEGER g_Start; //定时器的起始值
LARGE_INTEGER g_End; //定时器的结束值
LARGE_INTEGER g_Freq; //定时器的频率
int main(int argc, char *argv[])
{
//初始化WSA
WORD sockVersion = MAKEWORD(2, 2);
WSADATA wsaData;
if (WSAStartup(sockVersion, &wsaData) != 0)
{
return 0;
}
//创建套接字
SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (slisten == INVALID_SOCKET)
{
printf("socket error !");
return 0;
}
//绑定IP和端口
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(8686);
sin.sin_addr.S_un.S_addr = INADDR_ANY;
if (bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
{
// int n = GetLastError();
printf("bind error!");
return 0;
}
//开始监听
if (listen(slisten, 5) == SOCKET_ERROR)
{
printf("listen error!");
return 0;
}
//循环接收数据
SOCKET sClient;
sockaddr_in remoteAddr;
int nAddrlen = sizeof(remoteAddr);
QueryPerformanceFrequency(&g_Freq);
while (true)
{
printf("等待连接...\n");
sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
if (sClient == INVALID_SOCKET)
{
printf("accept error!");
continue;
}
printf("接收到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));
int count = 0;
double dTime[10], dTotalTime = 0;
char *sendData = (char *)malloc(sizeof(char)*1048576 * 10);
printf("分配内存成功!\n");
while (count != 10)
{
QueryPerformanceCounter(&g_Start);
send(sClient, sendData, strlen(sendData), 0);
QueryPerformanceCounter(&g_End);
count++;
dTime[count] = (double)(g_End.QuadPart - g_Start.QuadPart) / (double)g_Freq.QuadPart;
}
delete []sendData;
sendData = NULL;
printf("发送使用时间 :\n");
while (count != 0)
{
printf("第%d次发送所需时间为: %.10f seconds\n", count, dTime[count]);
dTotalTime += dTime[count];
count--;
}
printf("总时间: %.10f seconds\n", dTotalTime);
closesocket(sClient);
}
closesocket(slisten);
WSACleanup();
return 1;
}
2.客户端
#include <WinSock2.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
#define IP "192.168.0.171"
//#define IP "192.168.242.135"
LARGE_INTEGER g_Start; //定时器的起始值
LARGE_INTEGER g_End; //定时器的结束值
LARGE_INTEGER g_Freq; //定时器的频率
int main(int argc, char *argv[])
{
WORD sockVersion = MAKEWORD(2, 2);
WSADATA data;
if (WSAStartup(sockVersion, &data) != 0)
{
return 0;
}
SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sclient == INVALID_SOCKET)
{
printf("invalid socket !");
return 0;
}
sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
serAddr.sin_port = htons(8686);
serAddr.sin_addr.S_un.S_addr = inet_addr(IP);
if (connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
{
int a = GetLastError();
printf("connect error!");
closesocket(sclient);
return 0;
}
char *recData = (char *)malloc(sizeof(char)*1048576 * 10);
int ret = 0;
int nSum = 0;
int i = 1;
int j = 1;
QueryPerformanceFrequency(&g_Freq);
QueryPerformanceCounter(&g_Start);
while((ret = recv(sclient, recData, 10485760, 0)) > 0)
{
printf("接收长度:%d\n", ret);
LARGE_INTEGER g_End_10;
QueryPerformanceCounter(&g_End_10);
nSum += ret;
if (nSum >= 1024*1024*10*j)
{
printf("\n\n------------\n\n");
printf("接收第%d个10M使用时间 :", j);
printf(" %.10f seconds", (double)(g_End_10.QuadPart - g_Start.QuadPart) / (double)g_Freq.QuadPart);
printf("\n\n------------\n\n");
j++;
}
i++;
}
QueryPerformanceCounter(&g_End);
printf("接收使用时间 :");
printf(" %.10f seconds", (double)(g_End.QuadPart - g_Start.QuadPart) / (double)g_Freq.QuadPart);
getchar();
free(recData);
closesocket(sclient);
WSACleanup();
return 1;
}