2013-01-08 21:05:02 +08:00
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
|
2018-02-06 18:59:24 +08:00
|
|
|
|
#include <netdb.h> /* Ϊ<>˽<EFBFBD><CBBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>netdb.hͷ<68>ļ<EFBFBD> */
|
|
|
|
|
#include <sys/socket.h> /* ʹ<><CAB9>BSD socket<65><74><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>socket.hͷ<68>ļ<EFBFBD> */
|
|
|
|
|
|
|
|
|
|
void tcp_senddata(const char *url, int port, int length)
|
2013-01-08 21:05:02 +08:00
|
|
|
|
{
|
2017-08-10 14:09:57 +08:00
|
|
|
|
struct hostent *host;
|
|
|
|
|
int sock, err, result, timeout, index;
|
|
|
|
|
struct sockaddr_in server_addr;
|
|
|
|
|
rt_uint8_t *buffer_ptr;
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2017-08-10 14:09:57 +08:00
|
|
|
|
/* ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD>url<72><6C><EFBFBD><EFBFBD>host<73><74>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|
|
|
|
host = gethostbyname(url);
|
|
|
|
|
/* <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>socket<65><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SOCKET_STREAM<41><4D>TCP<43><50><EFBFBD><EFBFBD> */
|
|
|
|
|
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
|
|
|
|
|
{
|
|
|
|
|
/* <20><><EFBFBD><EFBFBD>socketʧ<74><CAA7> */
|
|
|
|
|
rt_kprintf("Socket error\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2017-08-10 14:09:57 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD> */
|
|
|
|
|
buffer_ptr = rt_malloc(length);
|
|
|
|
|
/* <20><><EFBFBD>췢<EFBFBD><ECB7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|
|
|
|
for (index = 0; index < length; index ++)
|
|
|
|
|
buffer_ptr[index] = index & 0xff;
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2017-08-10 14:09:57 +08:00
|
|
|
|
timeout = 100;
|
|
|
|
|
/* <20><><EFBFBD>÷<EFBFBD><C3B7>ͳ<EFBFBD>ʱʱ<CAB1><CAB1>100ms */
|
2018-02-06 18:59:24 +08:00
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
|
2017-08-10 14:09:57 +08:00
|
|
|
|
/* <20><>ʼ<EFBFBD><CABC>Ԥ<EFBFBD><D4A4><EFBFBD>ӵķ<D3B5><C4B7><EFBFBD><EFBFBD>˵<EFBFBD>ַ */
|
|
|
|
|
server_addr.sin_family = AF_INET;
|
|
|
|
|
server_addr.sin_port = htons(port);
|
|
|
|
|
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
|
|
|
|
|
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2017-08-10 14:09:57 +08:00
|
|
|
|
/* <20><><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|
|
|
|
err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
|
|
|
|
|
rt_kprintf("TCP thread connect error code: %d\n", err);
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2018-02-06 18:59:24 +08:00
|
|
|
|
while (1)
|
2017-08-10 14:09:57 +08:00
|
|
|
|
{
|
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>sock<63><6B><EFBFBD><EFBFBD> */
|
|
|
|
|
result = send(sock, buffer_ptr, length, MSG_DONTWAIT);
|
2018-02-06 18:59:24 +08:00
|
|
|
|
if (result < 0) //<2F><><EFBFBD>ݷ<EFBFBD><DDB7>ʹ<EFBFBD><CDB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2017-08-10 14:09:57 +08:00
|
|
|
|
{
|
|
|
|
|
rt_kprintf("TCP thread send error: %d\n", result);
|
2018-02-06 18:59:24 +08:00
|
|
|
|
closesocket(sock);
|
2017-08-10 14:09:57 +08:00
|
|
|
|
|
|
|
|
|
/* <20>ر<EFBFBD><D8B1><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD>´<EFBFBD><C2B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|
|
|
|
rt_thread_delay(10);
|
|
|
|
|
|
|
|
|
|
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
|
2018-02-06 18:59:24 +08:00
|
|
|
|
rt_kprintf("TCP Socket error:%d\n", sock);
|
2017-08-10 14:09:57 +08:00
|
|
|
|
|
|
|
|
|
err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
|
|
|
|
|
rt_kprintf("TCP thread connect error code: %d\n", err);
|
|
|
|
|
}
|
|
|
|
|
else if (result == 0)
|
|
|
|
|
{
|
|
|
|
|
/* <20><>ӡsend<6E><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ0<CEAA>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD><EFBFBD>Ϣ */
|
|
|
|
|
rt_kprintf("\n Send warning,send function return 0.\r\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-01-08 21:05:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef RT_USING_FINSH
|
|
|
|
|
#include <finsh.h>
|
|
|
|
|
/* <20><><EFBFBD><EFBFBD>tcpclient<6E><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>finsh shell<6C><6C> */
|
|
|
|
|
FINSH_FUNCTION_EXPORT(tcp_senddata, send a packet through tcp connection);
|
|
|
|
|
#endif
|
|
|
|
|
|