现在的位置: 首页 > 综合 > 正文

socket编程_给它做得这么简单

2014年01月28日 ⁄ 综合 ⁄ 共 1566字 ⁄ 字号 评论关闭

下面是我用beaglebone 做得一个UDP发送接收程序

在ti的官网里面提供的裸跑程序里面其中关于以太网部分已经移植好了Lwip

所以,在它上面写网络程序真的很方便。

昨天写了一个,程序写好了,本来以为要调试很久,所以迟迟不肯动手。今早,插上去,发现一点错误都没有。真是幸事。

 

源代码:

int main(void)
{
    unsigned char macArray[6];
    unsigned int ipAddr = 0;
    volatile unsigned int cnt = 3;

    struct udp_pcb *UdpPcb;
    struct ip_addr ipaddr;
    struct pbuf *p;

    MMUConfigAndEnable();

     /* Enable IRQ for ARM (in CPSR)*/
    IntMasterIRQEnable();
    IntAINTCInit();

    DelayTimerSetup();

    /* Set up the UART peripheral for Standard Input/Output operation.*/
    UARTStdioInit();

    CPSWPinMuxSetup();
    CPSWClkEnable();

    /* Chip configuration MII selection */
    EVMPortMIIModeSelect();

    /* Get the MAC address */
    EVMMACAddrGet(0, macArray);

    AintcCPSWIntrSetUp();

    UARTPuts("Acquiring IP Adress... \n\r" , -1);

    while(cnt--)
    {
    /* Initialze the lwIP library, using DHCP.*/
#if STATIC_IP_ADDRESS

        ipAddr = lwIPInit(0, macArray, STATIC_IP_ADDRESS, 0, 0, IPADDR_USE_STATIC);

#else

        ipAddr = lwIPInit(0, macArray, 0, 0, 0, IPADDR_USE_DHCP);

#endif
        if(0 != ipAddr)
        {
            break;
        }
    }

    UARTPuts("EVM IP Address Assigned: ", -1);

    IpAddrDisplay(ipAddr);

    /* starting the UDP connection */
    p = pbuf_alloc(PBUF_RAW,sizeof(UDPData),PBUF_RAM);

    p->payload=(void *)UDPData;

    IP4_ADDR(&ipaddr,192,168,1,102);  //远程主机地址

    UdpPcb = udp_new();

    udp_bind(UdpPcb,IP_ADDR_ANY,4000);  //绑定本地IP地址

    udp_connect(UdpPcb,&ipaddr,5000);  //连接远程主机

    udp_recv(UdpPcb,UDP_Receive,NULL);

    delay(400000);

    udp_send(UdpPcb,p);

    /* Initialize the sample httpd server. */
//    echo_init();

    /* Loop forever.  All the work is done in interrupt handlers. */
    while(1)
    {
        ; /* Perform nothing */
    }
}

 

抱歉!评论已关闭.