[C] 纯文本查看 复制代码
/*
*********************************************************************************************************
*
* 模块名称 : TCPnet网络协议栈测试
* 文件名称 : app_tcpnet_lib.c
* 版 本 : V1.0
* 说 明 : 测试的功能说明
* 1. 强烈推荐将网线接到路由器或者交换机上面测试,因为已经使能了DHCP,可以自动获取IP地址。
* 2. 客户端的例子相比服务器的例子稍麻烦些,因为客户端的例子需要用户知道电脑端IP和端口号。
* 并根据实际情况设置IP和端口号的宏定义,这个配置在文件app_tcpnet_lib.c开头,测试的时
* 候板子要连接这个IP和端口(下面是默认配置,一定要根据实际情况重新配置,如果不会配置,
* 看本例程对应的教程即可):
* #define IP1 192
* #define IP2 168
* #define IP3 28
* #define IP4 210
* #define PORT_NUM 1001
* 3. 创建了一个TCP Client,而且使能了局域网域名NetBIOS,用户只需在电脑端ping armfly
* 就可以获得板子的IP地址,本地端口被设置为1024。
* 4. 用户可以在电脑端用网络调试软件创建TCP Server连接此客户端。
* 5. 按键K1按下,发送8字节的数据给TCP Server。
* 6. 按键K2按下,发送1024字节的数据给TCP Server。
* 7. 按键K3按下,发送5MB字节的数据给TCP Server。
* 8、摇杆OK键按下,连接远程服务器。
*
* 修改记录 :
* 版本号 日期 作者 说明
* V1.0 2020-01-09 Eric2013 首发
*
* Copyright (C), 2019-2030, 安富莱电子 [url]www.armfly.com[/url]
*
*********************************************************************************************************
*/
#include "includes.h"
/*
*********************************************************************************************************
* 用于本文件的调试
*********************************************************************************************************
*/
#if 1
#define printf_debug printf
#else
#define printf_debug(...)
#endif
/*
*********************************************************************************************************
* 宏定义,远程服务器的IP和端口
*********************************************************************************************************
*/
/* 要访问的远程服务器IP和端口配置,也就是电脑端调试助手设置的IP和端口号 */
#define IP1 192
#define IP2 168
#define IP3 28
#define IP4 210
#define PORT_NUM 1001
/* 这个是本地端口 */
#define LocalPort_NUM 1026
/*
*********************************************************************************************************
* 变量
*********************************************************************************************************
*/
//NET_ADDR4 addr = { NET_ADDR_IP4, PORT_NUM, IP1,IP2,IP3,IP4};
static NET_ADDR addr = { NET_ADDR_IP4, PORT_NUM, IP1, IP2, IP3, IP4};
int32_t udp_sock;
/* TCPnet API的返回值 */
static const char * ReVal_Table1[]=
{
"netOK: Operation succeeded",
"netBusy: Process is busy",
"netError: Unspecified error",
"netInvalidParameter: Invalid parameter specified",
"netWrongState: Wrong state error",
"netDriverError: Driver error",
"netServerError: Server error",
"netAuthenticationFailed: User authentication failed",
"netDnsResolverError: DNS host resolver failed",
"netFileError: File not found or file r/w error",
"netTimeout: Operation timeout",
};
/*
*********************************************************************************************************
* 函 数 名: tcp_cb_client
* 功能说明: TCP Socket的回调函数
* 形 参: socket 句柄
* event 事件类型
* addr NET_ADDR类型变量,记录IP地址,端口号。
* buf ptr指向的缓冲区记录着接收到的TCP数据。
* len 记录接收到的数据个数。
* 返 回 值:
*********************************************************************************************************
*/
static uint32_t udp_cb_func (int32_t socket, const NET_ADDR *addr, const uint8_t *buf, uint32_t len)
{
char buffer[50];
uint16_t i = 0;
/* 确保是udp_soc的回调 */
if (socket != udp_sock)
{
return (0);
}
/* 发消息的远程IP,打印IP和端口号 */
sprintf(buffer, "远程连接IP: %d.%d.%d.%d", addr->addr[0], addr->addr[1], addr->addr[2], addr->addr[3]);
printf_debug("%s port:%d\r\n", buffer, addr->port);
/* 接收到的数据长度,单位字节 */
printf_debug("Data length = %d\r\n", len);
/* 打印接收到的消息 */
for(i = 0; i < len; i++)
{
printf_debug("buf[%d] = %d\r\n", i, buf[i]);
}
return (0);
}
/*
*********************************************************************************************************
* 函 数 名: TCPnetClient1
* 功能说明: TCPnet应用
* 形 参: 无
* 返 回 值: 无
*********************************************************************************************************
*/
void udpnetClient(void)
{
int32_t iCount;
uint8_t *sendbuf;
uint32_t maxlen;
netStatus res;
const uint16_t usMaxBlockTime = 2; /* 延迟周期 */
uint32_t EvtFlag;
uint32_t t1, t2;
udp_sock = netUDP_GetSocket (udp_cb_func);
if (udp_sock > 0)
{
/* 使能TCP_TYPE_KEEP_ALIVE,会一直保持连接 */
//netTCP_SetOption (udp_sock, netTCP_OptionKeepAlive, 1);
netUDP_Open (udp_sock, LocalPort_NUM);
}
while (1)
{
EvtFlag = osThreadFlagsWait(0x0000000FU, osFlagsWaitAny, usMaxBlockTime);
/* 按键消息的处理 */
switch (EvtFlag)
{
/* 接收到K2键按下,给远程TCP客户端发送4096字节数据 */
case KEY1_BIT0:
iCount = 1;
t1 = osKernelGetTickCount ();
printf("------%d\r\n", t1);
do
{
sendbuf = netUDP_GetBuffer (1452);
if(sendbuf != NULL)
{
sendbuf[0] = '1';
sendbuf[1] = '2';
sendbuf[2] = '3';
sendbuf[3] = '4';
sendbuf[4] = '5';
sendbuf[5] = '6';
sendbuf[6] = '7';
sendbuf[7] = '8';
/* 必须使用申请的内存空间 */
res = netUDP_Send(udp_sock, &addr, sendbuf, 1452);
/* 保证发送成功了发送次数才可以减一,这里发送成功并不保证远程设备接受成功 */
if(res == netOK){
iCount--;
}
else{
iCount--;
printf("发送失败%s\r\n", ReVal_Table1[res]);
}
}
}while(iCount > 0);
break;
case KEY2_BIT1:
// if (udp_sock > 0)
// {
// if(netTCP_GetState(udp_sock) != netTCP_StateESTABLISHED)
// {
// res = netTCP_Connect (udp_sock, (NET_ADDR *)&addr, LocalPort_NUM);
// printf_debug("%s\r\n", ReVal_Table1[res]);
// }
// }
break;
/* 其他的键值不处理 */
default:
break;
}
}
}
/***************************** 安富莱电子 [url]www.armfly.com[/url] (END OF FILE) *********************************/