📄 h_tcp_clt12.c
字号:
/* * Copyright (C) 1999-2006 MITSUBISHI ELECTRIC CORPORATION and * RENESAS SOLUTIONS CORPORATION and * RENESAS TECHNOLOGY CORPORATION * All rights reserved. * * Test program for recv() and send() on LAN. */#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); exit(0);}/*#define SZ_SND_DAT 256#define SZ_SND_DAT 512#define SZ_SND_DAT 1024#define SZ_SND_DAT 2048*/#define SZ_SND_DAT 1024#define SZ_RCV_DAT (16*1024)#define SZ_HEADERS 110#define SZ_CONTENTS 13517/* * TCP client program */main(argc, argv)int argc;char **argv;{ int i, j, len; char ch, buf[SZ_SND_DAT], *buf2; struct sockaddr_in sinme, sindst; int addrlen; int total; signal(SIGINT, sig_hdr); errno = 0; ch = 'a'; /* get buffer space */ buf2 = (char *)malloc(SZ_RCV_DAT); if (buf2 == NULL) { printf("malloc failed.\n"); return (1); } /* clear data contents */ memset(buf, ch, SZ_SND_DAT); 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(8989); for (j=1; j <= LOOP; j++) { /* create an endpoint for TCP communication */ if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("(%d)socket is NG.\n", j); break; } printf("(%d)socket is OK.\n", j); /* connect 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, SZ_SND_DAT, 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); /* receive contents data */ total = 0; while (total < (SZ_CONTENTS + SZ_HEADERS)) {#ifdef TMP /* clear buffer */ memset(buf2, 0, SZ_RCV_DAT);#endif if ((len = recv(sd, buf2, SZ_RCV_DAT, 0)) < 0) { printf("(%d)receiving TCP data is NG.(errno=%d)\n", j, errno); close(sd); break; } if (len == 0) { printf("(%d)receiving TCP data is NG.(size=%d)\n", j, len); break; } total += len; printf("(%d)receiving TCP data is OK.(size=%d)\n", j, len);#ifdef TMP /* 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); break; } printf("(%d)checking TCP data contents is OK.\n", j);#endif } if (total == 0) { printf("(%d)receiving TCP data is OK.(size=%d)\n", j, len); if (close(sd) < 0) { printf("(%d)close is NG.(errno=%d)\n", j, errno); } printf("(%d)close is OK.\n\n", j); close(sd); break; } if (total != (SZ_CONTENTS + SZ_HEADERS)) { printf("(%d)receiving TCP data is NG.(total size=%d)\n", j, total); break; } printf("(%d)receiving TCP data is OK.(total size=%d)\n", j, total); 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); }#ifdef TMP /* create an endpoint for TCP communication */ if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("socket2 is NG.\n"); return (1); } printf("socket2 is OK.\n"); /* connect a connection on the socket */ if ((connect(sd, (struct sockaddr *)&sindst, sizeof(sindst))) < 0) { printf("connect2 is NG.(errno=%d)\n", errno); close(sd); return (1); } printf("connect2 is OK.\n"); /* send data */ if ((len = send(sd, buf, 0, 0)) < 0) { printf("sending TCP data2 is NG.(errno=%d)\n", errno); close(sd); return (1); } printf("sending TCP data2 is OK.(size=%d)\n", len); if (shutdown(sd, SHUT_RDWR) < 0) { printf("shutdown2 is NG.(errno=%d)\n", errno); close(sd); return (1); } printf("shutdown2 is OK.\n"); if (close(sd) < 0) { printf("close2 is NG.(errno=%d)\n", errno); return (1); } printf("close2 is OK.\n\n");#endif printf("Pass.\n"); sleep(2); return (0);}/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -