📄 h_tcp_clt2.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); close(sd); sleep(2); exit(0);}#define SIZE (16*1024)/* * TCP client 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; 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); memset((char *)&sindst, 0, sizeof(struct sockaddr_in)); sindst.sin_family = AF_INET; sindst.sin_addr.s_addr = inet_addr(DST_ADDR); sindst.sin_port = htons(PORT_NUM); /* set data contents */ memset(buf, ch, size); for (j=1; j <= LOOP; j++) { /* create an endpoint for TCP communication */ errno = 0; if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("(%d)socket is NG.(errno=%d)\n", j, errno); break; } printf("(%d)socket is OK.\n", j); /* initiate a connection on the socket */ if (connect(sd, (struct sockaddr *)&sindst, sizeof(sindst)) < 0) { printf("(%d)connect is NG.(errno=%d)\n", j, errno); close(sd); break; } printf("(%d)connect is OK.\n", j); /* send data */ if ((len = send(sd, buf, size, 0)) < 0) { printf("(%d)sending TCP data is NG.(errno=%d)\n", j, errno); close(sd); break; } printf("(%d)sending TCP data is OK.(size=%d)\n", j, len); /* clear data contents */ memset(buf2, 0, size); /* receive data */ total = 0; while (total < size) { if ((len = recv(sd, buf2, size, 0)) < 0) { printf("(%d)receiving TCP data is NG.(errno=%d)\n", j, errno); close(sd); 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(sd); goto err; } } if (total != size) { printf("(%d)receiving TCP data is NG.(total size=%d)\n", j, total); close(sd); break; } else { printf("(%d)receiving TCP data is OK.(total size=%d)\n", j, total); } /* shut down sending data */ if (shutdown(sd, SHUT_RDWR) < 0) { printf("(%d)shutdown is NG.(errno=%d)\n", j, errno); close(sd); break; } printf("(%d)shutdown is OK.\n", j); if (close(sd) < 0) { printf("(%d)close is NG.(errno=%d)\n", j, errno); break; } printf("(%d)close is OK.\n\n", j); /* sleep 1 second */ sleep(1); } errno = 0; if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("(%d)socket is NG.(errno=%d)\n", j, errno); goto err; } printf("(%d)socket is OK.\n", j); if (connect(sd, (struct sockaddr *)&sindst, sizeof(sindst)) < 0) { printf("(%d)connect is NG.(errno=%d)\n", j, errno); close(sd); goto err; } printf("(%d)connect is OK.\n", j); if ((len = send(sd, buf, 0, 0)) < 0) { printf("(%d)sending TCP data is NG.(errno=%d)\n", j, errno); close(sd); goto err; } printf("(%d)sending TCP data is OK.(size=%d)\n", j, len); if (shutdown(sd, SHUT_RDWR) < 0) { printf("(%d)shutdown is NG.(errno=%d)\n", j, errno); close(sd); goto err; } printf("(%d)shutdown is OK.\n", j); if (close(sd) < 0) { printf("(%d)close is NG.(errno=%d)\n", j, errno); } printf("(%d)close is OK.\n\n", j);err: free(buf); free(buf2); printf("Pass.\n"); sleep(2); return (0);}/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -