1 分别用gcc和交叉编译将以下c文件编译成两个可执行程序
2 测试 TCP
pc发送 -> 开发板
pc端输入的参数 :./pc -t 0 -s 192.168.110.175 -nh #此处ip为目标板ip
开发板参数: ./kf -t 0 -r 192.168.110.170 -nh #ip为pc ip
开发板发送 -> pc
pc端输入的参数 :./pc -t 0 -r 192.168.110.175 -nh #此处ip为目标板ip
开发板参数: ./kf -t 0 -s 192.168.110.170 -nh #ip为pc ip
3 测试UDP
pc发送 -> 开发板
pc端输入的参数 :./pc -t 1 -s 192.168.110.175 -v #此处ip为目标板ip
开发板参数: ./kf -t 1 -r 192.168.110.170 -v #ip为pc ip
开发板发送 -> pc
pc端输入的参数 :./pc -t 0 -r 192.168.110.175 -nh #此处ip为目标板ip
开发板参数: ./kf -t 0 -s 192.168.110.170 -nh #ip为pc ip
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXHOSTNUM 128 // up to 128 network terminals
#define NET_DEV_NAME "eth0"
//#define UDELAY(A) udelay-s(A)
#define UDELAY(A) usleep(A)
typedef struct _Net_Host
{
char HostIPAddress[32];
// char HostName[64]; FIXME: IPV6 support
} HOST, *PHOST;
HOST g_HostList[MAXHOSTNUM];
/* default values */
unsigned int wait_usec = 10; // wait usec(s)
int cacu_wait_usec = 500; // caculation thread wait usec(s)
unsigned int cont_packets = 2; // continuous packets
unsigned int block_size = 2048; // data block size
unsigned int manual_ctl = 0; // manual control
unsigned int verbose = 0; // verbose message
unsigned int is_send = 0; // 0: receive; 1: send
unsigned int c_check = 1; // consistency check
unsigned int protocol_type = 0; // 0: tcp; 1: udp, 2: multicast
unsigned int block_mode = 1; // 0: non-block mode; 1: block mode
unsigned int tcp_header = 1; // 0: no header; 1: use header
unsigned short port_num = 8913;
unsigned long total_pack = 0;
char multicast_addr[16] = "230.0.0.1\0";
char strDstIP[32] = "127.0.0.1\0";
sem_t sem_stop;
const char HEADER[] = "NETHDR ";
typedef struct _Net_Header
{
char Header[sizeof(HEADER)];
int nDataSize;
} NETHEADER, *PNETHEADER;
void udelay_s(int);
int addHost(HOST *);
void InitHostList(void);
void record_time_stamp(void);
unsigned int SendData(int, unsigned char *, unsigned int);
int RecvData(int, unsigned char *, int);
int get_local_ipaddr(int);
int is_next_cmd(int, char *);
int cmd_parse(int, char **);
int init_client(char *);
int init_server();
void tcp_recv(int);
void tcp_send(int);
int udp_send();
int udp_recv();
int deleteHost(HOST *);
int findHost(HOST *);
static void sig_int(int);
//
static void sig_int(int sig_no)
{
exit(0);
}
void udelay_s(int usec)
{
fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
FD_SET(0, &rfds); /* If user press 'Enter' on the terminal, fd '0' will be set.
So select() seems useless !! */
tv.tv_sec = 0;
tv.tv_usec = usec;
select(1, &rfds, NULL, NULL, &tv);
}
void InitHostList(void)
{
memset(g_HostList, 0, sizeof(g_HostList));
}
int addHost(HOST * pHost)
{
int index = findHost(pHost);
if (MAXHOSTNUM != index)
{
return index;
}
for (index=0;index
{
if (g_HostList[index].HostIPAddress[0]=='\0')
{
strcpy(g_HostList[index].HostIPAddress,pHost->HostIPAddress);
break;
}
}
return index;
}
int deleteHost(HOST * pHost)
{
int index = findHost(pHost);
if (MAXHOSTNUM != index)
{
g_HostList[index].HostIPAddress[0]='\0';
}
return index;
}
int findHost(HOST * pHost)
{
int index;
for (index=0;index
{
if (strcmp(g_HostList[index].HostIPAddress,pHost->HostIPAddress)==0)
{
break;
}
}
return index;
}
void record_time_stamp(void)
{
time_t timep;
char *asciitime;
time(&timep);
asciitime = ctime(&timep);
printf("\n--------------------------\n%s--------------------------\n", asciitime);
return;
}
unsigned int SendData(int sockStream, unsigned char *pData, unsigned int nSize)
{
int nDataRemain = nSize;
unsigned char *pBufPos = pData;
while (1) {
int nWrite = send(sockStream, pBufPos, nDataRemain, 0);
if (nWrite <= 0) {
printf("send() error: %s\n", strerror(errno));
return -1;
}
nDataRemain -= nWrite;
if (nDataRemain <= 0) {
if (nDataRemain < 0)
printf("WARNING: send() more than wanted!\n");
break;
}
pBufPos += nWrite;
}
return nSize;
}
int RecvData(int sockStream, unsigned char *buf, int nSize)
{
int nDataRemain = nSize;
unsigned char *pBufPos = buf;
while (1) {
int nRead = recv(sockStream, pBufPos, nDataRemain, 0);
if (nRead < 0) {
printf("recv() error: %s\n", strerror(errno));
return -1;
}
else if (nRead == 0) {
printf("receive emtpy data\n");
return -1;
}
nDataRemain -= nRead;
if (nDataRemain <= 0) {
if (nDataRemain < 0)
printf("WARNING: recv() more than wanted!\n");
break;
}
pBufPos += nRead;
}
return nSize;
}
int get_local_ipaddr(int socket_fd)
{
struct ifreq ifr;
struct in_addr in_address;
struct sockaddr_in *sin_ptr;
char *str_in_addr;
/* record begin time */
record_time_stamp();
/* record version & parameters */
if (protocol_type == 0)
printf("tcp\n");
if (protocol_type == 1)
printf("udp unicast\n");
if (protocol_type == 2)
printf("udp multicast\n");
if (block_mode)
printf("block mode\n");
else
printf("non-block mode\n");
if (tcp_header)
printf("use tcp header\n");
else
printf("do _NOT_ use tcp header\n");
printf("port num: %d\n", port_num);
printf("block size: %d\nverbose: %d\n", block_size, verbose);
if (is_send)
printf("send\ncontinuous packets: %d\nwait usec: %d\n\n",
cont_packets, wait_usec);
else
printf("receive\nconsistency check: %d\n\n", c_check);
if (manual_ctl)
printf("manual control\n");
if (protocol_type == 2)
printf("dest addr: %s\n", multicast_addr);
else
printf("dest addr: %s\n", strDstIP);
/* get local IP address */
// FIXME: multi-netcard support
strcpy(ifr.ifr_name, NET_DEV_NAME);
if (ioctl(socket_fd, SIOCGIFADDR, &ifr) >= 0) {
sin_ptr = (struct sockaddr_in *) &ifr.ifr_addr;
in_address = sin_ptr->sin_addr;
}
str_in_addr = inet_ntoa(in_address);
printf("Local IP address: %s\n", str_in_addr);
return 0;
}
int is_next_cmd(int i, char *argv)
{
if (strcmp(argv, "-h") == 0) return 1;
if (strcmp(argv, "-s") == 0) return 1;
if (strcmp(argv, "-r") == 0) return 1;
if (strcmp(argv, "-w") == 0) return 1;
if (strcmp(argv, "-c") == 0) return 1;
if (strcmp(argv, "-b") == 0) return 1;
if (strcmp(argv, "-N") == 0) return 1;
if (strcmp(argv, "-m") == 0) return 1;
if (strcmp(argv, "-v") == 0) return 1;
if (strcmp(argv, "-t") == 0) return 1;
if (strcmp(argv, "-nb") == 0) return 1;
if (strcmp(argv, "-nh") == 0) return 1;
if (strcmp(argv, "-cw") == 0) return 1;
if (strcmp(argv, "-p") == 0) return 1;
return 0;
}
int cmd_parse(int argc, char *argv[])
{
int i;
for (i = 1; i < argc; ++i) {
if ((strcmp(argv[i], "-h") == 0) || (strcmp(argv[i], "--help") == 0)) {
printf("-b\tBlock size (bytes). default 2048\n");
printf("-c\tContinuous packets. loop for -c packets to be sent. default 2\n");
printf("-cw\tCaculation thread waiting time. default 500(us)\n");
printf("-m\tManually send. wait user to press enter\n");
printf("-N\tNO data packets consistency check. default check\n");
printf("-nb\tset to non-block mode\n");
printf("-nh\tNo tcp header\n");
printf("-p\tPort. (default 8913)\n");
printf("-r\tReceive packets from xxx.xxx.xxx.xxx(Multicast cast). (default 230.0.0.1)\n");
printf("-s\tsend to xxx.xxx.xxx.xxx (TCP/UDP destination address).\n");
printf("-t\tProtocol type. 0: tcp, 1: udp, 2: multicast. defalut tcp\n");
printf("-v\tVerbose.\n");
printf("-w\tWait time (usec). default 10\n");
return 1;
}
else if (strcmp(argv[i], "-nc") == 0) {
c_check = 0;
}
else if (strcmp(argv[i], "-nb") == 0) {
block_mode = 0;
}
else if (strcmp(argv[i], "-nh") == 0) {
tcp_header = 0;
}
else if (strcmp(argv[i], "-v") == 0) {
verbose = 1;
}
else if (strcmp(argv[i], "-t") == 0) {
++i;
if (i >= argc) {
printf("Too few argument (-t)\n");
return -1;
}
protocol_type = atoi(argv[i]);
if (protocol_type < 0 || protocol_type > 2) {
printf("Unknow protocol!!!\n");
return -1;
}
}
else if (strcmp(argv[i], "-b") == 0) {
++i;
if (i >= argc) {
printf("Too few argument (-b)\n");
return -1;
}
block_size = atoi(argv[i]);
if (block_size <= 0)
block_size = 2048;
}
else if (strcmp(argv[i], "-cw") == 0) {
++i;
if (i >= argc) {
printf("Too few argument (-cw)\n");
return -1;
}
cacu_wait_usec = atoi(argv[i]);
}
else if (strcmp(argv[i], "-s") == 0) {
is_send = 1;
++i;
if (i < argc) {
if(!is_next_cmd(i, argv[i])) {
strcpy(strDstIP, argv[i]);
strcpy(multicast_addr, argv[i]);
}
else
i--;
}
}
else if (strcmp(argv[i], "-r") == 0) {
is_send = 0;
++i;
if (i < argc) {
if(!is_next_cmd(i, argv[i]))
strcpy(multicast_addr, argv[i]);
else
i--;
}
}
else if (strcmp(argv[i], "-w") == 0) {
++i;
if (i >= argc) {
printf("Too few argument (-w)\n");
return -1;
}
wait_usec = atoi(argv[i]);
if (wait_usec > 10000000)
wait_usec = 10;
}
else if (strcmp(argv[i], "-c") == 0) {
++i;
if (i >= argc) {
printf("Too few argument (-c)\n");
return -1;
}
cont_packets = atoi(argv[i]);
}
else if (strcmp(argv[i], "-p") == 0) {
++i;
if (i >= argc) {
printf("Too few argument (-p)\n");
return -1;
}
port_num = atoi(argv[i]);
}
else if (strcmp(argv[i], "-m") == 0) {
manual_ctl = 1;
}
else {
printf("Unknow options \"%s\"\n", argv[i]);
return -1;
}
}
return 0;
}
int init_client(char *strDstIP)
{
struct sockaddr_in client_addr;
int sockfd_c;
int mode = 0;
sockfd_c = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd_c < 0) {
printf("socket() error: %s\n", strerror(errno));
return -1;
}
get_local_ipaddr(sockfd_c);
bzero(&client_addr, sizeof(struct sockaddr_in));
client_addr.sin_family = AF_INET;
client_addr.sin_addr.s_addr = inet_addr(strDstIP);
client_addr.sin_port = htons(port_num);
if (connect(sockfd_c, (struct sockaddr *) &client_addr, sizeof(struct sockaddr_in)) < 0) {
printf("connect() error: %s\n", strerror(errno));
}
else {
ioctl(sockfd_c, FIONBIO, &mode);
tcp_send(sockfd_c);
}
close(sockfd_c);
return 0;
}
int init_server()
{
struct sockaddr_in serveraddr;
struct sockaddr_in clientaddr;
int clientaddrlen = sizeof(clientaddr);
int mode = 0;
int sockfd_s;
int newsockfd;
sockfd_s = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd_s < 0) {
printf("socket() error: %s\n", strerror(errno));
return -1;
}
get_local_ipaddr(sockfd_s);
bzero(&serveraddr, sizeof(struct sockaddr_in));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(port_num);
if (bind(sockfd_s, (struct sockaddr *) &serveraddr, sizeof(struct sockaddr_in)) < 0) {
printf("bind() error: %s\n", strerror(errno));
return -2;
}
if (listen(sockfd_s, 8) < 0) {
printf("listen() error: %s\n", strerror(errno));
return -3;
}
while (1) {
if ((newsockfd = accept(sockfd_s, (struct sockaddr *) &clientaddr, &clientaddrlen)) < 0) {
printf("accept() error: %s\n", strerror(errno));
continue;
}
if (clientaddr.sin_family == AF_INET) {
printf("connection: IP addr: %s, port: %d\n",
(char *) inet_ntoa(clientaddr.sin_addr),
ntohs(clientaddr.sin_port));
}
else {
printf("unknown protocol type!\n");
close(newsockfd);
continue;
}
ioctl(newsockfd, FIONBIO, &mode);
tcp_recv(newsockfd);
close(newsockfd);
//break; // force exit. mark this for loop
}
close(sockfd_s);
return 0;
}
void tcp_recv(int sockStream)
{
NETHEADER NetHeader;
unsigned char *buf = NULL;
struct timeval time1, time2;
int byte_val = 0;
int buf_size;
int good_count = 0;
int i;
int nNext;
int data_recv = 0;
int bytes_recv;
double fMbps;
long time_interval;
int nRead;
int err_count = 0;
if (tcp_header) {
buf_size = 2048;
buf = (unsigned char *) malloc(2048);
}
else {
buf_size = block_size;
buf = (unsigned char *) malloc(buf_size);
}
if (buf == NULL) {
printf("Out of memory when TCP receiving data!\n");
return;
}
if (verbose)
printf("Net header size %d\n", sizeof(NETHEADER));
gettimeofday(&time1, 0);
while (1) {
// recv header
if (tcp_header) {
nRead = recv(sockStream, &NetHeader, sizeof(NETHEADER), 0);
if (nRead < 0) {
printf("recv() error: %s\n", strerror(errno));
break;
}
else if (nRead == 0) {
printf("receive empty data\n");
break;
}
else if (nRead < sizeof(NETHEADER)) {
printf("Wrong header size %d\n", nRead);
if (err_count < 10) {
err_count++;
continue;
}
else {
printf("Wrong header size %d, exit receiving for this time ...\n", nRead);
err_count = 0;
break;
}
}
else if (NetHeader.Header[sizeof(HEADER) - 1] != '\0') {
printf("Wrong header - no header terminal [%c %c %c %c]\n", NetHeader.Header[0], NetHeader.Header[1], NetHeader.Header[2], NetHeader.Header[3]);
continue;
}
else if (strcmp(NetHeader.Header, HEADER) != 0) {
printf("Wrong header %s\n", NetHeader.Header);
continue;
}
else {
if (verbose)
printf("Remain data size %d\n", NetHeader.nDataSize);
}
if (NetHeader.nDataSize != buf_size) {
/* allocate buffer for the biggest packet, and size is specified by HEADER */
if (NetHeader.nDataSize > buf_size) {
free(buf);
buf = (unsigned char *) malloc(NetHeader.nDataSize);
if (buf == NULL) {
printf("Out of memory when TCP receiving data!\n");
return ;
}
}
buf_size = ntohl(NetHeader.nDataSize); /* different arch platform!!! ntohl() */
}
} // if (tcp_header)
/* receive data */
if ((bytes_recv = RecvData(sockStream, buf, buf_size)) <= 0) {
printf("sth wrong with RecvData()\n");
break;
}
else {
data_recv += bytes_recv;
if (data_recv >= 0x640000) { // 50/8 MB
gettimeofday(&time2, 0);
time_interval = 1000000*(time2.tv_sec - time1.tv_sec) + (time2.tv_usec - time1.tv_usec);
fMbps = (((double) data_recv)/((double) time_interval))*8.0;
// if (verbose)
printf("Recved %u bytes in %ld usec. %f Mbps\n", data_recv, time_interval, fMbps);
// reset
gettimeofday(&time1, 0);
data_recv = 0;
}
if (bytes_recv != buf_size) {
printf("Received data size(%d) NOT as wanted(%d)\n", bytes_recv, buf_size);
}
else {
// handle recved data
if (buf[0] != byte_val) {
if (verbose)
printf("Received data(%d) NOT as wanted(%d)\nGood count %d\n", buf[0], byte_val, good_count);
good_count = 0;
byte_val = buf[0];
}
else {
++good_count;
if (verbose)
printf("Received data %d\n", buf[0]);
}
if (c_check) {
for (i = 1; i < buf_size; ++i) {
if (buf[i] != byte_val) {
if (verbose)
printf("Recv data(%d) NOT consistent(%d)\n", buf[i], byte_val);
}
}
} // if (c_check)
if (++byte_val >= 256) {
byte_val = 0;
}
}
}
} // while (1)
/* should never goes here. Just for safety! */
free(buf);
return;
}
void tcp_send(int sockStream)
{
NETHEADER NetHeader;
unsigned char *buf = NULL;
int byte_val = 0;
int i;
int data_sent = 0;
int bytes_sent; // hold returned value
long time_interval; // not longer than 35 minutes, or message useless
double fMbps;
struct timeval time1, time2;
if (tcp_header) {
strcpy(NetHeader.Header, HEADER);
NetHeader.nDataSize = htonl(block_size); /* platform arch different !!! htonl() */
printf("%s\n", NetHeader.Header);
if (verbose)
printf("Net header size %d\n", sizeof(NETHEADER));
}
buf = (unsigned char *) malloc(block_size);
if (buf == NULL) {
printf("Out of memory when sending data!\n");
return;
}
gettimeofday(&time1, 0);
while (1) {
for (i = 0; i < cont_packets; ++i) {
if (tcp_header) {
// send header
if ((bytes_sent = send(sockStream, &NetHeader, sizeof(NETHEADER), 0)) <= 0) {
printf("send() error: %s\n", strerror(errno));
break; // cause a new send
}
data_sent += bytes_sent;
}
// prepare data to send
memset(buf, byte_val, block_size);
byte_val += 1;
if (byte_val >= 256)
byte_val = 0;
// send data
if ((bytes_sent = SendData(sockStream, buf, block_size)) <= 0) {
/* error should be handled in SendData() */
printf("sth wrong with SendData()\n");
break;
}
else {
data_sent += bytes_sent;
if (data_sent >= 0x640000) { // 50/8 MB
gettimeofday(&time2, 0);
time_interval = 1000000*(time2.tv_sec - time1.tv_sec) + (time2.tv_usec - time1.tv_usec);
fMbps = (((double) data_sent)/((double) time_interval))*8.0;
if (verbose)
printf("Sent %u bytes in %ld usec. %f Mbps\n", data_sent, time_interval, fMbps);
// reset
gettimeofday(&time1, 0);
data_sent = 0;
}
}
if (manual_ctl)
printf("Send packet index %d.\n", buf[0]);
} // for
if (manual_ctl) {
printf("Press ENTER to continue sending...\n");
getchar();
}
else {
UDELAY(wait_usec);
}
}
/* should never goes here. Just for safety! */
free(buf);
return;
}
int udp_send()
{
struct sockaddr_in dest_addr;
struct timeval time1, time2;
unsigned char *buf;
int byte_val = 0;
int addrlen = sizeof(struct sockaddr_in);
int i;
int data_sent = 0;
int bytes_sent; // hold returned value
long time_interval; // not longer than 35 minutes, or message useless
double fMbps;
int sockfd;
struct sockaddr_in i_addr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
printf("udp_send(): socket() error: %s\n", strerror(errno));
return -1;
}
get_local_ipaddr(sockfd);
bzero(&i_addr, sizeof(struct sockaddr_in));
i_addr.sin_family = AF_INET;
i_addr.sin_addr.s_addr = htonl(INADDR_ANY);
i_addr.sin_port = htons(port_num);
if (bind(sockfd, (struct sockaddr *) &i_addr, sizeof(struct sockaddr_in)) < 0) {
printf("bind() error: %s\n", strerror(errno));
return -2;
}
/* destination */
bzero(&dest_addr, sizeof(struct sockaddr_in));
dest_addr.sin_family = AF_INET;
if (protocol_type == 1)
dest_addr.sin_addr.s_addr = inet_addr(strDstIP);
else if (protocol_type == 2)
dest_addr.sin_addr.s_addr = inet_addr(multicast_addr);
else {
printf("should never goes here 1\n");
return -3;
}
dest_addr.sin_port = htons(port_num);
if (NULL == (buf = (unsigned char *) malloc(block_size))) {
printf("Out of memory when sending data\n");
return -4;
}
/* set to NON block mode */
if (!block_mode) {
int mode = 1;
ioctl(sockfd, FIONBIO, &mode);
}
gettimeofday(&time1, 0);
while (1) {
for (i = 0; i < cont_packets; ++i) {
memset(buf, byte_val, block_size);
//fill_buf(buf, byte_val, block_size); // cy: for 9118 dbg
byte_val += 1;
if (byte_val >= 256)
byte_val = 0;
if ((bytes_sent = sendto(sockfd, buf, block_size, 0, (struct sockaddr*) &dest_addr, addrlen)) <= 0) {
printf("sendto() error: %s. probably destination addr err\n", strerror(errno));
break;
}
else {
total_pack++;
data_sent += bytes_sent;
if (data_sent >= 0x640000) { // 50/8 MB
gettimeofday(&time2, 0);
time_interval = 1000000*(time2.tv_sec - time1.tv_sec) + (time2.tv_usec - time1.tv_usec);
fMbps = (((double) data_sent)/((double) time_interval))*8.0;
if (verbose)
printf("Sent %u bytes in %ld usec. %f Mbps\n", data_sent, time_interval, fMbps);
// reset
gettimeofday(&time1, 0);
data_sent = 0;
}
if (manual_ctl)
printf("Send packet index %d.\n", buf[0]);
}
} // for
if (manual_ctl) {
printf("Press ENTER to continue sending...\n");
getchar();
}
else
if (wait_usec)
UDELAY(wait_usec);
} // while (1)
free(buf);
return 0;
}
int udp_recv()
{
struct sockaddr_in src_addr;
struct timeval time1, time2;
unsigned char *buf;
int addrlen = sizeof(struct sockaddr_in);
int nGoodCount[MAXHOSTNUM] = {0};
int data_recv = 0;
int bytes_recv; // hold returned value of recv()
long time_interval; // not longer than 35 minutes, or message useless
double fMbps;
HOST host;
int nHostIndex;
int byte_val[MAXHOSTNUM] = {0};
//int nNext;
int i;
int sockfd;
struct sockaddr_in i_addr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
printf("udp_recv(): socket() error: %s\n", strerror(errno));
return -1;
}
get_local_ipaddr(sockfd);
bzero(&i_addr, sizeof(struct sockaddr_in));
i_addr.sin_family = AF_INET;
i_addr.sin_addr.s_addr = htonl(INADDR_ANY);
i_addr.sin_port = htons(port_num);
if (bind(sockfd, (struct sockaddr *) &i_addr, sizeof(struct sockaddr_in)) < 0) {
printf("bind() error: %s\n", strerror(errno));
return -2;
}
/* join multicast grouop */
if (protocol_type == 2) {
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr(multicast_addr);
mreq.imr_interface.s_addr = INADDR_ANY;
if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mreq, sizeof(struct ip_mreq)) < 0) {
printf("Unsupport multicast. error: %s\n", strerror(errno));
return -3;
}
printf("Support multicast.\n");
unsigned char TTL = 1;
if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&TTL, sizeof(TTL)) < 0) {
printf("Set TTL error: %s\n", strerror(errno));
return -4;
}
}
InitHostList();
if (NULL == (buf = (unsigned char *) malloc(block_size))) {
printf("Out of memory when receiving data!\n");
return -5;
}
/* set to NON block mode */
if (!block_mode) {
int mode = 1;
ioctl(sockfd, FIONBIO, &mode);
}
gettimeofday(&time1, 0);
while (1) {
if ((bytes_recv = recvfrom(sockfd, buf, block_size, 0, (struct sockaddr*) &src_addr, &addrlen)) <= 0) {
printf("recvfrom() error: %s\n", strerror(errno));
}
else {
total_pack++;
// speed log
data_recv += bytes_recv;
if (data_recv >= 0x640000) { // 50/8 MB
gettimeofday(&time2, 0);
time_interval = 1000000*(time2.tv_sec - time1.tv_sec) + (time2.tv_usec - time1.tv_usec);
fMbps = (((double) data_recv)/((double) time_interval))*8.0;
if (verbose)
printf("Recved %u bytes in %ld usec. %f Mbps\n", data_recv, time_interval, fMbps);
// reset
gettimeofday(&time1, 0);
data_recv = 0;
}
if (bytes_recv != block_size) {
printf("Received data size(%d) NOT as wanted(%d)\n", bytes_recv, block_size);
}
else {
strcpy(host.HostIPAddress, inet_ntoa(src_addr.sin_addr));
// printf("Recv from %s\n", host.HostIPAddress);
nHostIndex = addHost(&host);
if (nHostIndex >= MAXHOSTNUM) {
printf("Host list full\n");
continue;
}
// else {
// printf("Host list index %d\n", nHostIndex);
// }
//nNext = byte_val[nHostIndex] + 1;
//nNext = byte_val[nHostIndex];
//if (nNext >= 256)
// nNext = 0;
if (buf[0] != byte_val[nHostIndex]) {
if (verbose)
printf("Received data(%d) NOT as wanted(%d) from %s\nGood count %d\n", buf[0], byte_val[nHostIndex], g_HostList[nHostIndex].HostIPAddress, nGoodCount[nHostIndex]);
nGoodCount[nHostIndex] = 0;
byte_val[nHostIndex] = buf[0]; // update byte_val for c_check
}
else {
++nGoodCount[nHostIndex];
//if (verbose)
// printf("Received data %d from %s\n", buf[0], g_HostList[nHostIndex].HostIPAddress);
}
if (c_check) {
for (i = 1; i < block_size; ++i) {
if (buf[i] != byte_val[nHostIndex]) {
if (verbose)
printf("Recv data(%d) NOT consistent(%d) from %s\n", buf[i], byte_val[nHostIndex], g_HostList[nHostIndex].HostIPAddress);
阅读(1189) | 评论(0) | 转发(0) |