【开发手记四】核心帖一:实现ZED的网络通信

来源:互联网 发布:win8 装 ubuntu 编辑:程序博客网 时间:2024/06/10 19:55

说明:整理之前项目博客,此系列之前发表于与非网

http://www.openhw.org/module/forum/thread-552482-1-1.html

根据我们小组的设计方案,ZED采集了单片机端的数据后将数据传输者网络服务器,这就要利用ZED的以太网部分了。我们以数据库端作为服务器端,ZED端作为客户端。由于教材上没有相应的例子,自豪自己做了,在学长的帮助下,终于实现了ZED的网络通信。
实验步骤如下:
1.QT里面编写serverclient端程序,这里要求在Ubuntu中用QT编程是因为某些库Windows并不支持,程序附在本文结尾。
注:先在Ubuntu终端中键入 ifconfig指令获得其IP地址,在client端程序中会用到。如果下一次再运行这个例子,不用修改程序,只需用ifconfig指令将虚拟机的IP地址修改为与程序中一致即可。
2.arm-xilinx-linux-gnueabi-gcc编译client.cpp得到client.out
gcc编译 server.cpp得到server.out
3. client.out拷贝至ZEDSD卡的FAT分区,在虚拟机中打开minicom启动ZED板。挂载SD卡,进入client.out所在目录。
4.Ubuntu中打开新的终端,进入server.out目录执行 ./server.out
  minicom中已进入的client.out目录下执行./client.out
5.实验结果,在两个终端中会有想用的数据显示。
附录:
client.cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
 
char host_name[]="192.168.210.130";
int port=5002;
 
int main ( )
{
    int socket_descriptor;
    struct sockaddr_in pin;
 
    int i = 1;
    while(i--)
    {
    pin.sin_family=AF_INET;
    pin.sin_addr.s_addr = inet_addr(host_name);
    pin.sin_port=htons(port);
 
    if((socket_descriptor=socket(AF_INET,SOCK_STREAM,0))==-1)
    {
        perror("Error openung socket!\n");
        return 0;
    }
    if(connect(socket_descriptor,(sockaddr *)&pin,sizeof (pin))==-1)
    {
        perror("can not connecting to server!\n");
        return 0;
    }
    printf("Send message  to server ...\n");
 
    char sendBuffer[]="a100000";
 
    if(send(socket_descriptor,sendBuffer,strlen(sendBuffer),0)==-1)
    {
        perror("can not send message!\n");
        return 0;
    }
    printf("waiting for response from server!\n");
 
        char recvbuffer[502];
        memset(recvbuffer,0,502);
        int recvlength;
        recvlength=recv(socket_descriptor,recvbuffer,sizeof(recvbuffer),0);
        if(recvlength==-1)
        {
            perror("can not receive response !\n");
            return 0;
        }
        recvbuffer[recvlength] = '\0';
        printf("%s\n",recvbuffer);
    }
 
    return 1;
}
server.cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
 
int port=5002;
 
int main()
{
    struct sockaddr_in sin;
    struct sockaddr_in pin;
    int sock_descriptor;
    int temp_sock_descriptor;
    int size_of_addr;
 
    char buf[502];
    int i,lenth;
 
    sock_descriptor=socket(AF_INET,SOCK_STREAM,0);
    if(sock_descriptor==-1)
    {
        exit(1);
    }
 
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr=INADDR_ANY;
    sin.sin_port=htons(port);
 
    if(bind(sock_descriptor,(sockaddr *)&sin,sizeof(sin))==-1)
    {
        exit(1);
    }
    if(listen (sock_descriptor,20)==-1)
    {
        exit(1);
    }
    printf("Waiting for accepting connection from client!\n");
    while(1)
    {
        printf("the process is waiting here!\n");
        sockaddr_in clientAddress;
        int len  = sizeof(clientAddress);
        temp_sock_descriptor=accept(sock_descriptor, (sockaddr *)&clientAddress,(socklen_t*)&len);
        if(temp_sock_descriptor==-1)
        {
            perror("call to accept!\n");
        }
        memset(buf,0,502);
        if(recv(temp_sock_descriptor,buf,sizeof(buf),0)==-1)
        {
            perror("recv!\n");
            return 0;
        }
 
        printf("received : \n");
        printf("the recv buf is :%s\n",buf);
 
        if(send (temp_sock_descriptor,"b011111",sizeof("b011111"),0)==-1)
        {
            return 0;
        }
    }
}

0 0
原创粉丝点击