📄 test-tcpc.c
字号:
/***************************************************************************** @(#) test-tcpc.c,v openss7-0_9_2_E(0.9.2.8) 2007/03/12 11:17:55 ----------------------------------------------------------------------------- Copyright (c) 2001-2007 OpenSS7 Corporation <http://www.openss7.com/> Copyright (c) 1997-2000 Brian F. G. Bidulock <bidulock@openss7.org> All Rights Reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ----------------------------------------------------------------------------- U.S. GOVERNMENT RESTRICTED RIGHTS. If you are licensing this Software on behalf of the U.S. Government ("Government"), the following provisions apply to you. If the Software is supplied by the Department of Defense ("DoD"), it is classified as "Commercial Computer Software" under paragraph 252.227-7014 of the DoD Supplement to the Federal Acquisition Regulations ("DFARS") (or any successor regulations) and the Government is acquiring only the license rights granted herein (the license rights customarily provided to non-Government users). If the Software is supplied to any unit or agency of the Government other than DoD, it is classified as "Restricted Computer Software" and the Government's rights in the Software are defined in paragraph 52.227-19 of the Federal Acquisition Regulations ("FAR") (or any successor regulations) or, in the cases of NASA, in paragraph 18.52.227-86 of the NASA Supplement to the FAR (or any successor regulations). ----------------------------------------------------------------------------- Commercial licensing and support of this software is available from OpenSS7 Corporation at a fee. See http://www.openss7.com/ ----------------------------------------------------------------------------- Last Modified 2007/03/12 11:17:55 by brian ----------------------------------------------------------------------------- test-tcpc.c,v Revision 0.9.2.8 2007/03/12 11:17:55 brian - rationalize sctp test programs Revision 0.9.2.7 2007/03/12 10:19:26 brian - corrections Revision 0.9.2.6 2007/03/12 09:33:49 brian - boosted default test port numbers from 10000 to 18000 Revision 0.9.2.5 2007/03/12 02:23:17 brian - updating tests Revision 0.9.2.4 2005/05/14 08:31:28 brian - copyright header correction *****************************************************************************/#ident "@(#) test-tcpc.c,v openss7-0_9_2_E(0.9.2.8) 2007/03/12 11:17:55"static char const ident[] = "test-tcpc.c,v openss7-0_9_2_E(0.9.2.8) 2007/03/12 11:17:55";#include <stdio.h>#include <errno.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netinet/tcp.h>#include <arpa/inet.h>#include <netdb.h>#include <unistd.h>#include <fcntl.h>#include <sys/poll.h>#include <sys/time.h>#include <signal.h>#include <getopt.h>#include <stdlib.h>#include <string.h>#ifdef _GNU_SOURCE#include <getopt.h>#endif#define MSG_LEN 64#define HOST_BUF_LEN 256int rep_time = 1;static int timer_timeout = 0;static voidtimer_handler(int signum){ if (signum == SIGALRM) timer_timeout = 1; return;}static inttimer_sethandler(void){ sigset_t mask; struct sigaction act; act.sa_handler = timer_handler; act.sa_flags = SA_RESTART | SA_ONESHOT; act.sa_restorer = NULL; sigemptyset(&act.sa_mask); if (sigaction(SIGALRM, &act, NULL)) return -1; sigemptyset(&mask); sigaddset(&mask, SIGALRM); sigprocmask(SIG_UNBLOCK, &mask, NULL); return 0;}static intstart_timer(void){ struct itimerval setting = { {0, 0}, {rep_time, 0} }; if (timer_sethandler()) return -1; if (setitimer(ITIMER_REAL, &setting, NULL)) return -1; timer_timeout = 0; return 0;}static struct sockaddr_in loc_addr = { AF_INET, 0, {INADDR_ANY}, };static struct sockaddr_in rem_addr = { AF_INET, 0, {INADDR_ANY}, };int len = MSG_LEN;int nodelay = 1;inttest_tcpc(void){ int fd; int out_offset = 0, inp_offset = 0; long rtt_delay = 0; long inp_count = 0, out_count = 0; long inp_bytes = 0, out_bytes = 0; struct pollfd pfd[1] = { {0, POLLIN | POLLOUT | POLLERR | POLLHUP, 0} }; unsigned char my_msg[] = "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it.\0" "This is a good short test message that has some 64 bytes in it."; unsigned char ur_msg[2048]; fprintf(stderr, "Opening socket\n"); if ((fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { perror("socket"); goto dead; } fprintf(stderr, "Binding socket to %s:%d\n", inet_ntoa(loc_addr.sin_addr), ntohs(loc_addr.sin_port)); if (bind(fd, (struct sockaddr *) &loc_addr, sizeof(loc_addr)) < 0) { perror("bind"); goto dead; } if (connect(fd, (struct sockaddr *) &rem_addr, sizeof(rem_addr)) < 0) { perror("connect"); goto dead; } if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { perror("fcntl"); goto dead; } if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) < 0) { perror("setsockopt"); goto dead; } if (start_timer()) { perror("timer"); goto dead; } for (;;) { pfd[0].fd = fd; pfd[0].events = POLLOUT | POLLIN | POLLERR | POLLHUP; pfd[0].revents = 0; if (timer_timeout) { printf ("Bytes sent: %7ld, recv: %7ld, tot: %7ld, dif: %8ld dly: %6ld\n", out_bytes, inp_bytes, out_bytes + inp_bytes, inp_bytes - out_bytes, inp_count ? rtt_delay / inp_count : 0); inp_count = 0; out_count = 0; inp_bytes = 0; out_bytes = 0; rtt_delay = 0; if (start_timer()) { perror("timer"); goto dead; } continue; } if (poll(&pfd[0], 1, -1) < 0) { if (errno == EINTR || errno == EAGAIN) continue; perror("poll"); goto dead; } if (pfd[0].revents & POLLIN) { int rtn; if ((rtn = recv(fd, ur_msg + inp_offset, len - inp_offset, MSG_DONTWAIT)) < 0) { if (errno == EINTR || errno == EAGAIN) goto skip_pollin; perror("recv"); goto dead; } if (rtn) { inp_bytes += rtn; inp_offset += rtn; if (inp_offset >= len) { struct timeval tnow; struct timeval *tv = (struct timeval *) ur_msg; if (gettimeofday(&tnow, NULL) < 0) { perror("gettimeofday"); goto dead; } if (tnow.tv_sec < tv->tv_sec || (tnow.tv_sec == tv->tv_sec && tnow.tv_usec < tv->tv_usec)) fprintf(stderr, "time: %ld.%06ld before %ld.%06ld\n", tnow.tv_sec, tnow.tv_usec, tv->tv_sec, tv->tv_usec); rtt_delay += (tnow.tv_sec - tv->tv_sec) * 1000000 + tnow.tv_usec - tv->tv_usec; inp_count++; inp_offset = 0; } } continue; } skip_pollin: if (pfd[0].revents & POLLOUT) { int rtn; if (!out_offset) { struct timeval *tv = (struct timeval *) my_msg;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -