这里向用户介绍一个让ZLIP同时处理4个TCP连接(2个客户端、2个服务器端)、两个UDP连接和PING的程序。该演示将让用户初步了解ZLIP的功能。
按照开发运行新程序一节的步骤将UseZLIPLib下载到开发板中。
1. 打开sscomv2.0,波特率为19200;重新打开开发板电源,串口显示:
test_complex_send_recv test...
TODO: listen at 192.168.0.3:1024, and press key
press any key...
按以上要求让SocketTestDlg在TCP1024端口监听,然后向串口发送一个任意字符,作为“press key”。
串口和SocketTestDlg提示信息表明开发板向PC机发起了两个连接并建立了两个连接。
2. 发送文件:由于发送文件需要较长时间,需要再运行一个SocketTestDlg程序,并使用Client Connect向192.168.0.2:1024发起连接。
串口输出和SocketTestDlg提示信息表明连接建立。SocketTestDlg中的send file选择为“\工具\SocketTestDlg\send_test_file\1_75M”,Send
Mode选择Send File。点击ClientSend 开始发送文件。
3. 采用ping 192.168.0.2 –t,PING开发板。
4. 做如下的操作(为了演示ZLIP的同时处理多个连接的能力,以下操作在上面的数据发送和PING同时进行):
(1) 采用ServerSend向板子发送“a”字符,串口显示“sc0 a”和“sc1 a”表明板子上的两个TCP客户端都收到数据“a”。
(2) 采用ClienConnect向板子的192.168.0.2:1024发起连接,可建立连接。串口显示“lisen 1 accept”,表明连接建立。连接建立以后采用ClientSend发送“b”,服务器端将收到回馈数据“b”。
(3) SocketTestDlg创建UDP后,向UDP 192.168.0.2:1024和UDP 192.168.0.2:1025分别发送“c和发送“d”字符,串口显示“su0
recv2:c”和“su1 recv2:d”表明板子上的两个UDP实例也收到了数据。
5. 当第二步“发送文件”完成后,点击ClientClose关闭连接,此时串口和SocketTestDlg提示消息如下:
图中显示了接收方(串口显示的数据)和发送方(SocketTestDlg显示的数据)的数据校验和一致,表明发送数据无误。
在该演示例子中,演示了ZLIP同时处理4个TCP连接(2个作为客户端、2个作为服务器端)、两个UDP连接和PING功能的能力。
以上例子的代码如下(该代码读者可稍作了解,详细的分析需参考Socket API):
void test_complex_send_recv()
{
/* 2 listen, 2 connect, 4 accpeted 2 udp,
and ping same time
server:
listen and send any data
connect and send big data
connect to 1025, 1024 at any time.
send any data to udp port
ping all the time.*/
SOCKET sl[2], sc[2], sa[4], su[2];
struct sockaddr_in src_addr, dest_addr, get_addr;
fd_set r, w;
zl_u32 len, val;
int accept_cnt, i, j;
int sc0_conn=FALSE, sc1_conn=FALSE;
int ret;
zl_u32 total_recv=0;
zl_u32 file_sum = 0;
zl_u8 c=0;
printf("\ntest_complex_send_recv test...");
if((sl[0] = socket(PF_INET, SOCK_STREAM, 0))
== SOCK_ERR)
return;
if((sl[1] = socket(PF_INET, SOCK_STREAM, 0))
== SOCK_ERR)
return;
if((sc[0] = socket(PF_INET, SOCK_STREAM, 0))
== SOCK_ERR)
return;
if((sc[1] = socket(PF_INET, SOCK_STREAM, 0))
== SOCK_ERR)
return;
if((su[0] = socket(PF_INET, SOCK_DGRAM, 0))
== SOCK_ERR)
return;
if((su[1] = socket(PF_INET, SOCK_DGRAM, 0))
== SOCK_ERR)
return;
/* listen */
src_addr.sin_addr = ipadsdr;
src_addr.sin_port = 1024;
dest_addr.sin_addr = DestIP;
dest_addr.sin_port = 1024;
len = sizeof(struct sockaddr_in);
printf("\nTODO: listen at %s:%d, and
press key",inet_ntoa(&dest_addr),dest_addr.sin_port);
wait_key();
printf("\nTODO: do any of the following
action, even at same time( if any socket closed by peer exit):"
"\n1. Peer listen
socket send any data to us."
"\n2. peer connect
to %s:%d or 1025 at any time (max two socket, NOTE by two TestSocket
app)and send big data."
"\n3. send any
UDP data to udp %s:%d or 1025."
"\n4. ping at
any time.",inet_ntoa(&src_addr),src_addr.sin_port,inet_ntoa(&src_addr),src_addr.sin_port);
printf("\nEXPECT: print \"sc0 and
sc1 connection establish\", and correspond to each action:"
"\n1. print \"s0
or s1 receive\" and peer receive the data sent"
"\n2. print \"listen
accept\". print \"sa receive\". print the result and
len of file sent by connection 0, peer will receive
some data from sa[0] for every receive;sa[1] will send what received."
"\n3. print \"su0
or su1 recevie data\" and peer receive the same data"
"\n4. ping success.");
/* listen and udp */
if(bind(sl[0], &src_addr, len) == SOCK_ERR)
return;
if(bind(su[0], &src_addr, len) == SOCK_ERR)
return;
src_addr.sin_port = 1025;
if(bind(sl[1], &src_addr, len) == SOCK_ERR)
return;
if(bind(su[1], &src_addr, len) == SOCK_ERR)
return;
listen(sl[0], 0);
listen(sl[1], 0);
/* connect in nonblock */
val = TRUE;
ioctlsocket(sc[0], FIONBIO, &val);
val = TRUE;
ioctlsocket(sc[1], FIONBIO, &val);
connect(sc[0], &dest_addr, len);
connect(sc[1], &dest_addr, len);
accept_cnt = 0;
while(TRUE)
{
FD_ZERO(&r);
FD_ZERO(&w);
FD_SET(sl[0], &r);
FD_SET(sl[1], &r);
FD_SET(su[0], &r);
FD_SET(su[1], &r);
if(sc0_conn == FALSE)
FD_SET(sc[0],
&w);
/* it can be read,
if connection fail */
FD_SET(sc[0], &r);
if(sc1_conn == FALSE)
FD_SET(sc[1],
&w);
FD_SET(sc[1], &r);
for(i = 0; i< accept_cnt;
i++)
FD_SET(sa[i],
&r);
select(0, &r,
&w, NULL, NULL);
/* check accept sl[0]*/
if(FD_ISSET(sl[0],
&r))
{
printf("\nlisten
0 accept");
len
= sizeof(struct sockaddr_in);
if((sa[accept_cnt++]
= accept(sl[0], &dest_addr, &len)) == SOCK_ERR)
{
printf("accept
error!");
accept_cnt--;
}
}
/* check accept sl[1]*/
if(FD_ISSET(sl[1],
&r))
{
printf("\nlisten
1 accept");
len
= sizeof(struct sockaddr_in);
if((sa[accept_cnt++]
= accept(sl[1], &dest_addr, &len)) == SOCK_ERR)
{
printf("accept
error!");
accept_cnt--;
}
}
/* check nonblock
connection stablish */
if(FD_ISSET(sc[0],
&w))
{
printf("\nsc
0 connection establish");
sc0_conn
= TRUE;
}
if(FD_ISSET(sc[1],
&w))
{
printf("\nsc
1 connection establish");
sc1_conn
= TRUE;
}
/* check peer listen
socket send data */
if(FD_ISSET(sc[0],
&r))
{
ret
= recv(sc[0], DataBlockBIG, DATA_SIZE_BIG, MSG_RECV_NORMAL);
send(sc[0],
DataBlockBIG, ret, 0);
if(ret
== 0)
{
printf("\nsocket
colsed by peer!");
break;
}
else
{
printf("sc0
%s ",DataBlockBIG);
}
}
if(FD_ISSET(sc[1],
&r))
{
ret
= recv(sc[1], DataBlockBIG, DATA_SIZE_BIG, MSG_RECV_NORMAL);
send(sc[1],
DataBlockBIG, ret, 0);
if(ret
== 0)
{
printf("\nsocket
colsed by peer!");
break;
}
else
{
printf("sc1
%s ",DataBlockBIG);
}
}
/* check upd socket
recv data */
if(FD_ISSET(su[0],
&r))
{
len
= sizeof(struct sockaddr_in);
ret
= recvfrom(su[0], DataBlockBIG, DATA_SIZE_BIG, MSG_RECV_NORMAL,
&get_addr,
&len);
printf("su0
recv%d: %s ",ret, DataBlockBIG);
sendto(su[0],
DataBlockBIG, ret, 0, &get_addr, len);
}
if(FD_ISSET(su[1],
&r))
{
len = sizeof(struct sockaddr_in);
ret
= recvfrom(su[1], DataBlockBIG, DATA_SIZE_BIG, MSG_RECV_NORMAL,
&get_addr,
&len);
printf("su1
recv%d: %s ",ret, DataBlockBIG);
sendto(su[1],
DataBlockBIG, ret, 0, &get_addr, len);
}
for(i = 0; i< accept_cnt;
i++)
{
if(FD_ISSET(sa[i],
&r))
{
//printf("sa%d
recv ",i);
ret
= recv(sa[i], DataBlockBIG, DATA_SIZE_BIG, MSG_RECV_NORMAL);
if(ret
== 0)
{
printf("\nsocket
colsed by peer!");
break;
}
/*
only sum what sa 0 received */
else
if(i == 0)
{
for(j
= 0; j<ret; j++)
{
file_sum
+= ((zl_u32)DataBlockBIG[j]-(zl_u32)(c>>(DataBlockBIG[j]&7)));
c
= DataBlockBIG[j];
}
total_recv
+= (zl_u32)ret;
//printf(".%ld.",total_recv);
}
else
{
send(sa[i],DataBlockBIG,ret,0);
}
}
}
if(i!=accept_cnt)
break;
}
socketclose(sl[0]);
socketclose(sl[1]);
socketclose(sc[0]);
socketclose(sc[1]);
socketclose(su[0]);
socketclose(su[1]);
for(i = 0; i<accept_cnt; i++)
{
socketclose(sa[i]);
}
printf("\nresult= %lu,file len= %ld ,
please check it!", file_sum,total_recv);
}
int main(void)
{
/* this devRTL will be used in tcp/ip, pointed
by NetIf->Info */
struct zlip_addr_info DT_XDATA addr_info=
{
ETHER_ADDR,
ipadsdr,
NetMask,
GateWay
};
/*
* init
*/
ZLIP_Init(&addr_info);
test_complex_send_recv();
ZLIP_Release();
/*
* release
*/
while(TRUE);
return TRUE;
}