64 lines
2.0 KiB
C
64 lines
2.0 KiB
C
|
#include <rtthread.h>
|
|||
|
#include <lwip/netdb.h> /* Ϊ<>˽<EFBFBD><CBBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>netdb.hͷ<68>ļ<EFBFBD> */
|
|||
|
#include <lwip/sockets.h> /* ʹ<><CAB9>BSD socket<65><74><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>sockets.hͷ<68>ļ<EFBFBD> */
|
|||
|
|
|||
|
void tcp_senddata(const char* url, int port, int length)
|
|||
|
{
|
|||
|
struct hostent *host;
|
|||
|
int sock, err, result, timeout, index;
|
|||
|
struct sockaddr_in server_addr;
|
|||
|
rt_uint8_t *buffer_ptr;
|
|||
|
|
|||
|
/* ͨ<><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;
|
|||
|
}
|
|||
|
|
|||
|
/* <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;
|
|||
|
|
|||
|
timeout = 100;
|
|||
|
/* <20><><EFBFBD>÷<EFBFBD><C3B7>ͳ<EFBFBD>ʱʱ<CAB1><CAB1>100ms */
|
|||
|
lwip_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
|
|||
|
/* <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));
|
|||
|
|
|||
|
/* <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);
|
|||
|
|
|||
|
while(1)
|
|||
|
{
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>sock<63><6B><EFBFBD><EFBFBD> */
|
|||
|
result = send(sock, buffer_ptr, length, MSG_DONTWAIT);
|
|||
|
if(result == -1) //<2F><><EFBFBD>ݷ<EFBFBD><DDB7>ʹ<EFBFBD><CDB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
rt_kprintf("TCP thread send error: %d\n", result);
|
|||
|
lwip_close(sock); //<2F>ر<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)
|
|||
|
rt_kprintf("TCP Socket error:%d\n",sock);
|
|||
|
err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
|
|||
|
rt_kprintf("TCP thread connect error code: %d\n", err);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#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
|
|||
|
|