📄 h_tcp_srv3.c
字号:
/* * Copyright (C) 1999-2006 MITSUBISHI ELECTRIC CORPORATION and * RENESAS SOLUTIONS CORPORATION and * RENESAS TECHNOLOGY CORPORATION * All rights reserved. * */#include <stdio.h>#include <signal.h>#include <ctype.h>#include <errno.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netinet/tcp.h>#include <netdb.h>#include "config.h"extern int errno;int sd, newsd;void sig_hdr(){ /* printf("received SIGINT.\n"); */ shutdown(sd, SHUT_RDWR); shutdown(newsd, SHUT_RDWR); close(sd); close(newsd); sleep(2); exit(0);}#define SIZE (64*1024)static int comm_size[] = { 1, 2, 4, 10, 16, 30, 32, 50, 64, 100, 128, 200, 256, 500, 512, 1000, 1024, 2000, 2048, 4000, 4096, 8000, 8192, 16000, 16384, 32000, 32768, 64000, 65536};/* * TCP server program */main(argc, argv)int argc;char **argv;{ int i, j, k, len; char ch, *buf, *buf2; struct sockaddr_in sinme, sindst; int size, total, addrlen; int size2, testno, err_flag; size = SIZE; ch = 'a'; errno = 0; buf = (char *)malloc(size); if (buf == NULL) { printf("malloc is NG.\n"); exit(1); } buf2 = (char *)malloc(size); if (buf2 == NULL) { printf("malloc is NG.\n"); exit(1); }signal(SIGINT, sig_hdr); /* create an endpoint for TCP communication */ errno = 0; if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("socket is NG.(errno=%d)\n", errno); return (1); } printf("socket is OK.\n"); /* bind an address to the socket */ memset((char *)&sinme, 0, sizeof(struct sockaddr_in)); sinme.sin_family = AF_INET; sinme.sin_addr.s_addr = inet_addr(MY_ADDR); sinme.sin_port = htons(PORT_NUM2); if (bind(sd, (struct sockaddr *)&sinme, sizeof(struct sockaddr_in)) < 0) { printf("bind is NG.(errno=%d)\n", errno); close(sd); return (1); } printf("bind is OK.\n"); /* listen for connections on the socket */ listen(sd, 5);err_flag = 0;for (k=0; k < sizeof(comm_size)/sizeof(comm_size[0]); k++) { size2 = comm_size[k]; printf("Test-%d: size=%d\n", k+1, size2); for (j=1; j <= LOOP; j++) { errno = 0; /* accept a connection on the socket */ memset((char *)&sindst, 0, sizeof(struct sockaddr_in)); addrlen = sizeof(sindst); if ((newsd=accept(sd, (struct sockaddr *)&sindst, &addrlen)) < 0) { printf("(%d)accept is NG.(errno=%d)\n", j, errno); close(sd); err_flag = 1; break; } printf("(%d)accept is OK.\n", j); /* clear data contents */ memset(buf2, 0, size); /* receive data */ total = 0; while (total < size2) { if ((len = recv(newsd, buf2, size2, 0)) < 0) { printf("(%d)receiving TCP data is NG.(errno=%d)\n", j, errno); close(newsd); break; } if (len == 0) { break; } total += len; /* check data contesnts */ for (i=0; i < len; i++) { if (buf2[i] != ch) { break; } } if (i < len) { printf("(%d)checking TCP data contents is NG.\n", j); close(newsd); goto err; } } if (total == 0) { printf("(%d)receiving TCP data is OK.(total size=%d)\n", j, total); if (shutdown(newsd, SHUT_RDWR) < 0) { printf("(%d)shutdown is NG.(errno=%d)\n", j, errno); } printf("(%d)shutdown is OK.\n", j); if (close(newsd) < 0) { printf("(%d)close is NG.(errno=%d)\n", j, errno); } printf("(%d)close is OK.\n\n", j); break; } if (total != size2) { printf("(%d)receiving TCP data is NG.(total size=%d)\n", j, total); close(newsd); err_flag = 1; break; } else { printf("(%d)receiving TCP data is OK.(total size=%d)\n", j, total); } /* set data contents */ memset(buf, ch, size); /* send data */ if ((len = send(newsd, buf, size2, 0)) < 0) { printf("(%d)sending TCP data is NG.(errno=%d)\n", j, errno); close(newsd); err_flag = 1; break; } printf("(%d)sending TCP data is OK.(size=%d)\n", j, len); /* shut down sending data */ if (shutdown(newsd, SHUT_RDWR) < 0) { printf("(%d)shutdown is NG.(errno=%d)\n", j, errno); close(newsd); err_flag = 1; } printf("(%d)shutdown is OK.\n", j); if (close(newsd) < 0) { printf("(%d)close is NG.(errno=%d)\n", j, errno); } printf("(%d)close is OK.\n\n", j); } if (total == 0 || err_flag) { break; }}err: shutdown(sd, SHUT_RDWR); close(sd); free(buf); free(buf2); printf("Pass.\n"); sleep(2); return (0);}/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -