Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1975167
  • 博文数量: 356
  • 博客积分: 8284
  • 博客等级: 中将
  • 技术积分: 4580
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-15 20:25
个人简介

天行健,君子以自强不息

文章分类

全部博文(356)

文章存档

2018年(1)

2016年(4)

2015年(13)

2014年(14)

2013年(2)

2012年(25)

2011年(43)

2010年(65)

2009年(189)

分类: C/C++

2012-03-30 15:48:28

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";
阅读(8349) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~