socket编程_给它做得这么简单
2012-08-15 08:17
316 查看
下面是我用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 */
}
}
在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 */
}
}
相关文章推荐
- Linux C Socket编程原理及简单实例
- Unix网络编程之socket编程(一)--socket入门简单程序
- Linux内核协议栈(2) 由简单的socket编程例子开始
- android的Socket编程---实现简单的服务器,并与客户端通信
- socket网络编程--简单的多人聊天
- Linux socket 编程简单例子
- Linux socket编程-最简单的服务器和客户端程序
- 使用java swing和socket编程实现简单的多人聊天室
- 简单理解php的socket编程
- Linux C Socket编程原理及简单实例
- Socket网络编程--简单Web服务器(3)
- 从零开始学写HTTP服务器(二)socket编程实现简单的http server
- java网络编程——Socket和ServerSocket,套接字编程——简单聊天室
- .NET中Socket编程的简单示例
- Windows下socket简单的编程实例
- Android Socket 编程简单实现及 属性设置
- Socket编程(Unity简单案例,TCP协议)
- socket编程实现简单的http server
- 关于socket的简单的客户服务端编程
- android socket简单编程(java在PC本地创建服务器)