📄 xclose.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/socktest/xclose.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: xclose.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:29 paul * Renamed from test_close_coop.c * * Revision 1.2 2000/03/17 00:14:47 meister * Update copyright message * * Revision 1.1 1999/05/03 19:13:43 paul * Cooperating program for the close 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 close() test suite. It * listens on tcp port 0xB1FF, and read one byte, which is the number * of seconds to sleep before closing connection. */#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>#define RCV_PORT 45567void main(void){ int s, ns, sz, i; struct sockaddr_in si; unsigned char delay;#if defined(SIGPIPE) && defined(SIG_IGN) signal(SIGPIPE, SIG_IGN);#endif if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) { perror("creating listening socket"); return; } memset(&si, 0, sizeof(si)); si.sin_family = AF_INET; si.sin_port = htons(RCV_PORT); if (bind(s, (struct sockaddr *) &si, sizeof(si)) != 0) { perror("binding listening socket"); close(s); return; } if (listen(s, 5) != 0) { perror("listen"); close(s); return; } for (;;) { sz = sizeof(si); if ((ns = accept(s, (struct sockaddr *) &si, &sz)) < 0) { perror("accept"); continue; } printf("connection from %s, port %d\n", inet_ntoa(si.sin_addr), ntohs(si.sin_port)); if ((i = recv(ns, (void *) &delay, sizeof(delay), 0)) < 0) { perror("recv"); } else if (i != sizeof(delay)) { fprintf(stderr, "received %d bytes, expected 1: ", i); perror(""); } printf("sleeping %d seconds...\n", (unsigned) delay); if (delay) sleep((unsigned) delay); printf("closing\n"); close(ns); } if (close(s) != 0) { perror("closing listening socket"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -