📄 tsend.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/socktest/tsend.c,v 1.1.1.1 2001/11/05 17:49:14 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 1998-1999 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* * $Log: tsend.c,v $ * Revision 1.1.1.1 2001/11/05 17:49:14 tneale * Tornado shuffle * * Revision 1.4 2001/05/24 20:32:31 paul * CAD-UL compiler is picky. * * Revision 1.3 2001/01/19 22:24:56 paul * Update copyright. * * Revision 1.2 2000/10/27 18:06:26 paul * Some comments should only be made in verbose mode. * * Revision 1.1 2000/10/16 19:23:04 paul * Renamed from test_send.c * * Revision 1.10 2000/03/17 00:14:49 meister * Update copyright message * * Revision 1.9 1999/07/30 21:29:47 paul * enhanced test_fail, test_pass * * Revision 1.8 1999/06/25 23:44:42 wes * Test large non-blocking sends and select-for-writeable * * Revision 1.7 1999/06/25 22:52:05 wes * Test large sends. * * Revision 1.6 1999/05/12 19:04:33 paul * Added tests for unconnected socket, listening socket; properly * qualified test for bogus address (udp succeeds, tcp fails). * * Revision 1.5 1999/05/03 19:24:09 paul * Removed loopback test. * * Revision 1.4 1999/02/18 04:16:13 wes * Socket Merge: new code (socket tests). * * Revision 1.3.2.6 1999/01/06 21:41:55 paul * added configurable server address * * Revision 1.3.2.5 1998/11/06 23:36:53 paul * rototill to support tcp * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*/#include "test.h"static char buf[32];static char bigbuf[1024*10];static void test_send_1(int type, char *proto);voidtest_send(int flags){ if (flags & TEST_UDP) test_send_1(SOCK_DGRAM, "udp"); if (flags & TEST_TCP) test_send_1(SOCK_STREAM, "tcp");}static voidtest_send_1(int type, char *proto){ int s; struct sockaddr_in si; int opt; int i, eret, eerr; for (i = 0; i < sizeof(buf); ++i) buf[i] = i; for (i = 0; i < sizeof(bigbuf); ++i) bigbuf[i] = i; s = socket(PF_INET, type, 0); if (s < 0) { test_fail(proto, "send", "socket"); return; } memset(&si, 0, sizeof(si)); test(proto, "send invalid socket descr", send(-1, 0, 0, 0), -1, EBADF); test(proto, "send unconnected socket", send(s, buf, sizeof(buf), 0), -1, (type == SOCK_DGRAM) ? EDESTADDRREQ : ENOTCONN); if (type == SOCK_STREAM) { listen(s, 5); test(proto, "send listening socket", send(s, buf, sizeof(buf), 0), -1, ENOTCONN); close(s); s = socket(PF_INET, type, 0); opt = -1; ioctl(s, FIONBIO, (char *) &opt); } si.sin_family = AF_INET; si.sin_port = htons(9); si.sin_addr.s_addr = htonl(0x0a000001); /* bogus */ connect(s, (struct sockaddr *) &si, sizeof(si)); if (type == SOCK_DGRAM) { eret = sizeof(buf); eerr = 0; } else { eret = -1; eerr = ENOTCONN; } test(proto, "send 10.0.0.1", send(s, buf, sizeof(buf), 0), eret, eerr); if (type == SOCK_STREAM) { close(s); s = socket(PF_INET, type, 0); } si.sin_addr.s_addr = server; connect(s, (struct sockaddr *) &si, sizeof(si)); test(proto, "send zero-length message", send(s, buf, 0, 0), 0, 0); test(proto, "send null buffer (zero length)", send(s, 0, 0, 0), 0, 0); test(proto, "send null buffer (non-zero length)", send(s, 0, sizeof(buf), 0), -1, EFAULT); if (type == SOCK_STREAM) { int count, ret; char *ptr; opt = -1; if (ioctl(s, FIONBIO, (char *) &opt) == -1) test_fail(proto, "send", "ioctl(FIONBIO on)"); ptr = bigbuf; count = sizeof(bigbuf); do { ret = send(s, ptr, count, 0); if (ret < 0) { if (errno == EWOULDBLOCK) { fd_set writefds; FD_ZERO(&writefds); FD_SET(s, &writefds); if (verbose) printf("blocking in select\n"); select(s+1, 0, &writefds, 0, 0); } else { if (verbose) printf("send failed with errno %d\n", errno); break; } } else { if (verbose) printf("wrote chunk of %d bytes\n", ret); ptr += ret; count -= ret; } } while (count > 0); opt = 0; if (ioctl(s, FIONBIO, (char *) &opt) == -1) test_fail(proto, "send", "ioctl(FIONBIO off)"); test(proto, "send short-length message", send(s, buf, sizeof(buf), 0), sizeof(buf), 0); test(proto, "send long message", send(s, bigbuf, sizeof(bigbuf), 0), sizeof(bigbuf), 0); } if (type == SOCK_DGRAM) { eret = -1; eerr = EOPNOTSUPP; } else { eret = sizeof(buf); eerr = 0; } test(proto, "send OOB", send(s, buf, sizeof(buf), MSG_OOB), eret, eerr); test(proto, "send discard", send(s, buf, sizeof(buf), 0), sizeof(buf), 0); if (type == SOCK_DGRAM) { si.sin_port = htons(999); connect(s, (struct sockaddr *) &si, sizeof(si)); test(proto, "send port 999", send(s, buf, sizeof(buf), 0), sizeof(buf), 0); /* server eventually sends a port unreachable message */ si.sin_port = htons(9); connect(s, (struct sockaddr *) &si, sizeof(si)); test(proto, "send message too big", send(s, buf, 16383, 0), -1, EMSGSIZE); si.sin_addr.s_addr = htonl(INADDR_BROADCAST); connect(s, (struct sockaddr *) &si, sizeof(si)); test(proto, "send broadcast without SO_BROADCAST set", send(s, buf, sizeof(buf), 0), -1, EACCES); opt = 1; if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, (void *) &opt, sizeof(opt)) != 0) test_fail(proto, "send", "setsockopt(SO_BROADCAST)"); else { test(proto, "send broadcast to INADDR_BROADCAST", send(s, buf, sizeof(buf), 0), sizeof(buf), 0); /* BSD sends to the subnet bcast addr */ } } if (shutdown(s, 2) != 0) { test_fail(proto, "send", "shutdown"); } else { test(proto, "send shutdown socket", send(s, buf, sizeof(buf), 0), -1, EPIPE); } close(s);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -