⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 h_tcp_clt1.c

📁 T-kernel Tcp/ip Protocol Stack Sample
💻 C
字号:
/* *  Copyright (C) 1999-2006 MITSUBISHI ELECTRIC CORPORATION and *  RENESAS SOLUTIONS CORPORATION and *  RENESAS TECHNOLOGY CORPORATION *  All rights reserved. * *  Test program for send() and recv() 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;void sig_hdr(){	/* printf("received SIGINT.\n"); */	shutdown(sd, SHUT_RDWR);	if (close(sd) < 0) {		printf("close is NG.\n");		exit(1);	}	printf("close is OK.\n");	exit(0);}/* * TCP client program */main(argc, argv)int	argc;char **argv;{	int i, j, sd, loop, len;	char ch, buf[SIZE], buf2[SIZE];	struct sockaddr_in	sinme, sindst;		if (argc > 1) {		loop = atoi(argv[1]);	}	else {		loop = 10;	}	printf("loop=%d\n", loop);	signal(SIGINT, sig_hdr);	ch = 'A' - 0x01;	for (i=0; i < sizeof(buf); i++) {		if (ch < 'z') ch += 0x01;		else ch = 'A';		buf[i] = ch;	}	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);	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.(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(struct sockaddr_in)) < 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.\n", j);			close(sd);			break;		}		printf("(%d)sending TCP data is OK.(size=%d)\n", j, len);		/* clear data contents */		memset(buf2, 0, SIZE);		/* receive data */		if ((len = recv(sd, buf2, SIZE, 0)) < 0) {			printf("(%d)receiving TCP data is NG.(errno=%d)\n", j, errno);			close(sd);			break;		}		printf("(%d)receiving TCP data is OK.(size=%d)\n", j, len);		/* check data contesnts */		for (i=0; i < len; i++) {			if (buf2[i] != buf[i]) {				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);		/* 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);	}	/* 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");		/* initiate a connection on the socket */	if (connect(sd, (struct sockaddr *)&sindst, sizeof(struct sockaddr_in)) < 0) {		printf("connect2 is NG.(errno=%d)\n", errno);		close(sd);		return (1);	}	printf("connect2 is OK.\n");		/* send zero byte data to terminate server */	if (send(sd, buf, 0, 0) < 0) {		printf("send2 is NG.\n");		close(sd);		return (1);	}	printf("send2 is OK.\n");	/* shut down sending data */	if (shutdown(sd, SHUT_RDWR) < 0) {		printf("shutdown2 is NG.(errno=%d)\n", errno);		return (1);	}	printf("shutdown2 is OK.\n");	if (close(sd) < 0) {		printf("close2 is NG.\n");		return (1);	}	printf("close2 is OK.\n\n");	printf("Pass.\n");	sleep(2);	return (0);}/* EOF */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -