1 #include <sys/socket.h>
2 #include <netinet/in.h>
10 #include <sys/types.h>
13 int main(
int argc,
char *argv[])
15 int listenfd = 0, connfd = 0;
16 struct sockaddr_in serv_addr;
18 char* recvBuff = malloc(512*1024);
19 if (!recvBuff)
return 1;
22 int64_t totalsz, totalbuf, totallost, lastcnt;
28 listenfd = socket(AF_INET, SOCK_STREAM, 0);
29 memset(&serv_addr,
'0',
sizeof(serv_addr));
31 serv_addr.sin_family = AF_INET;
32 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
33 serv_addr.sin_port = htons(5000);
35 bind(listenfd, (
struct sockaddr*)&serv_addr,
sizeof(serv_addr));
40 totalsz = 0; totalbuf = 0; totallost = 0;
46 connfd = accept(listenfd, (
struct sockaddr*)NULL, NULL);
49 printf(
"Fail to accept connection\n");
55 printf(
"Client connected\n");
58 sz = recv(connfd, header,
sizeof(header), MSG_WAITALL);
59 if (sz == 0)
continue;
60 if (sz !=
sizeof(header)) {
61 printf(
"Error when receive header %lu res %ld\n", (
long unsigned)
sizeof(header), (
long) sz);
67 sz = recv(connfd, recvBuff, header[1], MSG_WAITALL);
68 if (sz != header[1]) {
69 printf(
"Error when receive buffer %ld res %ld\n", (
long) header[1], (
long) sz);
79 if ((lastcnt>0) && (header[0]-lastcnt>1)) totallost+=(header[0]-lastcnt-1);
84 if (tm - lasttm > 3) {
85 printf(
"Bufs %6lld Size %8.3f MB Lost %3lld\n", (
long long) totalbuf, totalsz*1e-6, (
long long) totallost);
int main(int argc, char *argv[])