📄 h_udp_srv7.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;void sig_hdr(){ /* printf("received SIGINT.\n"); */ close(sd); sleep(2); exit(0);}#define SIZE (8*1024)#define SERVER 1#define CLIENT 1/* #define CLIENT2 1 */static int comm_size[] = { 1, 2, 4, 10, 16, 30, 32, 50, 64, 100, 128, 200, 256, 500, 512, 1000, 1024, 1472, 2000, 2048, 4000, 4096, 8000};/* * UDP 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 UDP communication */ errno = 0; if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { printf("socket is NG.(errno=%d)\n", errno); return (1); } printf("socket is OK.\n"); #ifdef SERVER /* 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");#endif /* SERVER */err_flag = 0;for (k=0; k < sizeof(comm_size)/sizeof(comm_size[0]); k++) { size2 = comm_size[k]; printf("\nTest-%d: size=%d\n", k+1, size2); for (j=1; j <= LOOP; j++) {#ifdef SERVER /* receive data */ total = 0; while (total < size2) { /* clear data contents */ memset(buf2, 0, sizeof(buf2)); memset((char *)&sindst, 0, sizeof(struct sockaddr_in)); addrlen = 0; errno = 0; if ((len = recvfrom(sd, buf2, size2, 0, (struct sockaddr *)&sindst, &addrlen)) < 0) { printf("(%d)receiving UDP data is NG.(errno=%d)\n", j, errno); close(sd); 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 UDP data contents is NG.\n", j); close(sd); goto err; } } if (total == 0) { printf("(%d)receiving UDP data is OK.(total size=%d)\n", j, total); if (close(sd) < 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 UDP data is NG.(total size=%d)\n", j, total); close(sd); err_flag = 1; break; } else { printf("(%d)receiving UDP data is OK.(total size=%d)\n", j, total); }#endif /* SERVER */#ifdef CLIENT /* set data contents */ memset(buf, ch, size2); /* 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, size2, 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);#endif /* CLIENT */#ifdef CLIENT2 sleep(1);#endif /* CLIENT2 */ } /* for */ if (total == 0 || err_flag) { break; }#ifdef CLIENT2 sleep(2);#endif /* CLIENT2 */} /* for */ if (close(sd) < 0) { printf("close is NG.(errno=%d)\n", errno); } printf("close is OK.\n");err: free(buf); free(buf2); printf("Pass.\n"); sleep(2); return (0);}/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -