搜索
bottom↓
回复: 9

跪求uip1.0作为客户端主动连接服务器的例子

[复制链接]

出0入0汤圆

发表于 2013-5-28 11:28:32 | 显示全部楼层 |阅读模式
手头上有个enc28j60的模块,移植uip1.0作为服务器与pc机通讯正常,现在按照其它高手说的一步一步改成服务端模式,能ping通,怎么也无法主动连上服务器.
整了好些时间,要崩溃了.我的是ATMEGA128+enc28j60。
   uip_ipaddr(ipaddr, 211,86,58,250); // 目的地址  
uip_connect((uip_ipaddr_t *)ipaddr , HTONS(8080) );
也定义了#define UIP_ACTIVE_OPEN 1
#define UIP_ACTIVE_OPEN 1

跪求大神给点意见.

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

 楼主| 发表于 2013-5-28 12:22:06 | 显示全部楼层
自己顶...

出0入0汤圆

 楼主| 发表于 2013-5-28 15:48:05 | 显示全部楼层
移植的uip1.0服务器模式性能还可以,客户端就是连不上,没有执行uip_connect函数,不明白为什么

出0入0汤圆

发表于 2013-5-28 16:18:02 | 显示全部楼层
你在局域网内试过吗
这个服务器是公网IP还是局域网内电脑,如果能ping同说明链接是没有问题的,建议你用交叉线或者通过交换机直接连接电脑试试,排除是不是路由器设置的问题

出0入0汤圆

发表于 2013-5-28 16:19:13 | 显示全部楼层
看你的IP应该是公网IP,你去检查一下你现在路由器是否设置了一些不该设置的东西

出0入0汤圆

 楼主| 发表于 2013-5-28 17:30:41 | 显示全部楼层
不是这个问题,我改了ip192.168系列的也是一样,就是不能主动连上
void tcp_appcall(void)
{
    /*if(uip_conn->rport == HTONS(RemotePort))
    {
        tcp_client_appcall();
    }*/
        switch(uip_conn->rport) {
                case HTONS(RemotePort):
                        if(uip_timedout()||uip_aborted()||uip_closed()){
#if AUTO_RECONNECT
                                /*reconnect to server*/
                                //delay_sec(5);
                                uip_ipaddr_t ipaddr;//定义IP类型变量
    uip_ipaddr(ipaddr, RemoteIP[0], RemoteIP[1], RemoteIP[2], RemoteIP[3]); //远程IP
    uip_connect(&ipaddr, HTONS(RemotePort));//建立到远程端口
                       
#endif
                        }else if(uip_connected()){
#if 1
                                /*is connected ok*/
                                uip_send(uip_appdata,sprintf((char*)uip_appdata,"%s",
                                "Hello,I connected to you! thanks."));
#endif
                        }else if(uip_rexmit()){
#if 1
                                /*need retransmission last packet*/
                                uip_send(uip_appdata,sprintf((char*)uip_appdata,"%s",
                                "this is retransmission packet"));
#endif
                        }else if(uip_poll()){
                                /*poll connecte is idle*/
#if 1
//                                uip_send(uip_appdata,sprintf((char*)uip_appdata,"%s",
//                                "Hi, we are idle"));
                                uip_send("idle",4);
#endif
                        }else if(uip_acked()){
                                /*get a ack for send packet ok*/
#if 0
                                uip_send(uip_appdata,sprintf((char*)uip_appdata,"%s",
                                "this is a second packet."));
#endif
                                if(uip_newdata()){
                                        goto newdata_with_acked;
                                }
                        }else if(uip_newdata()){
newdata_with_acked:
#if 1
                                /*receving a new data*/
                                memcpy(AppData, uip_appdata, uip_len);
               data_len=uip_len;
              uip_send(AppData,data_len);
#endif
                        }else{
                                /*error*/
                        }
                        break;
                default:
                        /*discard packet*/
                        break;
        }
}

出0入0汤圆

 楼主| 发表于 2013-5-28 17:54:10 | 显示全部楼层
lou0908 发表于 2013-5-28 16:19
看你的IP应该是公网IP,你去检查一下你现在路由器是否设置了一些不该设置的东西 ...

改了ip试了一下,依旧是客户端模式能ping通不能主动连上,服务器模式没问题,好像uip_connect这个函数就没有执行

出0入0汤圆

发表于 2013-7-2 15:44:59 | 显示全部楼层
我用的uip0.9 的客户端模式,感觉什么都没改,1.0不一样么?
        /* Initialize the ARP 初始化ARP*/
        uip_arp_init();

        /* Initialize the uIP TCP/IP stack. 初始化uIP的TCP/ IP协议栈*/
        uip_init();

        /*set host MAC addr设置主机的MAC地址*/
        uip_ethaddr.addr[0] = 0x12;
        uip_ethaddr.addr[1] = 0x34;
        uip_ethaddr.addr[2] = 0x56;
        uip_ethaddr.addr[3] = 0x78;
        uip_ethaddr.addr[4] = 0x90;
        uip_ethaddr.addr[5] = 0xAB;

#if UIP_FIXEDADDR == 0
        /*host ip addr主机IP地址*/
        uip_ipaddr(ipaddr, 192,169,13,201);
        uip_sethostaddr(ipaddr);

        /*netmask addr网络掩码地址*/
        uip_ipaddr(ipaddr, 255,255,255,0);
        uip_setnetmask(ipaddr);

        /*router ip addr路由器的IP地址*/
        uip_ipaddr(ipaddr, 192,169,13,1);
        uip_setdraddr(ipaddr);
#endif

        /*connect to server连接服务器*/
        uip_ipaddr(ipaddr, 192,169,13,14);
        uip_connect(ipaddr, HTONS(SERVER_PORT));
       

       

//        SFRPAGE   = SMB0_PAGE;                        //设置从机地址       
        SMB0ADR        =        (0x01<<1)&0xFE;                //不使能全局呼叫地址(0x00)识别
               
        while(1)
        {
                TaskA();
        }
}


void TaskA(void)
{
        uint8 xdata i, arptimer,Ledtimer;
    //while (1)
    {
//                SFRPAGE   = CONFIG_PAGE;
                Delayms(50);
                Ledtimer++;
                if(Ledtimer%10==0)
                        LedState = ~LedState;
//                if(I2CErrCnt>100)
//                        LedState3=~LedState3;
//                if(I2CErrCnt>0)
//                        LedState2=~LedState2;
//                for(TestCnt=0;TestCnt<TESTNUM;TestCnt++)
//                {
//                        RiOrder[TestCnt]        =        TestData[TestCnt];
//                }
//                RevOrderCnt        =        TESTNUM;
//                OSTaskResume(ID_COMMANDPARSING);               
//                SFRPAGE = SMB0_PAGE;
//                STA = 1;
                uip_len = dev_poll();

            if(uip_len == 0)
                {
                        for(i = 0; i < UIP_CONNS; i++)
                        {
                                uip_periodic(i);
                                /* If the above function invocation resulted in data that
                                should be sent out on the network, the global variable
                                uip_len is set to a value > 0.
                                如果以上函数调用的数据应该被发送到网络
                                全局变量uip_len设置为>0的值。*/
                                if(uip_len > 0)
                                {
                                        uip_arp_out();
                                        dev_send();
                                }
                        }

                        /* Call the ARP timer function every 10 seconds.
                        每10秒调用一次ARP定时器功能*/
                        if(++arptimer == 20)
                        {
                                uip_arp_timer();
                                arptimer = 0;
                        }
                        Delayms(1);
            }
                else
                {
                        if(BUF->type == htons(UIP_ETHTYPE_IP))
                        {
                                uip_arp_ipin();
                                uip_input();
                                /* If the above function invocation resulted in data that
                                should be sent out on the network, the global variable
                                uip_len is set to a value > 0.
                                如果以上函数调用的数据应该被发送到网络
                                全局变量uip_len设置为>0的值。 */
                                if(uip_len > 0)
                                {
                                        uip_arp_out();
                                        dev_send();
                                }
                        }
                        else if(BUF->type == htons(UIP_ETHTYPE_ARP))
                        {
                                uip_arp_arpin();
                                /* If the above function invocation resulted in data that
                                should be sent out on the network, the global variable
                                uip_len is set to a value > 0.
                                如果以上函数调用的数据应该被发送到网络
                                全局变量uip_len设置为>0的值。*/
                                if(uip_len > 0)
                                {
                                        dev_send();
                                }
                        }
                }
    }
}
/*uIP call back func回调函数*/
void uip_appcall(void)
{
        switch(uip_conn->rport) {
                case HTONS(SERVER_PORT):
                        if(uip_timedout()||uip_aborted()||uip_closed()){
#if AUTO_RECONNECT
                                /*reconnect to server重新连接到服务器*/
                                Delayms(1000);
                                //NetIni();
                                uip_connect(ipaddr, HTONS(SERVER_PORT));
#endif
                        }else if(uip_connected()){
#if 1
                                /*is connected ok 是否连接OK*/
                                uip_send(uip_appdata,sprintf((char*)uip_appdata,"%s",
                                "Hello,I connected to you! thanks."));
#endif
                        }else if(uip_rexmit()){
#if 1
                                /*need retransmission last packet需要重传最后一个包*/
//                                uip_send(uip_appdata,sprintf((char*)uip_appdata,"%s",
//                                "this is retransmission packet"));
                                uip_send("this is retransmission packet",30);
#endif
                        }else if(uip_poll()){
                                /*poll connecte is idle 轮询连接处于闲置状态*/
#if 1
//                                uip_send(uip_appdata,sprintf((char*)uip_appdata,"%s",
//                                "Hi, we are idle"));
                                //uip_send("idle",4);
#endif
                        }else if(uip_acked()){
                                /*get a ack for send packet ok得到一个ACK发送包OK*/
#if 0
                                uip_send(uip_appdata,sprintf((char*)uip_appdata,"%s",
                                "this is a second packet."));
#endif
                                if(uip_newdata()){
                                        goto newdata_with_acked;
                                }
                        }else if(uip_newdata()){
newdata_with_acked:
#if 1
                                /*receving a new data接收一个新的数据*/
//                                uip_appdata[uip_len+0] = ((char*)&uip_len)[0];
//                                uip_appdata[uip_len+1] = ((char*)&uip_len)[1];
//                                uip_send(uip_appdata,uip_len+2);
                                uip_send("OK thanks",10);
#endif
                        }else{
                                /*error*/
                        }
                        break;
                default:
                        /*discard packet丢弃数据包*/
                        break;
        }
}

出0入0汤圆

发表于 2014-4-21 15:45:16 | 显示全部楼层
您好·我也遇到同样问题,好纠结·希望能问问你看··谢谢了

出0入0汤圆

发表于 2014-4-27 13:24:35 | 显示全部楼层
爱看下雪 发表于 2014-4-21 15:45
您好·我也遇到同样问题,好纠结·希望能问问你看··谢谢了

我之前用的同样 的代码是好的,放了一段时间,现在回来再用,怎么都连接不上服务器了,也是Uip做客服端的,你问题解决了吗,共同交流下啊
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-23 22:35

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表