1、在ucosII系统中创建一个进程:
//TCP 2000
OSTaskCreate(tcp, (void *)0, &gstack_task_socket4[TASK_SOCKET4_STK_SIZE - 1], 14);
OSTaskNameSet(14, "tcp", &os_err);
2、进程的具体处理:
void tcp(void *arg)
{
struct netconn *__pstConn, *__pstNewConn;
//建立TCP连接
__pstConn = netconn_new(NETCONN_TCP);
//将建立的连接进行端口绑定
netconn_bind(__pstConn, NULL, 2000); //HTTP_PORT,80端口
//开始监听
netconn_listen(__pstConn);
while(1)
{
//接收外部来的连接
__pstNewConn = netconn_accept(__pstConn);
//具体处理函数
tcp_process(__pstNewConn);
//如果连接有效,删除连接
if (__pstNewConn == NULL)
continue;
netconn_delete(__pstNewConn);
OSTimeDly(100);
}
具体处理函数如下:
void tcp_process(struct netconn *conn)
{
struct netbuf *inbuf;
char *rq;
u16_t len;
///获取数据
inbuf = netconn_recv(conn);
if(inbuf != NULL)
{
netbuf_data(inbuf, &rq, &len);
/* HTTP "GET /\r\n" */
if(rq[0] == 'G' && rq[1] == 'E' && rq[2] == 'T') {
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr),
NETCONN_NOCOPY);
//发送数据
netconn_write(conn, indexdata, sizeof(indexdata),
NETCONN_NOCOPY);
//netconn_write(conn,tcp_data_send,sizeof(tcp_data_send),NETCONN_NOCOPY);
//发送数据
netconn_close(conn);
//关闭连接
}
}
memp_free(MEMP_NETBUF, inbuf);
}
补充一点,两个数组的定义:
char indexdata1[] =
" \
Mr Lee's test page \
\
Hello , Mr Lee!\r\n \
Good Luck to you !\r\n \
This is a small test page. \
\
";
char http_html_hdr[] =
"HTTP/1.0 200 OK\r\n\
Content-type: text/html\r\n\r\n";
阅读(8427) | 评论(0) | 转发(1) |