转自:http://bbs.csdn.net/topics/360111289, 有改动。
#include <windows.h>#include <stdio.h>#include <process.h>#include<memory>#pragma comment(lib,"ws2_32.lib")
ULONGLONG g_nReadCounts=0,g_nWriteCounts=0,g_nOtherCounts=0,g_nReadsBytes=0,g_nWriteBytes=0,g_nOtherBytes=0;void PrintIODetails()
{static DWORD lastTickCount;
static IO_COUNTERS lastIoCounters;
DWORD tickCount;IO_COUNTERS ioCounters;static int firstTime = 1;tickCount = GetTickCount();if (!GetProcessIoCounters(GetCurrentProcess(), &ioCounters)) {
return;
}if (firstTime)
{firstTime = 0;printf(" Time Reads Writes Others Read(Bytes) Write(Bytes) Other(Bytes)\n");
printf("--------------------------------------------------------------------------------\n");
}else
{printf("%8lu%8llu%8llu%8llu%16llu%16llu%16llu\n",
tickCount - lastTickCount,ioCounters.ReadOperationCount - lastIoCounters.ReadOperationCount,ioCounters.WriteOperationCount - lastIoCounters.WriteOperationCount,ioCounters.OtherOperationCount - lastIoCounters.OtherOperationCount,ioCounters.ReadTransferCount - lastIoCounters.ReadTransferCount,ioCounters.WriteTransferCount - lastIoCounters.WriteTransferCount,ioCounters.OtherTransferCount - lastIoCounters.OtherTransferCount);g_nReadCounts += (ioCounters.ReadOperationCount - lastIoCounters.ReadOperationCount);g_nWriteCounts += (ioCounters.WriteOperationCount - lastIoCounters.WriteOperationCount);g_nOtherCounts += (ioCounters.OtherOperationCount - lastIoCounters.OtherOperationCount);g_nReadsBytes += (ioCounters.ReadTransferCount - lastIoCounters.ReadTransferCount);g_nWriteBytes += (ioCounters.WriteTransferCount - lastIoCounters.WriteTransferCount);g_nOtherBytes += (ioCounters.OtherTransferCount - lastIoCounters.OtherTransferCount);}lastTickCount = tickCount;memcpy(&lastIoCounters, &ioCounters, sizeof(IO_COUNTERS));
}#define PACKCOUNT 10#define BUFFSIEZ 50000volatile bool bWorkThreadFinish = false;void workerThread(void * dummy){int i;
FILE * fp;char buff[BUFFSIEZ];
WSADATA wsaData;SOCKET s;struct sockaddr_in localAddr;
struct sockaddr_in peerAddr;
memset(buff, ‘X‘, sizeof(buff));
Sleep(2000);// 写文件
fp = fopen("io.txt", "w");if (fp) {
printf("fwrite = %d\n", fwrite(buff, 1, sizeof(buff), fp));Sleep(10000);// 关闭文件,测试Flush
printf("fclose\n");
fclose(fp);Sleep(2000);}// 读文件
fp = fopen("io.txt", "r");if (fp) {
printf("fread = %d\n", fread(buff, 1, sizeof(buff), fp));fclose(fp);Sleep(2000);}// 网络发
memset((void *)&localAddr, 0, sizeof(localAddr));localAddr.sin_family = AF_INET;localAddr.sin_addr.s_addr = htonl(INADDR_ANY);localAddr.sin_port = htons(0);memset((void *)&peerAddr, 0, sizeof(peerAddr));peerAddr.sin_family = AF_INET;peerAddr.sin_addr.s_addr = inet_addr("10.19.85.1");
peerAddr.sin_port = htons(3389);printf("WSAStartup\n");
WSAStartup(MAKEWORD(2, 2), &wsaData);Sleep(2000);// UDP
printf("UDP Test:\n");
printf("socket\n");
s = socket(AF_INET, SOCK_DGRAM, 0);Sleep(2000);printf("bind\n");
bind(s, (struct sockaddr *)&localAddr, sizeof(localAddr));Sleep(2000);for (i = 0; i < PACKCOUNT; i++) {
printf("sendto = %d\n", sendto(s, buff, i * 100, 0, (SOCKADDR *) &peerAddr, sizeof(peerAddr)));Sleep(2000);}printf("closesocket\n");
closesocket(s);Sleep(2000);// TCP
printf("TCP Test:\n");
printf("socket\n");
s = socket(AF_INET, SOCK_STREAM, 0);Sleep(2000);printf("bind\n");
bind(s, (struct sockaddr *)&localAddr, sizeof(localAddr));Sleep(2000);printf("connect\n");
connect(s, (SOCKADDR *) &peerAddr, sizeof(peerAddr));
Sleep(2000);printf("send = %d\n", send(s, buff, sizeof(buff), 0));Sleep(2000);printf("closesocket\n");
closesocket(s);Sleep(2000);printf("WSACleanup\n");
WSACleanup();bWorkThreadFinish = true;
}UINT _stdcall ThreadProc(PVOID pv){workerThread(pv);return 1;
}//HANDLE g_hEvt = NULL;
int main(int argc, char ** argv){// 启动工作线程
::_beginthreadex(NULL,0,ThreadProc,NULL,0,0);//g_hEvt = ::CreateEventW(NULL,FALSE,FALSE,NULL);
printf("Ctrl-C to exit\n\n");
for (;;)
{PrintIODetails();Sleep(1000);if (bWorkThreadFinish)
{break;
}}printf("*********************************Total*************************************\n");
printf(" Reads Writes Others Read(Bytes) Write(Bytes) Other(Bytes)\n");
printf("%8llu%8llu%8llu%16llu%16llu%16llu\n",
g_nReadCounts,g_nWriteCounts,g_nOtherCounts,g_nReadsBytes,g_nWriteBytes,g_nOtherBytes);return 0;
}