📄 socktest.c
字号:
/* Copyright 2008 SMSC, All rights reserved
*
* udp & tcp test application
* File : socktest.c
*/
#include "socktest.h"
#if defined (UNIX) || defined (WIN32)
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#else /* smsc network stack */
#include "api/utilities.h"
#endif
#ifndef WIN32 /* UNIX or smsc network stack */
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#endif
#define SELECT_TEST (0)
#define DEF_PORT 6000
#define DEF_SEND_MSG_SIZE 64
#define MAX_RECV_MSG_SIZE 1500
#define DEF_BURST_COUNT 10
#define START_MARKER "SSSSSS"
#define END_MARKER "EEEEEE"
#if defined (UNIX) || defined (WIN32)
#define SMSC_NOTICE(condition, message) \
do { \
if(condition) \
printf message; \
printf ("\n"); \
} while(0)
#define SMSC_TRACE(condition, message) \
do { \
if(condition) \
printf message; \
printf ("\n"); \
} while(0)
#endif
struct stat {
unsigned int bytesReceived;
double secRecvTime;
};
#if defined (UNIX) || defined (WIN32)
extern char *optarg;
struct tester test;
void usage()
{
SMSC_NOTICE(1,("usage : socktest <[-s -d <destination ip> <-p port> [-m msg size]]> [-c count] [-l local port] [-u] [-t] [-b burst time] [-n burst count]\n)"));
SMSC_NOTICE(1, ("where : s - send, u - udp, t - skip data verify, b - should be 0 for continuos transmit)"));
return;
}
void catch_signal()
{
/* Record end time */
read_end_time(&test);
/* Declare end of transmission */
if (test.sflag)
end_of_transmission(&test);
else {
SMSC_TRACE(1, ("Bytes received:%u Time to receive:=%f", test.byteCount, test.realTime));
/* For throughput measurement, use the byteCount count from other host and use the local time. */
SMSC_TRACE(1, ("Throughput: %lu Kbps", (unsigned long)((test.byteCount * 8.0)/test.realTime/1000.0)));
}
exit(0);
}
int main(int argc, char *argv[])
{
int i, c, rc = 0;
char *destAddr = NULL;
#ifdef WIN32
WSADATA wsa_data ;
/* Initialize the winsock lib ( version 2.2 ) */
if ( WSAStartup(MAKEWORD(2,2), &wsa_data) == SOCKET_ERROR ) {
SMSC_NOTICE(1, ("WSAStartup() failed"));
return 1 ;
}
#endif
memset(&test, 0, sizeof(test));
test.burstCount = DEF_BURST_COUNT;
while ((c = getopt (argc, argv, "suthc:d:p:l:m:b:n:")) != -1) {
switch (c) {
case 's':
test.sflag = 1;
break;
case 'c':
test.count = atoi(optarg);
break;
case 'd':
destAddr = optarg;
break;
case 'p':
test.dport = atoi(optarg);
break;
case 'l':
test.lport = atoi(optarg);
break;
case 'm':
test.sndMsgLen = atoi(optarg);
break;
case 'u':
test.udp = 1;
break;
case 't':
test.skipVerify = 1;
break;
case 'b':
test.burstTime = atoi(optarg);
break;
case 'n':
test.burstCount = atoi(optarg);
break;
case '?':
case 'h':
default:
usage();
return 1;
}
}
if (test.sflag && !destAddr && !test.dport) {
usage();
return;
}
if (!test.lport && !test.sflag)
test.lport = DEF_PORT;
test.dataCount = 0;
if (!test.sndMsgLen)
test.sndMsgLen = DEF_SEND_MSG_SIZE;
test.myAddr.sin_family = AF_INET;
test.myAddr.sin_addr.s_addr = htonl(INADDR_ANY);
test.myAddr.sin_port = htons(test.lport);
if (test.sflag) {
test.otherAddr.sin_family = AF_INET;
test.otherAddr.sin_addr.s_addr = inet_addr(destAddr);
test.otherAddr.sin_port = htons(test.dport);
}
tester_start(&test);
}
#else /* smsc network stack */
int socket_test_initiate(
struct tester *test,
int udp, /* 1 for udp test, 0 for tcp */
int sflag, /* 1 if sender(client), 0 for receiver(server) */
char *destAddr, /* Destination address, used when sender (sflag=1) */
int dport, /* Destination port, used when sender */
int lport, /* Local port to bind, used when receiver (sflag=0) */
unsigned int count, /* No. of message to send, used when sender */
int msgLen, /* Length of the message to send, used when sender */
short skipVerify, /* If 1, skip the data verification (only throughput) */
short burstTime, /* time to pause in ms after each burst*/
unsigned long burstCount /* No. of messages to send in each burst */
)
{
test->udp = udp;
test->sflag = sflag;
test->dport = dport;
test->lport = lport;
test->count = count;
test->skipVerify = skipVerify;
test->burstTime = burstTime;
if (!burstCount)
test->burstCount = DEF_BURST_COUNT;
else
test->burstCount = burstCount;
if (!msgLen)
test->sndMsgLen = DEF_SEND_MSG_SIZE;
else
test->sndMsgLen = msgLen;
if (!test->lport && !test->sflag)
test->lport = DEF_PORT;
test->dataCount = 0;
test->myAddr.sin_family = AF_INET;
test->myAddr.sin_addr.s_addr = htonl(INADDR_ANY);
test->myAddr.sin_port = htons(test->lport);
if (test->sflag) {
test->otherAddr.sin_family = AF_INET;
test->otherAddr.sin_addr.s_addr = inet_addr(destAddr);
test->otherAddr.sin_port = htons(test->dport);
}
/* Start the tester thread */
smsc_thread_create(tester_thread, (void *)test, 2048, SMSC_THREAD_PRIORITY_HIGH, "socktest", 0);
return 0;
}
void tester_thread(void *arg)
{
tester_start(arg);
}
#endif
int tester_start(struct tester *test)
{
int otherAddrLen, i, bCount = 0, rc = 0;
char *msgbuf;
struct stat stat;
#if SELECT_TEST
fd_set read_fd;
struct timeval time_val;
#endif
memset(&stat, 0, sizeof(stat));
#if !(defined (UNIX) || defined (WIN32))
/* udp sender need to wait till stack initializes */
if (test->udp && test->sflag)
smsc_mdelay(10000);
#endif
if (test->udp) {
SMSC_NOTICE(1, ("***UDP TEST**"));
} else {
SMSC_NOTICE(1, ("***TCP TEST**"));
}
if (test->sflag) {
SMSC_NOTICE(1, ("Destination addr: %s Destination Port:%d", (char *)inet_ntoa(test->otherAddr.sin_addr), test->dport));
} else {
SMSC_NOTICE(1, ("Local port:%d", test->lport));
}
/* socket creation */
if ((test->sd = socket(AF_INET, test->udp ? SOCK_DGRAM : SOCK_STREAM, 0)) == SOCKET_ERROR) {
SMSC_NOTICE(1,("cannot open socket"));
return 1;
}
/* bind to any local port */
if(bind(test->sd, (struct sockaddr *) &test->myAddr, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
SMSC_NOTICE(1, ("cannot bind port"));
return 1;
}
#if SELECT_TEST
/* To exercise select: Check whether timeout works*/
FD_ZERO (&read_fd);
FD_SET (test->sd, &read_fd);
time_val.tv_sec=5;
time_val.tv_usec=0;
SMSC_NOTICE(1,("select(): Waiting for %lu secs",time_val.tv_sec));
if ((select(0, NULL, NULL, NULL, &time_val)) == SOCKET_ERROR) {
SMSC_NOTICE(1, ("select error..."));
return 1;
}
SMSC_NOTICE(1,("select(): Waiting done"));
#endif
if (!test->udp) {
if (test->sflag) {
if (connect(test->sd, (struct sockaddr *) &test->otherAddr, sizeof(struct sockaddr_in)) == SOCKET_ERROR ) {
SMSC_NOTICE(1, ("connect error.."));
return 1;
}
}
else {
listen(test->sd, 5);
otherAddrLen = sizeof(struct sockaddr_in);
if ((test->nsd = accept(test->sd, (struct sockaddr *) &test->otherAddr, &otherAddrLen)) == INVALID_SOCKET ) {
SMSC_NOTICE(1, ("accept error..."));
return 1;
}
#ifdef WIN32
closesocket(test->sd);
#else
close(test->sd);
#endif
}
}
/* send data */
if (test->sflag) {
if ((msgbuf = malloc((unsigned int)test->sndMsgLen)) == NULL)
{
SMSC_NOTICE(1, ("malloc error.."));
return 1;
}
#if defined (UNIX) || defined (WIN32)
/* install signal handler */
signal(SIGINT, (void *)catch_signal);
#endif
SMSC_TRACE(1,("Send the START marker..."));
/* Send start marker */
if (msgsend(test, START_MARKER, strlen(START_MARKER)) < 0)
{
SMSC_NOTICE(1, ("send data failure.."));
return 1;
}
/* Record start time */
read_start_time(test);
do {
memset(msgbuf, 0, test->sndMsgLen);
if(!test->skipVerify) {
/* Fill the sequential data to the buffer */
for (i = 0; i < test->sndMsgLen; i++)
{
msgbuf[i] = test->dataCount++;
}
if (test->udp)
test->dataCount = 0;
}
rc = msgsend(test, msgbuf, test->sndMsgLen);
if(rc < 0)
{
SMSC_NOTICE(1,("send data failure.."));
return(1);
}
if(!test->skipVerify) {
/* If the transmitted byte is less than data size adjust the dataCount */
if (rc != test->sndMsgLen)
test->dataCount-=(test->sndMsgLen-rc);
}
test->msgCount++;
test->byteCount += rc;
if (test->count)
if (!(--test->count))
break;
bCount++;
if (test->burstTime && bCount == test->burstCount) {
bCount = 0;
#ifdef WIN32
Sleep(test->burstTime);
#else
#ifdef UNIX
usleep(test->burstTime*1000);
#else
smsc_mdelay(test->burstTime);
#endif
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -