📄 xaccept.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/socktest/xaccept.c,v 1.1.1.1 2001/11/05 17:49:15 tneale Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** * Copyright 1999 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* * $Log: xaccept.c,v $ * Revision 1.1.1.1 2001/11/05 17:49:15 tneale * Tornado shuffle * * Revision 1.3 2001/04/24 19:00:24 paul * Remove prototypes for system functions. * * Revision 1.2 2001/01/19 22:24:57 paul * Update copyright. * * Revision 1.1 2000/10/16 19:23:26 paul * Renamed from test_accept_coop.c * * Revision 1.4 2000/03/17 00:14:46 meister * Update copyright message * * Revision 1.3 1999/10/06 19:29:13 qli * included <signal.h> * * Revision 1.2 1999/05/03 19:12:28 paul * Rewrote to allow multiple concurrent connection attempts without * leaking sockets. * * Revision 1.1 1999/03/04 16:04:51 paul * Cooperating program for the accept tests. Compile it separately on the * machine that's running the echo server for the connect/send/recv tests. * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*//* This is the cooperating program for the accept() test suite. It * waits for a udp trigger packet on port 0xC0DE, and opens a tcp * connection to the port number indicated in the trigger packet * payload. */#include <stdio.h>#include <sys/types.h>#include <stdlib.h>#include <errno.h> /* errno */#include <string.h> /* strerror */#include <sys/uio.h> /* for iovec, but should be automagic */#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h> /* inet_ntoa */#include <netdb.h>#include <sys/ioctl.h>#include <errno.h>#include <sys/time.h>#include <signal.h>#define RCV_PORT 49374#ifndef MAX#define MAX(a,b) ((a)>(b)?(a):(b))#endifvoid main(void){ int rcv_sock, snd_sock, i, j; struct sockaddr_in rcv_si, snd_si; unsigned short port; fd_set rfds, wfds, cfds; char buf[1024];#if defined(SIGPIPE) && defined(SIG_IGN) signal(SIGPIPE, SIG_IGN);#endif if ((rcv_sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { perror("creating listening socket"); return; } memset(&rcv_si, 0, sizeof(rcv_si)); rcv_si.sin_family = AF_INET; rcv_si.sin_port = htons(RCV_PORT); if (bind(rcv_sock, (struct sockaddr *) &rcv_si, sizeof(rcv_si)) != 0) { perror("binding listening socket"); close(rcv_sock); return; } FD_ZERO(&cfds); /* connecting fds */ for (;;) { FD_ZERO(&rfds); FD_SET(rcv_sock, &rfds); wfds = cfds; i = select(FD_SETSIZE, &rfds, &wfds, 0, 0); if (i < 0) perror("select"); if (i == 0) /* shouldn't happen */ continue; if (FD_ISSET(rcv_sock, &rfds)) { memset(&rcv_si, 0, sizeof(rcv_si)); j = sizeof(rcv_si); i = recvfrom(rcv_sock, (void *) &port, sizeof(port), 0, (struct sockaddr *) &rcv_si, &j); printf("got %d bytes from %s, udp port %d\n", i, inet_ntoa(rcv_si.sin_addr), ntohs(rcv_si.sin_port)); if ((snd_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { perror("creating sending socket"); continue; } i = -1; if (ioctl(snd_sock, FIONBIO, (char *) &i) == -1) perror("ioctl(FIONBIO)"); memset(&snd_si, 0, sizeof(snd_si)); snd_si.sin_family = AF_INET; snd_si.sin_addr = rcv_si.sin_addr; snd_si.sin_port = port; printf("connecting to %s, tcp port %d\n", inet_ntoa(snd_si.sin_addr), ntohs(snd_si.sin_port)); if ((connect(snd_sock, (struct sockaddr *) &snd_si, sizeof(snd_si)) != 0) && (errno != EINPROGRESS)) { perror("connecting sending socket"); close(snd_sock); continue; /* If we have an error connecting the socket, we should * eventually time out and become readable again, right? */ } FD_SET(snd_sock, &cfds); } for (snd_sock = 0; snd_sock < FD_SETSIZE; ++snd_sock) { if (FD_ISSET(snd_sock, &wfds)) { printf("writing data\n"); if ((j = send(snd_sock, (const void *) buf, sizeof(buf), 0)) != sizeof(buf)) { fprintf(stderr, "send[%d] returned %d: ", snd_sock, j); perror(""); } if (close(snd_sock) != 0) perror("closing sending socket"); FD_CLR(snd_sock, &cfds); } } } /* end for(;;) */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -