shaoh.12 发表于 2015-8-19 17:36:04

UIP UDP通信问题

   移植的UIP协议,TCP可以运行,但是UDP不能收不能发。代码如下。帮我看看哪边有问题?
main.c

int main(void)
{
        unsigned int i;
        uip_ipaddr_t ipaddr;        /* local IP address */
        struct timer periodic_timer, arp_timer;

        // system init
        SystemInit();                                    /* setup core clocks */

        // clock init
        clock_init();
                // two timers for tcp/ip
        timer_set(&periodic_timer, CLOCK_SECOND / 2); /* 0.5s */
        timer_set(&arp_timer, CLOCK_SECOND * 10);        /* 10s */
       
        // ethernet init
        tapdev_init();

        // Initialize the uIP TCP/IP stack.
        uip_init();

        uip_ipaddr(ipaddr, 192,168,0,100);
        uip_sethostaddr(ipaddr);        /* host IP address */
        uip_ipaddr(ipaddr, 192,168,0,1);
        uip_setdraddr(ipaddr);        /* router IP address */
        uip_ipaddr(ipaddr, 255,255,255,0);
        uip_setnetmask(ipaddr);        /* mask */
       
udp_service_init();

        // Initialize the HTTP server, listen to port 80.
        //httpd_init();
uip_listen(HTONS(1200));                        //¼àÌý1200¶Ë¿Ú,ÓÃÓÚTCP Server
        udp_service_init();
        while(1)
        {
        /* receive packet and put in uip_buf */
                uip_len = tapdev_read(uip_buf);
            if(uip_len > 0)                /* received
             ........



udp_demo.c

#include "uip.h"
#include "uipopt.h"
#include "uip_arch.h"
#include "tcp_demo.h"

#include <string.h>       
#include <stdio.h>


//extern void tcp_demo_appcall(void);
//void myudp_appcall(void);


void udp_service_init(void)
{
        uip_ipaddr_t ipaddr;
        uip_ipaddr(ipaddr, 192,168,0,5);
uip_udp_conn = uip_udp_new(&ipaddr, 1000);
        if(uip_udp_conn != NULL)
            {
             uip_udp_bind(uip_udp_conn, 2000);                               
            }
}

/*******************************************************************************
*        º¯ÊýÃû: void myudp_send(char *str,short n)
*        ²ÎÊý:
*        ·µ»Ø: ÎÞ
*        ¹¦ÄÜ: UDP Êý¾Ý°ü·¢ËÍ
**************************************************************************/
void myudp_send(char *str,short n)
{

   char   *nptr;   
   nptr = (char *)uip_appdata;      
   memcpy(nptr, str, n);
   uip_udp_send(n);                                   //·¢ËÍn¸öÊý¾Ý
}

/*******************************************************************************
*        º¯ÊýÃû: void UDP_newdata(void)
*        ²ÎÊý:
*        ·µ»Ø: ÎÞ
*        ¹¦ÄÜ: UDP Êý¾Ý°ü·¢ËÍ
**************************************************************************/
void UDP_newdata(void)
{
char   *nptr;
short len;
        len = uip_datalen();                                                                        //¶ÁÈ¡Êý¾Ý³¤¶È
    nptr = (char *)uip_appdata;

       if(strncmp(nptr,"ledon 1",7)==0)
               
                myudp_send("LED1 ÁÁ\n",8);

        /*else if(strncmp(nptr,"ledon 2",7)==0){
       
                myudp_send("LED2 ÁÁ\n",8);
        }
        else if(strncmp(nptr,"ledon 3",7)==0){
               
                myudp_send("LED3 ÁÁ\n",8);
        }
        else if(strncmp(nptr,"ledoff 1",8)==0){
       
                myudp_send("LED1 Ãð\n",8);
        }
        else if(strncmp(nptr,"ledoff 2",8)==0){
               
                myudp_send("LED2 Ãð\n",8);
        }
        else if(strncmp(nptr,"ledoff 3",8)==0){
       
                myudp_send("LED3 Ãð\n",8);
        }

    else myudp_send("Unkown command!\n",16); */
}
/*******************************************************************************
*        º¯ÊýÃû: void myudp_appcall(void) )
*        ²ÎÊý:
*        ·µ»Ø: ÎÞ
*        ¹¦ÄÜ: UDPÖ÷º¯Êý
**************************************************************************/
void myudp_appcall(void)
{
       if(uip_poll())
       {
       myudp_send("LED1 ÁÁ\n",8);
       }
       else if(uip_newdata())                                                
   {
       UDP_newdata();      
   }
}



页: [1]
查看完整版本: UIP UDP通信问题