simple tcp client source code for testing.
TCPパケットを送信して、受信を待つというプログラムです。 このブログのTCPサーバーと対で使います。クライアント側から受信すべきデータのサイズと送信インターバルを指定できます。
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
extern int h_errno;
char buf[20000];
char logtimes[128];
#include <sys/time.h>
#include <time.h>
const char *logtime()
{
struct timeval tv;
struct tm * tm;
gettimeofday(&tv, 0);
tm = (struct tm *)localtime(&tv.tv_sec);
sprintf(logtimes,"%02d/%02d/%02d %02d:%02d:%02d.%03d ",
tm->tm_year - 100, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec,
(int)(tv.tv_usec/ 1000));
return logtimes;
}
int main(int argc, char *argv[])
{
int total = 0;
int i;
int sock;
struct sockaddr_in addr;
char *ips = "0.0.0.0";
char *ipd = "0.0.0.0";
int nPort = 12345;
int nCount = 0;
int nInt = 0;
int rlen = 0;
int c;
int bWait = 1;
int datalen = 1024;
opterr = 0;
i = time(0) % 10000;
while(( c = getopt(argc, argv, "s:d:p:n:t:w:l:hr:")) != -1)
{
switch(c)
{
case 'l':
datalen = atol(optarg);
break;
case 'h':
printf(" tcps (client) \n");
printf(" -d xxx.xxx.xxx.xxx : server IP [0.0.0.0]\n");
printf(" -p 9999 : server port [12345]\n");
printf(" -l 9999 : send packet size [1500]\n");
printf(" -w [0|1] : wait reply data [1:Wait]\n");
printf(" -n 9999 : request reply count [no-request]\n");
printf(" -t 9999 : request reply interval milli-sec. [no-request]\n");
printf(" -r 9999 : request reply length byte. [no-request]\n");
printf(" \n");
return 0;
break;
case 's':
ips = optarg;
break;
case 'd':
ipd = optarg;
break;
case 'p':
printf("-p %p\n", optarg);
if (optarg)
nPort = atol(optarg);
break;
case 'n':
if (optarg)
nCount = atol(optarg);
break;
case 't':
if (optarg)
nInt = atol(optarg);
if (nInt <= 0)
nInt = 1;
break;
case 'w':
bWait = atol(optarg);
break;
case 'r':
rlen = atol(optarg);
break;
}
}
if (datalen > sizeof(buf))
datalen = sizeof(buf)+
printf("\n tcps (client)\n");
printf(" Src = %s, Dst = %s, Port=%d, send datalen=%d %s\n",
ips, ipd, nPort, datalen, bWait ? "Wait" : "Not-Wait");
printf(" Request legnth=%d, count=%d, interval=%dmsec\n",
rlen, nCount, nInt);
sock = socket(AF_INET,SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(nPort);
addr.sin_addr.s_addr = inet_addr(ipd);
if (connect(sock, (struct sockaddr *)&addr,sizeof(addr)) != 0)
{
printf("connect error %d\n", h_errno);
close(sock);
return 0;
}
memset(buf, i & 0x07f, sizeof(buf));
sprintf(buf, "P%04d-L%d-C%d-T%d", i,
rlen, nCount, nInt);
sendto(sock, buf, datalen, 0, (struct sockaddr *)&addr, sizeof(addr));
printf("%sSND>>>%s",logtime(), buf);
printf("... %d\n", datalen);
while(bWait)
{
int addrlen;
char senderstr[256];
int reclen;
memset(buf, 0, sizeof(buf));
reclen = read(sock, buf, sizeof(buf) - 1);
if (reclen <= 0) // if host is closed ?
break;
total += reclen;
printf("%sRCV<<<%s",logtime(), buf);
printf("... len=%d... ", reclen);
printf("\n");
usleep(10);
}
printf(" total length=%d\n", total);
printf(" ... end.\n");
close(sock);
return 1;
}
0 件のコメント:
コメントを投稿