📄 trecv.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/socktest/trecv.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: trecv.c,v $ * Revision 1.1.1.1 2001/11/05 17:49:14 tneale * Tornado shuffle * * Revision 1.3 2001/01/19 22:24:55 paul * Update copyright. * * Revision 1.2 2000/10/20 18:32:35 paul * si.sin_len is only defined for BSD * * Revision 1.1 2000/10/16 19:22:50 paul * Renamed from test_recv.c * * Revision 1.10 2000/03/17 00:14:48 meister * Update copyright message * * Revision 1.9 1999/07/30 21:29:45 paul * enhanced test_fail, test_pass * * Revision 1.7 1999/05/13 20:03:03 paul * MSG_PEEK test * * Revision 1.6 1999/05/12 19:03:44 paul * Added test for unconnected socket. * * Revision 1.5 1999/05/03 19:23:48 paul * Added SO_RCVTIMEO test. * * Revision 1.4 1999/02/18 04:16:08 wes * Socket Merge: new code (socket tests). * * Revision 1.3.2.7 1999/02/09 20:35:38 paul * simplified test_rf * * Revision 1.3.2.6 1999/02/09 18:29:32 paul * fixed empty-buffer tests * * Revision 1.3.2.5 1999/01/06 21:41:54 paul * added configurable server address * * Revision 1.3.2.4 1998/11/10 19:37:16 paul * fixed init of send buffer * * Revision 1.3.2.3 1998/11/06 23:36:52 paul * rototill to support tcp * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*/#include "test.h"static voidtest_rf(char *proto, char *name, int s, char *snd_buf, size_t snd_len, void *rcv_buf, size_t rcv_len, int flags, int expected, int expected_errno){ int ret, i; static int j = 0; fd_set readfds; struct timeval tv; if (snd_buf) { /* initialize the buffer differently every time, to catch cases * where recv gets a previously-queued packet */ for (i = 0, ++j; i < snd_len; ++i) snd_buf[i] = j; if (send(s, snd_buf, snd_len, flags) != snd_len) test_fail(proto, name, "send"); } if (rcv_buf) memset(rcv_buf, 0, rcv_len); /* Select here to make sure we get either a valid echo response or * EWOULDBLOCK. Otherwise we could screw some of our 0-buffer tests. */ if (s >= 0) { /* don't try select -1 fd */ FD_ZERO(&readfds); FD_SET(s, &readfds); tv.tv_sec = 1; tv.tv_usec = 0; select(s+1, &readfds, 0, 0, &tv); } ret = recv(s, rcv_buf, rcv_len, flags); test(proto, name, ret, expected, expected_errno); if ((ret != -1) && rcv_buf && snd_buf && (memcmp(rcv_buf, snd_buf, ret) != 0)) test_fail(proto, name, "unexpected data");}static void test_recv_1(int type, char *proto);voidtest_recv(int flags){ if (flags & TEST_UDP) test_recv_1(SOCK_DGRAM, "udp"); if (flags & TEST_TCP) test_recv_1(SOCK_STREAM, "tcp");}static voidtest_recv_1(int type, char *proto){ int s; struct sockaddr_in snd_si; char snd_buf[32], rcv_buf[32]; int opt; int i; struct timeval tv; for (i = 0; i < sizeof(snd_buf); ++i) snd_buf[i] = i; s = socket(PF_INET, type, 0); if (s < 0) { test_fail(proto, "recv", "socket"); return; } tv.tv_sec = 5; tv.tv_usec = 0; if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (void *) &tv, sizeof(tv)) != 0) test_fail(proto, "recv", "setsockopt(SO_RCVTIMEO)"); if (type == SOCK_STREAM) { test_rf(proto, "recv unconnected socket", s, 0, 0, rcv_buf, sizeof(rcv_buf), 0, -1, ENOTCONN); } memset(&snd_si, 0, sizeof(snd_si)); snd_si.sin_family = AF_INET;#if INSTALL_ATTACHE_SOCKETS_44BSD snd_si.sin_len = sizeof(snd_si);#endif snd_si.sin_addr.s_addr = server; snd_si.sin_port = htons(7); /* echo */ if (connect(s, (struct sockaddr *) &snd_si, sizeof(snd_si)) != 0) { test_fail(proto, "recv", "connect"); return; } test_rf(proto, "recv invalid socket descr", -1, 0, 0, 0, 0, 0, -1, EBADF); test_rf(proto, "recv null buffer", s, snd_buf, sizeof(snd_buf), 0, sizeof(rcv_buf), 0, -1, EFAULT); test_rf(proto, "recv zero-length buffer", s, 0, 0, rcv_buf, 0, 0, 0, 0); /* UDP zero-length recv consumes data, but TCP doesn't; sync up here */ if (type == SOCK_STREAM) { test_rf(proto, "recv old data", s, 0, 0, rcv_buf, sizeof(rcv_buf), 0, sizeof(snd_buf), 0); } test_rf(proto, "recv data", s, snd_buf, sizeof(snd_buf), rcv_buf, sizeof(rcv_buf), 0, sizeof(snd_buf), 0); test_rf(proto, "recv MSG_PEEK", s, snd_buf, sizeof(snd_buf), rcv_buf, sizeof(rcv_buf), MSG_PEEK, sizeof(snd_buf), 0); test_rf(proto, "recv MSG_PEEK (again)", s, 0, 0, rcv_buf, sizeof(rcv_buf), MSG_PEEK, sizeof(snd_buf), 0); test_rf(proto, "recv after MSG_PEEK", s, 0, 0, rcv_buf, sizeof(rcv_buf), 0, sizeof(snd_buf), 0); /* Perversely, BSD returns EWOULDBLOCK instead of ETIMEDOUT when a * blocking operation times out. */ test_rf(proto, "recv empty queue (blocking)", s, 0, 0, rcv_buf, sizeof(rcv_buf), 0, -1, EWOULDBLOCK); opt = -1; if (ioctl(s, FIONBIO, (char *) &opt) == -1) test_fail(proto, "recv", "ioctl(FIONBIO)"); test_rf(proto, "recv empty queue (non-blocking)", s, 0, 0, rcv_buf, sizeof(rcv_buf), 0, -1, EWOULDBLOCK); if (send(s, snd_buf, sizeof(snd_buf), 0) != sizeof(snd_buf)) test_fail(proto, "recv", "send before shutdown"); else if (shutdown(s, 2) != 0) test_fail(proto, "recv", "shutdown"); else { test_rf(proto, "recv shutdown socket", s, 0, 0, rcv_buf, sizeof(rcv_buf), 0, 0, 0); } close(s);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -