18,361
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include <stdio.h>
#include <process.h>
void printIoDelta() {
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 Read Write Other 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
);
}
lastTickCount = tickCount;
memcpy(&lastIoCounters, &ioCounters, sizeof(IO_COUNTERS));
}
void workerThread(void * dummy) {
int i;
FILE * fp;
char buff[500000];
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.65.19.7");
peerAddr.sin_port = htons(3389);
printf("WSAStartup\n");
WSAStartup(MAKEWORD(2, 2), &wsaData);
Sleep(2000);
// UDP
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 < 20; 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("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();
}
int main(int argc, char ** argv) {
// 启动工作线程
_beginthread(workerThread, 0, 0);
printf("Ctrl-C to exit\n\n");
for (;;) {
printIoDelta();
Sleep(1000);
}
return 0;
}
Ctrl-C to exit
Time Read Write Other Read(Bytes) Write(Bytes) Other(Bytes)
--------------------------------------------------------------------------------
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
fwrite = 500000
1000 0 98 1 0 499712 0
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
fclose
1000 0 1 1 0 288 0
1000 0 0 0 0 0 0
fread = 500000
1000 2 0 2 500000 0 0
1000 0 0 0 0 0 0
WSAStartup
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
socket
1000 0 0 37 0 0 0
1000 0 0 0 0 0 0
bind
1000 1 1 22 68 72 4270
1000 0 0 0 0 0 0
sendto = 0
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
sendto = 100
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
sendto = 200
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
sendto = 300
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
sendto = 400
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
sendto = 500
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
sendto = 600
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
sendto = 700
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
sendto = 800
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
sendto = 900
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
sendto = 1000
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
sendto = 1100
1000 0 0 1 0 0 1100
1000 0 0 0 0 0 0
sendto = 1200
1000 0 0 1 0 0 1200
1000 0 0 0 0 0 0
sendto = 1300
1000 0 0 1 0 0 1300
1000 0 0 0 0 0 0
sendto = 1400
1000 0 0 1 0 0 1400
1000 0 0 0 0 0 0
sendto = 1500
1000 0 0 1 0 0 1500
1000 0 0 0 0 0 0
sendto = 1600
1000 0 0 1 0 0 1600
1000 0 0 0 0 0 0
sendto = 1700
1000 0 0 1 0 0 1700
1000 0 0 0 0 0 0
sendto = 1800
1000 0 0 1 0 0 1800
1000 0 0 0 0 0 0
sendto = 1900
1000 0 0 1 0 0 1900
1000 0 0 0 0 0 0
closesocket
1000 0 0 1 0 0 0
1000 0 0 0 0 0 0
socket
1000 0 0 1 0 0 0
1000 0 0 0 0 0 0
bind
1000 0 0 2 0 0 1944
1000 0 0 0 0 0 0
connect
1000 0 0 2 0 0 0
1000 0 0 0 0 0 0
send = 500000
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
closesocket
1000 0 0 1 0 0 0
1000 0 0 0 0 0 0
WSACleanup
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
1000 0 0 0 0 0 0
^C