树莓派和荔枝派,局域网socket 通信(Raspberry pie and litchi pie, LAN socket communication)

在虚拟机上面实现了socket 之间的通信之后,突发奇想,想要实现树莓派和 荔枝派zero之间的通信。

1.直接将虚拟机下面的程序复制过来,树莓派上面重新编译并运行server.c ,荔枝派上面重新编译并运行client.c 。发现是没有办法进行通信的。客户端一直报错:connect error: Connection refused 在网上查找了很多文档之后,也没有发现问题的所在。

2..在经历过各种魔改之后,终于发现了问题的所在,就是IP地址的问题。开始时,IP是 INADDR_ANY ,后来改成 inet_addr();就报错 bind error: Cannot assign requested address 。在网上查看之后,发现这个错误的缘由是IP地址不能随意填写。需要使服务器端的主机IP地址,然后改成服务器端的主机IP地址之后,就能够正常通信了。

注:我的服务端程序所在主机IP就是 192.168.2.102

#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include <net/ethernet.h>
#include <string.h>
#define IP_PORT 9527
int main(void)
{
    ssize_t len;
    char buf[100];
    int ret;
    int sock_fd ,acc_fd;
    socklen_t addr_len;
    struct sockaddr_in con_addr ,acc_addr;
    sock_fd =socket(AF_INET,SOCK_STREAM,0);
    if(sock_fd <0)
    {
        perror("socket error");
        exit(1);
    }
    con_addr.sin_family = AF_INET; //IPv4
    con_addr.sin_addr.s_addr = inet_addr("192.168.2.102");
    con_addr.sin_port = htons(IP_PORT);
    ret = connect(sock_fd,(struct sockaddr*)&con_addr,sizeof(con_addr));
    if(ret <0)
    {
        perror("connect error");
        exit(1);
    }

    while(1)
    {
        memset(buf,0,100);
        read(STDIN_FILENO,buf,100);
        if(len <0)
        {
            perror("read error");
            exit(1);
        }
        len = write(sock_fd,buf,sizeof(buf));
        if(len <0)
        {
            perror("write error");
            exit(1);
        }
        len =read(sock_fd,buf,100);
        if(len <0)
        {
            perror("read error");
            exit(1);
        }
        printf("%s",buf);

    }
    return 0;
}
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <pthread.h>

#define SERVER_PORT 9527

void *server_handle(void *argc)
{
    int con_fd =*((int*)argc);
    ssize_t len;
    char buf[100];
    while(1)
    {
        len  = read(con_fd,buf,100);
        if(len <0)
        {
            perror("read error");
            exit(1);
        }
        else if(len ==0)
        {
           break;
        }
        len = write(con_fd,buf,len);
        if(len <0)
        {
            perror("write error");
            exit(1);
        }
    }
    close(con_fd);
    pthread_exit(NULL);
}
int main(void)
{
    int sock_fd,acc_fd;
    int ret;
    socklen_t sock_len;
    pthread_t thread_id;
    struct sockaddr_in bind_addr, acc_addr;
    sock_fd =socket(AF_INET,SOCK_STREAM,0);
    if(sock_fd <0)
    {
        perror("socket error");
        exit(1);
    }

    bind_addr.sin_port =htons(SERVER_PORT);
    bind_addr.sin_family = AF_INET;
    bind_addr.sin_addr.s_addr = inet_addr("192.168.2.102");
    ret = bind(sock_fd,(struct sockaddr*)&bind_addr,sizeof(bind_addr));
    if(ret <0)
    {
        perror("bind error");
        exit(1);
    }
    ret = listen(sock_fd,20);
    if(ret < 0)
    {
        perror("listen error");
        exit(1);
    }
    while(1)
    {
        acc_fd = accept(sock_fd,(struct sockaddr *)&acc_addr,&sock_len);
        if(acc_fd < 0)
        {
            perror("accpet error");
            exit(1);
        }
        pthread_create(&thread_id,NULL,server_handle,(void *)&acc_fd);
        pthread_detach(thread_id);
    }
    close(sock_fd);
    return 0;
}
————————

After realizing the communication between sockets on the virtual machine, I had a whim to realize the communication between raspberry pie and litchi pie zero.

1. Directly copy the program under the virtual machine, recompile it on the raspberry pie and run the server c. Recompile the litchi pie and run client c 。 It was found that there was no way to communicate. The client keeps reporting errors: connect error: connection rejected. After searching many documents on the Internet, the problem is not found.

2.. After going through various magic changes, I finally found the problem, that is, the problem of IP address. At the beginning, the IP is inaddr_ Any, later changed to INET_ addr(); Bind error: cannot assign requested address. After checking on the Internet, I found that the reason for this error is that the IP address can not be filled in at will. You need to change the host IP address of the server to the host IP address of the server, and then you can communicate normally.

Note: the host IP of my server program is 192.168.2.102

#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include <net/ethernet.h>
#include <string.h>
#define IP_PORT 9527
int main(void)
{
    ssize_t len;
    char buf[100];
    int ret;
    int sock_fd ,acc_fd;
    socklen_t addr_len;
    struct sockaddr_in con_addr ,acc_addr;
    sock_fd =socket(AF_INET,SOCK_STREAM,0);
    if(sock_fd <0)
    {
        perror("socket error");
        exit(1);
    }
    con_addr.sin_family = AF_INET; //IPv4
    con_addr.sin_addr.s_addr = inet_addr("192.168.2.102");
    con_addr.sin_port = htons(IP_PORT);
    ret = connect(sock_fd,(struct sockaddr*)&con_addr,sizeof(con_addr));
    if(ret <0)
    {
        perror("connect error");
        exit(1);
    }

    while(1)
    {
        memset(buf,0,100);
        read(STDIN_FILENO,buf,100);
        if(len <0)
        {
            perror("read error");
            exit(1);
        }
        len = write(sock_fd,buf,sizeof(buf));
        if(len <0)
        {
            perror("write error");
            exit(1);
        }
        len =read(sock_fd,buf,100);
        if(len <0)
        {
            perror("read error");
            exit(1);
        }
        printf("%s",buf);

    }
    return 0;
}
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <pthread.h>

#define SERVER_PORT 9527

void *server_handle(void *argc)
{
    int con_fd =*((int*)argc);
    ssize_t len;
    char buf[100];
    while(1)
    {
        len  = read(con_fd,buf,100);
        if(len <0)
        {
            perror("read error");
            exit(1);
        }
        else if(len ==0)
        {
           break;
        }
        len = write(con_fd,buf,len);
        if(len <0)
        {
            perror("write error");
            exit(1);
        }
    }
    close(con_fd);
    pthread_exit(NULL);
}
int main(void)
{
    int sock_fd,acc_fd;
    int ret;
    socklen_t sock_len;
    pthread_t thread_id;
    struct sockaddr_in bind_addr, acc_addr;
    sock_fd =socket(AF_INET,SOCK_STREAM,0);
    if(sock_fd <0)
    {
        perror("socket error");
        exit(1);
    }

    bind_addr.sin_port =htons(SERVER_PORT);
    bind_addr.sin_family = AF_INET;
    bind_addr.sin_addr.s_addr = inet_addr("192.168.2.102");
    ret = bind(sock_fd,(struct sockaddr*)&bind_addr,sizeof(bind_addr));
    if(ret <0)
    {
        perror("bind error");
        exit(1);
    }
    ret = listen(sock_fd,20);
    if(ret < 0)
    {
        perror("listen error");
        exit(1);
    }
    while(1)
    {
        acc_fd = accept(sock_fd,(struct sockaddr *)&acc_addr,&sock_len);
        if(acc_fd < 0)
        {
            perror("accpet error");
            exit(1);
        }
        pthread_create(&thread_id,NULL,server_handle,(void *)&acc_fd);
        pthread_detach(thread_id);
    }
    close(sock_fd);
    return 0;
}