5189|3

63

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

UIP学习笔记1 [复制链接]

8962的开发板用FlashUserGet()函数并不能从内部得到MAC地址,所以在uip的例程里要直接赋值给ulUser0和ulUser1.
为了方便和明显,我这里在FlashUserGet(&ulUser0, &ulUser1);这一语句之后直接设置MAC地址:
    ulUser0=0xaaaaaa;
    ulUser1=0xaaaaaa;
这样的话就能够用DHCP的方法分配到IP地址了.
通过访问分配的ip地址,能够显示uIP的简单服务器
uip的中断处理函数void *_appcall(void)是一个很有用的函数,当网卡中断处理时,会调用这一个函数.
来看看一下这个函数进行了哪些操作:
void tcp_appcall(void)
{
    char *hello="Hi,This is a TCP test!\n";
    if(uip_connected())
    {
        uip_send(hello,strlen(hello)+1);
    }
    if(uip_newdata())
    {
        uip_send(uip_appdata,uip_len);
    }
}
作为一个服务端(server),如果开发板接到了请求并且和客户端(client)建立了连接,那么开发板将返回给客户端一个字符串,并把客户端发来的数据echo back.

以上函数添加到main.c文件里.同时修改uip-conf.h里的UIP_APPCALL定义为:
#ifndef UIP_APPCALL
#define UIP_APPCALL     tcp_appcall
#endif
在main.c文件里添加要监听的端口,这里我们使用以前的端口80.

在linux下写了一个程序来测试tcp的连接,当建立连接后,程序向服务器发送一个字符串,这里是在终端输入的,开发板收到后,返回一个字符串"Hi,This is a TCP test!",或者是把收到的字符串回传.

通过这样的方法,我们就能通过appcall这个函数来写自己的应用函数.
源代码如下:



# include <stdlib.h>
# include <string.h>
# include <stdio.h>
# include <errno.h>
#include<netdb.h>

int client(const char *host,const char *port);
int connectsock(const char *host,const char *port,const char *transport);
int connecttcp(const char *host,const char *port);

//#include"errexit.h"

extern int errno;

# ifndef     INADDR_NONE
# define     INADDR_NONE     0xffffffff
# endif
# define LINELEN 128

/*---------------------------------------------------------------
 * main - TCP client for ECHO port
 *---------------------------------------------------------------
 */
int main(int argc,char *argv[])
{
    char *host="192.168.1.106"; /* host to use if none supplied*/
//    char *host="127.0.0.1"; /* host to use if none supplied*/
//    char *host="192.168.56.103"; /* host to use if none supplied*/
    char *port="80";     /* default port name */
//    char *port="10001";     /* default port name */
    client(host,port);
    exit(0);
}

/*-----------------------------------------------------------------
 * client - send input to ECHO port on specified host and print reply
 *-----------------------------------------------------------------
 */
int client(const char *host,const char *port)
{
    char buf[LINELEN+1];   /* buffer for one line of text*/
    int s,n;             /* socket descriptor, read count*/
    int outchars,inchars;  /* characters sent and received*/

    s=connecttcp(host,port);

    while(fgets(buf,sizeof(buf),stdin))
    {
        buf[LINELEN]='\0';     /* insure line null-terminated */
        outchars=strlen(buf);
        (void)write(s,buf,outchars);

        /* read it back */
        for(inchars=0;inchars<outchars;inchars+=n)
         {
            n=read(s,&buf[inchars] ,outchars-inchars);
            if(n<0)
                errexit("socket read failed: %s\n",strerror(errno));
        }
        printf("echo back ok.\n");
        fputs(buf,stdout);
    }
}



//connectsock - allocate & connect a socket using TCP or UDP
int connectsock(const char *host,const char *port,const char *transport)
/*
 * Arguments:
 * host - name of host to which connection is desired
 * port - port associated with the desired port
 * transport - name of transport protocol to use ("tcp" or "udp")
 */
{
    struct hostent *phe;   /* pointer to host information entry*/
    struct servent *pse; /* pointer to port information entry*/
    struct protoent *ppe;  /* pointer to protocol information entry*/
    struct sockaddr_in sin ;  /* an Internet endpoint address    */
    int s,type;     /* socket descriptor and socket type    */


    memset(&sin,0,sizeof(sin));
    sin.sin_family=AF_INET;

    /* Map port name to port number */
    if(pse=getservbyname(port,transport))
        sin.sin_port=pse->s_port;
    else if((sin.sin_port=htons((unsigned short)atoi(port)))==0)
               errexit("can't get \"%s\" port entry\n",port);

    /* Map host name to IP address, allowing for dotted decimal */
    if(phe=gethostbyname(host))
        memcpy(&sin.sin_addr,phe->h_addr,phe->h_length);
    else if((sin.sin_addr.s_addr=inet_addr(host))==INADDR_NONE)
        errexit("can't get \"%s\" host entry\n",host);

    /* Map transport protocol name to protocol number */
    if((ppe=getprotobyname(transport))==0)
        errexit( "can't get \"%s\" protocol entry\n" , transport) ;

    /* Use protocol to choose a socket type */
    if ( strcmp(transport,"udp")==0)
        type=SOCK_DGRAM;
    else
        type=SOCK_STREAM;

    /* Allocate a socket */
    s=socket(PF_INET,type,ppe->p_proto);
    if(s<0)
        errexit("can't create socket: %s\n",strerror(errno));

    /* Connect the socket */
    if(connect(s,(struct sockaddr *)&sin,sizeof(sin))<0)
            errexit("can't connect to %s.%s: %s\n",host,port,strerror(errno));
    return s;
}

int connecttcp(const char *host,const char *port)
/*
 * Arguments:
 * host - name of host to which connection is desired
 * port - port associated with the desired port
 */
{
     return connectsock(host,port,"tcp") ;
}

最新回复

真好,谢谢楼主!  详情 回复 发表于 2012-11-10 22:40
 
点赞 关注

回复
举报

2万

帖子

74

TA的资源

管理员

沙发
 

回复 楼主 blackwc2006 的帖子

谢谢分享
加EE小助手好友,
入技术交流群
EE服务号
精彩活动e手掌握
EE订阅号
热门资讯e网打尽
聚焦汽车电子软硬件开发
认真关注技术本身
 
个人签名

加油!在电子行业默默贡献自己的力量!:)

 

回复

239

帖子

0

TA的资源

一粒金砂(中级)

板凳
 
 
 
 

回复

55

帖子

0

TA的资源

一粒金砂(中级)

4
 
真好,谢谢楼主!
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表