Chinaunix首页 | 论坛 | 博客
  • 博客访问: 174460
  • 博文数量: 35
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 305
  • 用 户 组: 普通用户
  • 注册时间: 2016-02-01 12:35
个人简介

不断超越自己,将更强大!

文章分类

全部博文(35)

文章存档

2022年(1)

2017年(5)

2016年(29)

我的朋友

分类: 嵌入式

2016-03-22 22:55:30

        其实嵌入式的Linux 与PC Linux共用一套内核,实现网络通信,只是编译器不一样,运行效果一样,因为测试也比较方便,可以在PC Linux下测试好,再用交叉编译器编译成嵌入式的运行程序,放到嵌入式板子上运行。

以上为实现UDP服务器端的源码,很简单,当然,嵌入式的板子需要支持网络,配置好IP等。


点击(此处)折叠或打开

  1. /*******************************************************************************
  2. * 文件名称: socket.c
  3. * 描 述: UDP服务端测试程序
  4. *******************************************************************************/

  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <netdb.h>
  10. #include <sys/types.h>
  11. #include <netinet/in.h>
  12. #include <sys/socket.h>


  13. #define UDP_SERVER_PORT        888
  14. typedef unsigned char     BYTE;

  15. void UdpServer();



  16. main(int argc,char *argv[]){
  17.     

  18.     UdpServer();

  19. }



  20. /***************************************************************
  21.  * 函数名: UdpServer()
  22.  * 描述 : UDP 服务器程序:接收到客户端的UDP连接,
  23.  *                        打印接收到的数据。
  24.  * 参数 : NO
  25.  * 返回 : NO
  26.  ***************************************************************/
  27. void UdpServer ()
  28. {
  29.     unsigned int i=0;
  30.     int            sock = 0;                         //socket fd's
  31.     struct sockaddr_in    serverAddr; // server's address
  32.     struct sockaddr_in     clientAddr;     // client's address
  33.     int        sockAddrSize=0;
  34.     
  35.     char    acRevBuf[512];                         //接收数据缓冲区
  36.     int        iRevLength = 0;                 //接收数据长度
  37.     char    acSndBuf[512];                         //接收数据缓冲区
  38.     int        iSndLength = 0;                 //接收数据长度

  39.   for(;;)
  40.     {
  41.             //清空缓冲区
  42.       sockAddrSize = sizeof(struct sockaddr_in);
  43.       bzero ((char *)&serverAddr, sockAddrSize);
  44.       bzero ((char *)acRevBuf,sizeof(acRevBuf));
  45.       bzero    ((char *)acSndBuf,sizeof(acSndBuf));
  46.       
  47.             printf ("UDP Server Starting!\r\n");
  48.    
  49.      serverAddr.sin_family = AF_INET;
  50.      serverAddr.sin_port = htons(UDP_SERVER_PORT        /*    UDP Server Port*/);
  51.      serverAddr.sin_addr.s_addr = INADDR_ANY;

  52.      //创建 UDP SOCKET
  53.      sock = socket (AF_INET, SOCK_DGRAM, 0);
  54.      if (sock == -1)
  55.      {
  56.                 usleep(20);        //创建socket出错!!
  57.                 continue;
  58.      }
  59.             printf ("UDP Server Socket OK!\r\n");

  60.      if (bind (sock, (struct sockaddr *)&serverAddr, sockAddrSize)== -1)        //bind socket to local address
  61.      {
  62.                 printf ("bind failed, errno = %d\n", errno);
  63.                 perror(strerror(errno));
  64.                 close (sock);
  65.                 return;
  66.                 exit (1);
  67.      }
  68.         
  69.             printf ("UDP Server Bind OK!\r\n");
  70.             printf("Server prot : %d\n",ntohs(serverAddr.sin_port));
  71.             
  72.          for (;;)
  73.             {
  74.                 iRevLength = 0;
  75.                 iRevLength = recvfrom(sock,acRevBuf,sizeof(acRevBuf),0,(struct sockaddr *)&clientAddr,&sockAddrSize);

  76.                 if (iRevLength == -1) //客户端退出
  77.                 {
  78.                     break;
  79.                 }
  80.                 if (iRevLength == 0)
  81.                 {
  82.                     continue;
  83.                 }

  84.              int clientip = clientAddr.sin_addr.s_addr; //打印客户端IP地址与Port
  85.              printf("\nClient ip : %d.%d.%d.%d ",clientip&255,(clientip>>8)&255, (clientip>>16)&255,(clientip>>24)&255);
  86.              printf("\nClient port : %d\n",ntohs(clientAddr.sin_port));

  87.     
  88.                 iSndLength = iRevLength;
  89.                 for(i=0;i<iRevLength;i++)
  90.                 {
  91.                     acSndBuf[i]=acRevBuf[i];
  92.                 }
  93.                 sendto(sock,acSndBuf,iSndLength,0,(struct sockaddr *)&clientAddr,sizeof(clientAddr));
  94.                 for(i=0;i<iRevLength;i++)
  95.                 {
  96.                     printf("%02x ",(BYTE)acRevBuf[i]);
  97.                     if(i%16==15)
  98.                         printf("\r\n");
  99.                 }
  100.             }
  101.         }
  102.     close (sock);
  103. }


    Makefile就比较容易了,因为只有一个文件。


点击(此处)折叠或打开

  1. all:
  2.     gcc UDP.c -o Server_UDP
  3. clean:
  4.     rm -rf *.o Server_UDP


嵌入式端的makefile

点击(此处)折叠或打开

  1. all:
  2.     arm-none-linux-gnueabi-gcc UDP.c -o Server_UDP
  3. clean:
  4.     rm -rf *.o Server_UDP

        编译方法:直接# make
生所可执行程序,如果放到嵌入式的板子上运行,需要设置一下执行权限
# chmod 777 Server_UDP
# ./Server_UDP    //即可运行。

        然后就可以用网络助手连接板子,进行测试了,这里只是实现了数据收发,如果接收不成功,注意UDP的端口,要设置正确。因为UDP不是面向连接的,服务器在不在,都一样可以发送出去,但是,只有端口正确,才能正确的接收到并返回。





阅读(2805) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~