关于windowsOS下和linuxOS之间的socket通信

来源:互联网 发布:诈骗软件怎么举报 编辑:程序博客网 时间:2024/05/20 21:59

            先写好windows和linux各自不同版本的socket代码,就是几个头文件不一样,没有多大的区别。

            值的注意的是,我用的是xilinx ISE14.7  自带的  xilinx SDK 编译lixun端代码的,而build之后,linux端的debug中的执行文件是.elf。而在windows下,是.exe

          

            把build 好的.elf文件,拷贝到开发板的SD卡上,我这次选用的是zed board开发板。SD卡上,已经搭建好了linuxOS,其OS文件就在SD卡上。

            1.我们要用Tera Team来操作linux。把开发板用USB线和PC机连接,找到COM号,然后再Tera Team设置好相应的COM号,然后再串口中,设置好数据位宽。

            2.进入linuxOS后,我们可以通过操作,找到刚刚拷贝上去的.elf文件,然后执行。在执行之前,要配置好IP号。

 

            一些操作:

              3.“ls”:查看文件树,即列出文件
              4.“cd 文件名”:进入文件
              5.“./文件名”:运行文件
              6.“cd ../”:返回上一级
              7.“ping IP”:可以查看和IP的链接情况
              8.“ifconfig etho IP”:可以配置IP

 

    配置好这些,就可以愉快的用PC和开发板通信了。

  

  附录 linux端的socket代码,我把linux端设为服务端。

 

/* server */ 
#include <stdio.h> 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <sys/socket.h> 
#include <netdb.h> 
#include <unistd.h> 
#include <string.h> 
#include <arpa/inet.h> 
#include <sys/wait.h> 
#include <errno.h> 
 
#define PORT 6000  //define the port
#define BACKLOG 10 
 
int main(void) 

    int sockServer, sockClient; 
    struct sockaddr_in my_addr; 
    struct sockaddr_in their_addr; 
    int sin_size; 
 
    if ((sockServer = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
    { 
        fprintf(stderr, "socket error!\n"); 
  //stderr:The standard error stream is the default destination
  // for error messages and other diagnostic warnings.
          return 0; 
    } 
    my_addr.sin_family = AF_INET; 
    my_addr.sin_port = htons(PORT); 
    my_addr.sin_addr.s_addr = INADDR_ANY; 
    bzero(&(my_addr.sin_zero), 8);
 //The bzero() function sets the first n bytes
 //of the byte string s to zero. 
    if (bind(sockServer, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) 
    { 
        fprintf(stderr, "bind error!\n"); 
           return 0; 
    } 
    if (listen(sockServer, BACKLOG) == -1) 
    { 
        fprintf(stderr, "listen error!\n"); 
          return 0; 
    } 
    while (1) 
    { 
        sin_size = sizeof(struct sockaddr_in); 
        if ((sockClient = accept(sockServer, (struct sockaddr *)&their_addr, &sin_size)) == -1) 
        { 
            fprintf(stderr, "accept error!\n"); 
            continue; 
        } 
        printf("server: got connection from %s\n", inet_ntoa(their_addr.sin_addr)); 
        if (!fork()) 
        { 
        double recvData[3];
     char *pRecv = (char *)&recvData[0];//接受客户端返回的字符串
     memset(pRecv, 0, sizeof(double) * 3);

     //接收并打印客户端数据
     if(recv(sockClient, pRecv, sizeof(double) * 3, 0) == -1)
      {   
              fprintf(stderr, "recv error!\n");
     close(sockClient); 
     return 0; 
      }
     
     printf("-->\ndata1 = %lf\ndata2 = %lf\ndata3 = %lf\n\n\n", recvData[0], recvData[1], recvData[2]);
 
            if (send(sockClient, "data trans success.", sizeof("data trans success."), 0) == -1) 
    { 
             fprintf(stderr, "send error!\n"); 
     close(sockClient); 
        return 0; 
    }
        } 
        close(sockClient); 
        while(waitpid(-1, NULL, WNOHANG) > 0); 
    } 

 

0 0
原创粉丝点击