在两个设备之间与TCP通信时如何解决错误“连接被拒绝”

时间:2019-05-01 20:50:05

标签: c++ sockets tcp pthreads beagleboneblack

因此,我目前正在尝试通过TCP从运行debian的beaglebone black到mac进行通信。我想从beaglebone到mac进行通信,并在两者之间发送文本消息。设置代码的方式是,我有一个主连接,使用UDP连接在端口1000上传输视频,这本身可以正常工作,但是我已经建立了基本的TCP连接以通过端口发送文本10001在一个单独的pthread中,并且在两个设备之间进行连接时,它会为我提供输出

ERROR connecting -1 Connection refused 111

如果我打印出上面说的值

Port: 10001, name: 192.168.1.37

对我来说特别有趣的是,我知道IP地址正确,因为我可以使用IP地址和端口10000进行视频流传输。

总体设置是,我有一个连接到路由器的beaglebone微控制器(基本上是树莓派),而我的mac通过以太网连接到同一路由器。我正在使用“ sys / socket.h”库

TCPClient代码(在Macbook端)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

class TCPClient {

private:
    int sockfd;
    struct sockaddr_in serv_addr;
    struct hostent *server;
    char buffer[512];

    void createSocket() {

        sockfd = socket(AF_INET, SOCK_STREAM, 0);

        if (sockfd < 0) {
            printf("ERROR opening socket\n");
            exit(0);
        }
    }

    void createConnection() {

        memcpy((char *)server->h_addr_list[0],
               (char *)&serv_addr.sin_addr.s_addr,
               server->h_length);

        int er = connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
        if (er < 0) {
            printf("ERROR connecting %i %s\n", er, strerror(errno));
            exit(0);
        }
    }

public:
    TCPClient(int port, char* name) {

        memset((char *) &serv_addr, 0, sizeof(serv_addr));

        printf("Port: %i, name: %s\n", port, name);

        serv_addr.sin_port = htons(port);

        server = gethostbyname(name);
        if (server == NULL) {
            fprintf(stderr,"ERROR, no such host\n");
            exit(0);
        }

        serv_addr.sin_family = AF_INET;

        createSocket();

        createConnection();

    }

    ~TCPClient() {
        close(sockfd);
    }

    void transmitData(char data[]) {

        int n = write(sockfd, data, strlen(data));
        if (n < 0) {
            printf("ERROR writing to socket\n");
            exit(0);
        }
    }

    char* receiveData() {
        memset(buffer, 0, sizeof(buffer));

        int n = read(sockfd, buffer, sizeof(buffer));
        if (n < 0) {
            printf("ERROR reading from socket\n");
            exit(0);
        }

        return buffer;
    }

};

TCPServer代码(微控制器)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

class TCPServer {

private:
    // struct for data
    int sockfd, newsockfd;
    socklen_t clilen;
    char buffer[512];
    struct sockaddr_in serv_addr, cli_addr;

public:
    TCPServer(int port) {

        // clear address structure
        memset((char *) &serv_addr, 0, sizeof(serv_addr));

        // setup the host_addr structure for use in bind call
        // server byte order
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_port = htons(port);
        // automatically be filled with current host's IP address
        serv_addr.sin_addr.s_addr = INADDR_ANY;

        // Create socket
        // socket(int domain, int type, int protocol)
        // i.e. IPV_4, TCP, default
        sockfd = socket(AF_INET, SOCK_STREAM, 0);

        if (sockfd < 0) {
            printf("ERROR opening socket\n");
            exit(0);
        }

        // Create bind
        if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
            printf("ERROR on binding\n");
            exit(0);
        }

        // start listening
        // This listen() call tells the socket to listen to the incoming connections.
        // The listen() function places all incoming connection into a backlog queue
        // until `() call accepts the connection.
        // Here, we set the maximum size for the backlog queue to 5.
        listen(sockfd, 5);

        // The accept() call actually accepts an incoming connection
        clilen = sizeof(cli_addr);

        // This accept() function will write the connecting client's address info
        // into the the address structure and the size of that structure is clilen.
        // The accept() returns a new socket file descriptor for the accepted connection.
        // So, the original socket file descriptor can continue to be used
        // for accepting new connections while the new socker file descriptor is used for
        // communicating with the connected client.
        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
        printf("Ending accept function\n");

        if (newsockfd < 0) {
            printf("ERROR on accept");
            exit(0);
        }

        printf("server: got connection from %s port %d\n",
               inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));

    }

    ~TCPServer() {
        close(newsockfd);
        close(sockfd);
    }

    void createSocket() {
        // create a socket
        // socket(int domain, int type, int protocol)
        // i.e. IPV_4, TCP, default
        sockfd = socket(AF_INET, SOCK_STREAM, 0);

        if (sockfd < 0) {
            printf("ERROR opening socket\n");
            exit(0);
        }

    }

    void transmitData(char data[]) {
        // This send() function sends the 13 bytes of the string to the new socket
        send(newsockfd, data, strlen(data), 0);

    }

    char* receiveData() {

        memset(buffer, 0, sizeof(buffer));

        int n = read(newsockfd, buffer, sizeof(buffer));
        if (n < 0) {
            printf("ERROR reading from socket\n");
            exit(0);
        }

        return buffer;

    }

};

这是我用来创建连接的代码;这是在我的台式计算机上运行的部分(接收连接并显示输出代码)

void* sensor_com(void* conn_data_void) {
    struct conn *conn_data = (struct conn*)conn_data_void;

    printf("printing from pthread\n");
    printf("port: %d \n", (*conn_data).port);

    TCPServer *sensor_com = new TCPServer(conn_data->port);

    char *buf = (char*) malloc(sizeof(char)*256);

    while(1) {

        // receive data from ROV and translate json
        char *buffer = (*sensor_com).receiveData();
        std::string json(buffer);

        picojson::value v;

        std::string err = picojson::parse(v, json);
        if (! err.empty()) {
        std:cerr << err << std::endl;
        }

        // check if the type of the value is "object"
        if (! v.is<picojson::object>()) {
            std::cerr << "JSON is not an object" << std::endl;
            exit(2);
        }

        std::map<std::string, string> data;

        // obtain a const reference to the map, and print the contents
        const picojson::value::object& obj = v.get<picojson::object>();
        for (picojson::value::object::const_iterator i = obj.begin();
             i != obj.end();
             ++i) {
            data[i->first] = i->second.to_str();
        }

        cout << "accel_x of rov: " << data["accel_x"] << endl;
        cout << "accel_y of rov: " << data["accel_y"] << endl;
        cout << "accel_z of rov: " << data["accel_z"] << endl;
        cout << "gyro_x of rov: " << data["gyro_x"] << endl;
        cout << "gyro_y of rov: " << data["gyro_y"] << endl;
        cout << "gyro_z of rov: " << data["gyro_z"] << endl;

    }

    free(buf);

    return NULL;

}

这是微控制器/ beaglebone上的部分

void * sensor_com(void * conn_data_void){

struct conn *conn_data = (struct conn*)conn_data_void;
unsigned int port = (*conn_data).port;

printf("printing from pthread\n");
printf("port: %d \n", port);

// create TCP connection includeing socket and connection call
TCPClient *sensor_com = new TCPClient(conn_data->port, conn_data->host);

char* val;

// Data from MPU6050
struct sensor_data movement;

if (RUNNING_ON_BEAGLEBONE) {
    initialize_i2c();
}

// send data to master forever
while (1) {

    if (RUNNING_ON_BEAGLEBONE) {
        // collect data from I2C_interface
        get_data(&movement);
    } else {
        // get data here
        movement.accel_x = 10;
        movement.accel_y = 10;
        movement.accel_z = 10;
        movement.gyro_x = 10;
        movement.gyro_y = 10;
        movement.gyro_z = 10;
    }
    // Create json for data to be stored
    string data = "{";

    // Add accelleration data
    data += "\"accel_x\": \"" + std::to_string(movement.accel_x) + "\",";
    data += "\"accel_y\": \"" + std::to_string(movement.accel_y) + "\",";
    data += "\"accel_z\": \"" + std::to_string(movement.accel_z) + "\",";

    // Add gyroscopic dataa
    data += "\"gyro_x\": \"" + std::to_string(movement.gyro_x) + "\",";
    data += "\"gyro_y\": \"" + std::to_string(movement.gyro_y) + "\",";
    data += "\"gyro_z\": \"" + std::to_string(movement.gyro_z) + "\"}";

    // convert to char array
    char *charData = new char [data.length()+1];
    strcpy(charData, data.c_str());

    (*sensor_com).transmitData(charData);

    // delay for readability
    sleep(2);

}

free(val);

return NULL;

}

我为代码墙表示歉意,尽管坦率地说,我认为问题与两者之间的联系有关,所以我不知道它是否有用。当代码在同一台计算机上时,它可以很好地工作(仅在存在问题的计算机之间进行通信)。我也尝试过使用tcpdump,但是我不知道如何使用tcpdump将数据包发送到另一台服务器,我开始认为它仅用于侦听数据包,在这种情况下效果不是很好。

任何指导将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

所以我有点想出了这个问题,这在很大程度上要感谢@ user4581301

我最终尝试重建可能的最简单版本,结果发现我基于很多代码的原始源出了问题(我找不到链接,但这只是一个教程) )。它可以在任何一台机器上独立工作,但是如果您尝试在两台机器之间发送数据,它将无法正常工作(可能与beaglebone上的debian os和笔记本电脑上的mac os之间的不同库有关,或者只是其中一些功能折旧)。

我最终从该站点(http://man7.org/linux/man-pages/man3/getaddrinfo.3.html)重建了TCP连接,该站点使用了更新的技术,并且能够跨平台正常工作。所以我想我不太确定问题是什么,但是如果您在跨平台交流时遇到问题,我建议您看一下man7链接。

感谢大家的帮助!