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

📄 h_udp_srv1.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 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, newsd;void sig_hdr(){	/* printf("received SIGINT.\n"); */	close(sd);	exit(0);}/* * UDP server program */main(argc, argv)int	argc;char **argv;{	int i, j, len;	char ch, buf[SIZE], buf2[SIZE];	struct sockaddr_in	sinme, sindst;	int addrlen;	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;	}	for (j=1; j >= 1; j++) {		/* create an endpoint for UDP communication */		if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {			printf("(%d)socket is NG.(errno=%d)\n", j, errno);			return (1);		}		printf("(%d)socket is OK.\n", j);			/* 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(sinme)) < 0) {			printf("(%d)bind is NG.(errno=%d)\n", j, errno);			close(sd);			return (1);		}		printf("(%d)bind is OK.\n", j);		/* clear data contents */		memset(buf2, 0, sizeof(buf2));		/* receive data */		memset((char *)&sindst, 0, sizeof(struct sockaddr_in));		addrlen = 0;		if ((len = recvfrom(sd, buf2, sizeof(buf2), 0,				(struct sockaddr *)&sindst, &addrlen)) < 0) {			printf("(%d)receiving UDP data is NG.\n", j);			close(sd);			break;		}		printf("(%d)receiving UDP data is OK.(size=%d)\n", j, len);		if (len == 0) {			close(sd);			break;		}		/* check data contesnts */		for (i=0; i < len; i++) {			if (buf2[i] != buf[i]) {				break;			}		}		if (i < len) {			printf("(%d)checking UDP data contents is NG.\n", j);			close(sd);			break;		}		printf("(%d)checking UDP data contents is OK.\n", j);		/* send data */		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);		if ((len = sendto(sd, buf, len, 0,				(struct sockaddr *)&sindst, sizeof(sindst))) < 0) {			printf("(%d)sending UDP data is NG.\n", j);			close(sd);			break;		}		printf("(%d)sending UDP 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);	}	printf("\nPass.\n");	sleep(2);	return (0);}/* EOF */

⌨️ 快捷键说明

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