bug fix, add server send/recv normal udp

This commit is contained in:
HappyZ 2016-09-28 09:56:02 -07:00
parent a5d8de37e1
commit b584fa0cf5
22 changed files with 267 additions and 641 deletions

View File

@ -1,21 +0,0 @@
CC = /media/Lucifer/android/lib/android-18-toolchain/bin/arm-linux-androideabi-gcc
#CC = arm-linux-androideabi-gcc
CFLAGS = -pie -fPIE
TARGET = TCPSender_mobile
OBJS = $(patsubst %.c, %.o, $(wildcard *.c))
HEADERS = $(wildcard *.h)
all: $(TARGET)
%.o: %.c $(HEADERS)
$(CC) $(CFLAGS) -c $< -o $@
.PRECIOUS: $(TARGET) $(OBJS)
$(TARGET): $(OBJS)
$(CC) $(OBJS) -Wall $(CFLAGS) -o $@
clean:
-rm -f *.o
-rm -f $(TARGET)

View File

@ -1,133 +0,0 @@
/*
* 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, either version 3 of the License, or
* (at your option) any later version.
*/
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/ether.h>
#include <sys/sendfile.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#define ETH_P_IP 0x0800 /* Internet Protocol packet */
#define ETH_ALEN 6 /* from <net/ethernet.h> */
#define ETH_P_ALL 0x0003
#define MY_DEST_MAC0 0xba
#define MY_DEST_MAC1 0xf6
#define MY_DEST_MAC2 0xb1
#define MY_DEST_MAC3 0x71
#define MY_DEST_MAC4 0x09
#define MY_DEST_MAC5 0x64
#define DEFAULT_IF "wlan0"
#define BUF_SIZ 8192
int main(int argc, char *argv[])
{
int slotLength = 10000; // in microseconds
int quota = 1000000000; // Bytes per slot, default 1GB/slot
int sentInSlot = 0, slot = 1;
double elapsedTime;
struct iovec iov;
int sockfd, listenfd;
struct ifreq if_idx;
struct ifreq if_mac;
int tx_len = 0;
char sendbuf[BUF_SIZ];
struct ether_header *eh = (struct ether_header *) sendbuf;
struct iphdr *iph = (struct iphdr *) (sendbuf + sizeof(struct ether_header));
struct sockaddr_ll socket_address;
char ifName[IFNAMSIZ];
int i, j, ret, sendsize=1488, packet_num=1000000000, offset = 0, port;
int fd; /* file descriptor for file to send */
struct sockaddr_in servaddr,cliaddr;
socklen_t clilen;
struct timeval t_start,t_end,t_now;
signal(SIGPIPE, SIG_IGN);
if (argc > 1)
port = atoi(argv[1]);
else
{
printf("Usage: TCPServer port rate");
exit(1);
}
if (argc > 2)
quota = atoi(argv[2]) / (1000000 / slotLength);
listenfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
listen(listenfd, 1);
for(;;)
{
clilen=sizeof(cliaddr);
sockfd = accept(listenfd,(struct sockaddr *)&cliaddr, &clilen);
printf("Accepted.\n");
//setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
gettimeofday(&t_start, NULL);
while (offset < packet_num)
{
if ((packet_num - offset) < quota)
{
quota = packet_num - offset;
}
while (sentInSlot < quota)
{
ret = send(sockfd, sendbuf, (quota - sentInSlot < 4096) ? quota - sentInSlot : 4096, 0);
if (ret <= 0)
{
printf("send fail\n");
slot = 1;
sentInSlot = 0;
offset = 0;
goto RESTART;
}
offset += ret;
sentInSlot = sentInSlot + ret;
}
gettimeofday(&t_now, NULL);
elapsedTime = (t_now.tv_sec-t_start.tv_sec)*1000000.0+(t_now.tv_usec-t_start.tv_usec);
//printf("sent %d, quota %d, packet_num %d, usleep %lf\n", offset, quota, packet_num, slotLength * slot - elapsedTime);
if (elapsedTime < slotLength * slot)
{
usleep((int)(slotLength * slot - elapsedTime));
}
sentInSlot = 0;
slot++;
}
RESTART:
close(sockfd);
gettimeofday(&t_end, NULL);
printf("%lf\n", (t_end.tv_sec-t_start.tv_sec)*1000.0+(t_end.tv_usec-t_start.tv_usec)/1000.0);
}
close(fd);
return 0;
}

View File

@ -1,20 +0,0 @@
CC = gcc
CFLAGS = -Wall
TARGET = UDPSender
OBJS = $(patsubst %.c, %.o, $(wildcard *.c))
HEADERS = $(wildcard *.h)
all: $(TARGET)
%.o: %.c $(HEADERS)
$(CC) $(CFLAGS) -c $< -o $@
.PRECIOUS: $(TARGET) $(OBJS)
$(TARGET): $(OBJS)
$(CC) $(OBJS) $(CFLAGS) -o $@
clean:
-rm -f *.o
-rm -f $(TARGET)

View File

@ -1,125 +0,0 @@
/*
* 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, either version 3 of the License, or
* (at your option) any later version.
*/
//#define _BSD_SOURCE
#include <arpa/inet.h>
//#include <linux/if_packet.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/ethernet.h>
//#include <socket.h>
//#include <netinet/ether.h>
//#include <sys/sendfile.h>
//#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <math.h>
#define ETH_P_IP 0x0800 /* Internet Protocol packet */
#define ETH_ALEN 6 /* from <net/ethernet.h> */
#define ETH_P_ALL 0x0003
//ca:e0:eb:61:b7:64
#define MY_DEST_MAC0 0xca
#define MY_DEST_MAC1 0xe0
#define MY_DEST_MAC2 0xeb
#define MY_DEST_MAC3 0x61
#define MY_DEST_MAC4 0xb7
#define MY_DEST_MAC5 0x64
#define DEFAULT_IF "wlan0"
#define BUF_SIZ 8192
int main(int argc, char *argv[])
{
int slotLength = 10000; // in microseconds
int quota = 1000000000; // Bytes per slot, default 1GB/slot
int sentInSlot = 0, slot = 1;
double elapsedTime;
double packetPerSlot;
struct iovec iov;
int sockfd, listenfd;
struct ifreq if_idx;
struct ifreq if_mac;
int tx_len = 0;
char sendbuf[BUF_SIZ];
struct ether_header *eh = (struct ether_header *) sendbuf;
struct iphdr *iph = (struct iphdr *) (sendbuf + sizeof(struct ether_header));
// struct sockaddr_ll socket_address;
char ifName[IFNAMSIZ];
int i, j, ret, sendsize=1458, packet_num=1000000000, offset = 0, port;
//int fd; /* file descriptor for file to send */
struct sockaddr_in servaddr,cliaddr;
socklen_t clilen;
struct timeval t_start,t_end,t_now;
signal(SIGPIPE, SIG_IGN);
if (argc > 1)
quota = atoi(argv[1]) / (1000000 / slotLength);
else
{
printf("Usage: UDPSender rate");
exit(1);
}
for (i = 0; i < sendsize; i++) sendbuf[i] = '0';
// fix packet size problem
packetPerSlot = ceil(((double)quota) / sendsize);
slotLength = (int)(packetPerSlot * sendsize / quota * slotLength);
quota = (int)packetPerSlot * sendsize;
//printf("TESThere1");
sockfd=socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=inet_addr("192.168.2.2");
servaddr.sin_port=htons(4444);
//printf("testhere1");
gettimeofday(&t_start, NULL);
while (offset < packet_num)
{
if ((packet_num - offset) < quota)
{
quota = packet_num - offset;
}
while (sentInSlot < quota)
{
ret = sendto(sockfd, sendbuf, sendsize, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
if (ret < sendsize)
{
printf("send fail\n");
usleep(100);
}
offset += ret;
sentInSlot = sentInSlot + ret;
}
//printf("here3");
gettimeofday(&t_now, NULL);
elapsedTime = (t_now.tv_sec-t_start.tv_sec)*1000000.0+(t_now.tv_usec-t_start.tv_usec);
//printf("sent %d, quota %d, packet_num %d, usleep %lf\n", offset, quota, packet_num, slotLength * slot - elapsedTime);
if (elapsedTime < slotLength * slot)
{
usleep((int)(slotLength * slot - elapsedTime));
}
sentInSlot = 0;
slot++;
}
//close(fd);
return 0;
}

View File

@ -1,20 +0,0 @@
CC = gcc
CFLAGS = -pie -fPIE
TARGET = UDPServer
OBJS = $(patsubst %.c, %.o, $(wildcard *.c))
HEADERS = $(wildcard *.h)
all: $(TARGET)
%.o: %.c $(HEADERS)
$(CC) $(CFLAGS) -c $< -o $@
.PRECIOUS: $(TARGET) $(OBJS)
$(TARGET): $(OBJS)
$(CC) $(OBJS) -Wall $(CFLAGS) -o $@
cp UDPServer ~/mobileRDMABeach/
clean:
-rm -f *.o
-rm -f $(TARGET)

View File

@ -1,149 +0,0 @@
/*
* 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, either version 3 of the License, or
* (at your option) any later version.
*/
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/ether.h>
#include <sys/sendfile.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#define ETH_P_IP 0x0800 /* Internet Protocol packet */
#define ETH_ALEN 6 /* from <net/ethernet.h> */
#define ETH_P_ALL 0x0003
#define MY_DEST_MAC0 0xba
#define MY_DEST_MAC1 0xf6
#define MY_DEST_MAC2 0xb1
#define MY_DEST_MAC3 0x71
#define MY_DEST_MAC4 0x09
#define MY_DEST_MAC5 0x64
#define DEFAULT_IF "wlan0"
#define BUF_SIZ 8192
int main(int argc, char *argv[])
{
int slotLength = 10000; // in microseconds
int quota = 1000000000; // Bytes per slot, default 1GB/slot
int sentInSlot = 0, slot = 1, defaulttimeperiod = 500000000;
double elapsedTime;
struct iovec iov;
int sockfd, listenfd;
struct ifreq if_idx;
struct ifreq if_mac;
int tx_len = 0;
char sendbuf[BUF_SIZ];
struct ether_header *eh = (struct ether_header *) sendbuf;
struct iphdr *iph = (struct iphdr *) (sendbuf + sizeof(struct ether_header));
struct sockaddr_ll socket_address;
char ifName[IFNAMSIZ];
int i, j, ret, sendsize=1458, packet_num=1000000000, offset = 0, port;
int fd; /* file descriptor for file to send */
struct sockaddr_in servaddr,cliaddr;
socklen_t clilen;
struct timeval t_start,t_end,t_now;
signal(SIGPIPE, SIG_IGN);
if (argc > 1)
port = atoi(argv[1]);
else
{
printf("Usage: UDPServer port rate");
exit(1);
}
if (argc > 2)
quota = atoi(argv[2]) / (1000000 / slotLength);
if (argc > 3)
defaulttimeperiod = atoi(argv[3])*1000000;
listenfd = socket(AF_INET, SOCK_DGRAM, 0);
// printf("listen");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
// listen(listenfd, 1);
// printf("bind");
for(;;)
{
clilen=sizeof(cliaddr);
printf("start receiving..\n");
ret = recvfrom(listenfd,sendbuf,4096, 0, (struct sockaddr *)&cliaddr, &clilen);
// printf("msg: %s\n",sendbuf);
if (sendbuf[0] == '0') {
printf("Accepted.\n");
gettimeofday(&t_start, NULL);
for (i = 0; i < BUF_SIZ; i++) sendbuf[i] = '0';
while (offset < packet_num)
{
if ((packet_num - offset) < quota)
{
quota = packet_num - offset;
}
while (sentInSlot < quota)
{
ret = sendto(listenfd, sendbuf, sendsize, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
if (ret < sendsize)
{
printf("send fail\n");
slot = 1;
sentInSlot = 0;
offset = 0;
goto RESTART;
}
offset += ret;
sentInSlot = sentInSlot + ret;
}
gettimeofday(&t_now, NULL);
elapsedTime = (t_now.tv_sec-t_start.tv_sec)*1000000.0+(t_now.tv_usec-t_start.tv_usec);
if (elapsedTime > defaulttimeperiod) {
slot = 1;
sentInSlot = 0;
offset = 0;
goto AUTOCLOSE;
}
//printf("sent %d, quota %d, packet_num %d, usleep %lf\n", offset, quota, packet_num, slotLength * slot - elapsedTime);
if (elapsedTime < slotLength * slot)
{
usleep((int)(slotLength * slot - elapsedTime));
}
sentInSlot = 0;
slot++;
}
// sockfd = accept(listenfd,(struct sockaddr *)&cliaddr, &clilen);
}
RESTART:
// close(sockfd);
gettimeofday(&t_end, NULL);
printf("%lf\n", (t_end.tv_sec-t_start.tv_sec)*1000.0+(t_end.tv_usec-t_start.tv_usec)/1000.0);
}
AUTOCLOSE:
gettimeofday(&t_end, NULL);
printf("%lf\n", (t_end.tv_sec-t_start.tv_sec)*1000.0+(t_end.tv_usec-t_start.tv_usec)/1000.0);
close(fd);
return 0;
}

View File

@ -1,21 +0,0 @@
CC = /media/Lucifer/android/lib/android-18-toolchain/bin/arm-linux-androideabi-gcc
#CC = gcc
CFLAGS = -pie -fPIE
TARGET = UDPServer_mobile
OBJS = $(patsubst %.c, %.o, $(wildcard *.c))
HEADERS = $(wildcard *.h)
all: $(TARGET)
%.o: %.c $(HEADERS)
$(CC) $(CFLAGS) -c $< -o $@
.PRECIOUS: $(TARGET) $(OBJS)
$(TARGET): $(OBJS)
$(CC) $(OBJS) -Wall $(CFLAGS) -o $@
# cp UDPServer ~/mobileRDMABeach/
clean:
-rm -f *.o
-rm -f $(TARGET)

View File

@ -1,149 +0,0 @@
/*
* 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, either version 3 of the License, or
* (at your option) any later version.
*/
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/ether.h>
#include <sys/sendfile.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#define ETH_P_IP 0x0800 /* Internet Protocol packet */
#define ETH_ALEN 6 /* from <net/ethernet.h> */
#define ETH_P_ALL 0x0003
#define MY_DEST_MAC0 0xba
#define MY_DEST_MAC1 0xf6
#define MY_DEST_MAC2 0xb1
#define MY_DEST_MAC3 0x71
#define MY_DEST_MAC4 0x09
#define MY_DEST_MAC5 0x64
#define DEFAULT_IF "wlan0"
#define BUF_SIZ 8192
int main(int argc, char *argv[])
{
int slotLength = 10000; // in microseconds
int quota = 1000000000; // Bytes per slot, default 1GB/slot
int sentInSlot = 0, slot = 1, defaulttimeperiod = 500000000;
double elapsedTime;
struct iovec iov;
int sockfd, listenfd;
struct ifreq if_idx;
struct ifreq if_mac;
int tx_len = 0;
char sendbuf[BUF_SIZ];
struct ether_header *eh = (struct ether_header *) sendbuf;
struct iphdr *iph = (struct iphdr *) (sendbuf + sizeof(struct ether_header));
struct sockaddr_ll socket_address;
char ifName[IFNAMSIZ];
int i, j, ret, sendsize=1458, packet_num=1000000000, offset = 0, port;
int fd; /* file descriptor for file to send */
struct sockaddr_in servaddr,cliaddr;
socklen_t clilen;
struct timeval t_start,t_end,t_now;
signal(SIGPIPE, SIG_IGN);
if (argc > 1)
port = atoi(argv[1]);
else
{
printf("Usage: UDPServer port rate");
exit(1);
}
if (argc > 2)
quota = atoi(argv[2]) / (1000000 / slotLength);
if (argc > 3)
defaulttimeperiod = atoi(argv[3])*1000000;
listenfd = socket(AF_INET, SOCK_DGRAM, 0);
// printf("listen");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
// listen(listenfd, 1);
// printf("bind");
for(;;)
{
clilen=sizeof(cliaddr);
printf("start receiving..\n");
ret = recvfrom(listenfd,sendbuf,4096, 0, (struct sockaddr *)&cliaddr, &clilen);
// printf("msg: %s\n",sendbuf);
if (sendbuf[0] == '0') {
printf("Accepted.\n");
gettimeofday(&t_start, NULL);
for (i = 0; i < BUF_SIZ; i++) sendbuf[i] = '0';
while (offset < packet_num)
{
if ((packet_num - offset) < quota)
{
quota = packet_num - offset;
}
while (sentInSlot < quota)
{
ret = sendto(listenfd, sendbuf, sendsize, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
if (ret < sendsize)
{
printf("send fail\n");
slot = 1;
sentInSlot = 0;
offset = 0;
goto RESTART;
}
offset += ret;
sentInSlot = sentInSlot + ret;
}
gettimeofday(&t_now, NULL);
elapsedTime = (t_now.tv_sec-t_start.tv_sec)*1000000.0+(t_now.tv_usec-t_start.tv_usec);
if (elapsedTime > defaulttimeperiod) {
slot = 1;
sentInSlot = 0;
offset = 0;
goto AUTOCLOSE;
}
//printf("sent %d, quota %d, packet_num %d, usleep %lf\n", offset, quota, packet_num, slotLength * slot - elapsedTime);
if (elapsedTime < slotLength * slot)
{
usleep((int)(slotLength * slot - elapsedTime));
}
sentInSlot = 0;
slot++;
}
// sockfd = accept(listenfd,(struct sockaddr *)&cliaddr, &clilen);
}
RESTART:
// close(sockfd);
gettimeofday(&t_end, NULL);
printf("%lf\n", (t_end.tv_sec-t_start.tv_sec)*1000.0+(t_end.tv_usec-t_start.tv_usec)/1000.0);
}
AUTOCLOSE:
gettimeofday(&t_end, NULL);
printf("%lf\n", (t_end.tv_sec-t_start.tv_sec)*1000.0+(t_end.tv_usec-t_start.tv_usec)/1000.0);
close(fd);
return 0;
}

View File

@ -1,6 +1,6 @@
/*
* Initial commit by Yibo @ Jul. 28, 2015
* Last update by Yanzi @ Sept. 27, 2016
* Last update by Yanzi @ Sept. 28, 2016
*/
#include <stdio.h>
@ -53,8 +53,8 @@ int main(int argc, char *argv[])
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(argv[2]);
servaddr.sin_port = htons(atoi(argv[3]));
servaddr.sin_addr.s_addr = inet_addr(argv[1]);
servaddr.sin_port = htons(atoi(argv[2]));
// if instrument to write to a file
if (argc > 4)

View File

@ -22,6 +22,8 @@ cd ../
# server sender
cd server_send_normaltcp && make
cd ../
cd server_send_normaludp && make
cd ../
# server receiver
cd server_recv_normaltcp && make
cd ../

View File

@ -0,0 +1,18 @@
#CC = /media/Lucifer/android/lib/android-18-toolchain/bin/arm-linux-androideabi-gcc
CC1 = arm-linux-androideabi-gcc
CC2 = gcc
CFLAGS = -pie -fPIE
TARGET1 = server_m_send_normaludp
TARGET2 = server_send_normaludp
all: $(TARGET1) $(TARGET2)
$(TARGET1): main.c
$(CC1) main.c -Wall $(CFLAGS) -o $@
$(TARGET2): main.c
$(CC2) main.c -Wall $(CFLAGS) -o $@
clean:
-rm -f $(TARGET1)
-rm -f $(TARGET2)

View File

@ -0,0 +1,244 @@
/*
* Initial commit by Yibo @ Jul. 28, 2015
* Last update by Yanzi @ Sept. 28, 2016
*/
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <net/if.h>
#include <netinet/ether.h>
#include <sys/sendfile.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <signal.h>
#define BUF_SIZ 65536
char isNumber(char number[])
{
int i = 0;
//checking for negative numbers
if (number[0] == '-')
i = 1;
for (; number[i] != 0; i++)
{
//if (number[i] > '9' || number[i] < '0')
if (!isdigit(number[i]))
return 0;
}
return 1;
}
int main(int argc, char *argv[])
{
// defaults
uint slotLength = 10000; // in microseconds, for bandwidth control
uint quota = 1000000000; // default bytes per slot, default 1GB/slot
uint sentInSlot = 0, slot = 1;
uint total_bytes_sent = 0;
// for timing
double elapsedTime;
struct timeval t_start, t_end, t_now;
// for socket
int fd; // file descriptor of file to send
int listenfd; // socket
char sendbuf[BUF_SIZ];
struct sockaddr_in servaddr, cliaddr;
socklen_t clilen;
// for misc
int ret;
int sendsize = 1460; // 1500 MTU - 20 IPv4 - 20 TCP
uint bytes2send = 0;
struct stat st;
unsigned char listenOnce = 0;
signal(SIGPIPE, SIG_IGN);
if (argc < 3)
{
printf("Usage: %s <port> <bytes2send/file2send> <[optional] bandwidth (bps)> <[optional] listenOnce ([0]/1)> <[optional] sendsize (bytes)>\n", argv[0]);
exit(0);
}
// set bandwidth
if (argc > 3)
quota = atoi(argv[3]) / 8 / (1000000 / slotLength);
// only one session
if (argc > 4)
listenOnce = atoi(argv[4]);
// set sendsize (if larger than 1460 will do packetization (fragmentation))
if (argc > 5)
sendsize = atoi(argv[5]);
// adjust slotLength to address packet size issue in the end
if ((quota % sendsize) > 0)
{
// printf("quota:%d,sendsize:%d,slotLength:%d\n", quota, sendsize, slotLength);
slotLength = (uint)((double)(quota / sendsize + 1) * sendsize / quota * slotLength);
quota = (quota / sendsize + 1) * sendsize;
// printf("quota:%d,sendsize:%d,slotLength:%d\n", quota, sendsize, slotLength);
}
// listen to socket
listenfd = socket(AF_INET, SOCK_DGRAM, 0);
// bind socket and listen
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(atoi(argv[1]));
bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
for (;;)
{
clilen = sizeof(cliaddr);
// wait for one client and accept it once found
ret = recvfrom(listenfd, sendbuf, 4096, 0, (struct sockaddr *)&cliaddr, &clilen);
if (ret <= 0)
{
fprintf(stderr, "! Fail to recv: ret:%d, err:%d; quiting..\n", ret, errno);
exit(1);
}
// check validity of udp transmission
if ((sendbuf[0] == '!') && (sendbuf[1] == '?') && (sendbuf[2] == '='))
{
printf("Accepted client at %s\n", inet_ntoa(cliaddr.sin_addr));
// get file size (bytes2send)
if (isNumber(argv[2]))
{
// set bytes to send
bytes2send = atoi(argv[2]);
// open file descriptor
fd = open("/dev/zero", O_RDONLY);
if (fd == -1)
{
fprintf(stderr, "! Unable to open /data/local/tmp/bigfile.\n");
exit(1);
}
}
else
{
// open file descriptor
fd = open(argv[2], O_RDONLY);
if (fd == -1)
{
fprintf(stderr, "! Unable to open file %s.\n", argv[2]);
exit(1);
}
fstat(fd, &st);
bytes2send = st.st_size;
printf("bytes2send:%d\n", bytes2send);
}
// clear total_bytes_sent to 0
total_bytes_sent = 0;
sentInSlot = 0;
slot = 1;
// start timing
gettimeofday(&t_start, NULL);
// start to send
while (total_bytes_sent < bytes2send)
{
if ((bytes2send - total_bytes_sent) < quota)
{
quota = bytes2send - total_bytes_sent;
}
// send in slots
while (sentInSlot < quota)
{
// printf(
// "before: total_bytes_sent %d, sentInSlot %d, quota - sentInSlot %d\n",
// total_bytes_sent, sentInSlot, quota - sentInSlot);
read(fd, sendbuf, (quota - sentInSlot < sendsize) ? (quota - sentInSlot) : sendsize);
ret = sendto(
listenfd, sendbuf,
(quota - sentInSlot < sendsize) ? (quota - sentInSlot) : sendsize,
0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
if (ret <= 0)
{
fprintf(stderr, "! Fail to send: ret:%d, err:%d; wait for 100us..\n", ret, errno);
total_bytes_sent = bytes2send + 1;
break;
}
total_bytes_sent += ret;
sentInSlot += ret;
// printf(
// "after: total_bytes_sent %d, sentInSlot %d, quota - sentInSlot %d\n",
// total_bytes_sent, sentInSlot, quota - sentInSlot);
}
// workaround to break the while loop when error happens
if (total_bytes_sent > bytes2send)
{
break;
}
// control bandwidth
gettimeofday(&t_now, NULL);
elapsedTime = (t_now.tv_sec - t_start.tv_sec) * 1000000.0 + (t_now.tv_usec - t_start.tv_usec);
if (elapsedTime < slotLength * slot)
{
// printf(
// "sent %d, quota %d, bytes2send %d, usleep %lfus\n",
// total_bytes_sent, quota, bytes2send, slotLength * slot - elapsedTime);
usleep((int)(slotLength * slot - elapsedTime));
}
sentInSlot = 0;
++slot;
}
// end timing
if (total_bytes_sent == bytes2send)
{
gettimeofday(&t_end, NULL);
elapsedTime = (t_end.tv_sec - t_start.tv_sec) + (t_end.tv_usec - t_start.tv_usec) / 1000000.0;
printf(
"sent(bytes):%d\nduration(s):%lf\nthroughput(bps):%lf\n",
total_bytes_sent, elapsedTime, total_bytes_sent * 8 / elapsedTime);
ret = sendto(listenfd, "=?!\n", 4, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
if (ret <= 0)
{
fprintf(stderr, "! Unable to initialize data transfer. errno:%d.\n", errno);
close(fd);
close(listenfd);
exit(1);
}
}
else
{
fprintf(stderr, "! error: total_bytes_sent > bytes2send\n");
}
close(fd);
if (listenOnce)
break;
}
}
close(listenfd);
return 0;
}