C、套接字:连接被拒绝错误

2023-12-22

我有一个数据采集模块,我想从中收集以太网端口的数据。我正在一步步到达那里,目前我只想connect从客户端到服务器。我使用 Beej 的指南来获取基本的 C 代码。但我不断收到此连接错误connect: Connection refused.

这就是我所做的:

  1. 这里提到的网络IP是静态IP我已经配置了。

  2. 服务器端的端口号设置为 50000,我从客户端通过端口 50000 连接到该 IP。

  3. 我构建并运行服务器端应用程序,然后尝试通过运行客户端应用程序来连接到它。

服务器端的一个疑惑,服务器端应用returns在启动客户端应用程序之前,我应该保持它运行吗(while(1);)以便我可以从客户端连接到它?

出了什么问题,我在这里忘记了什么吗?帮助!

我粘贴了稍微修改过的(IP 和端口号不同)Beej 的 C 代码服务器端和客户端 here:

Server.c

/*
** server.c
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>



int main(int argc, char *argv[])
{
    // code for a server waiting for connections
    // namely a stream socket on port 3490, on this host's IP
    // either IPv4 or IPv6.
    int sockfd;
    struct addrinfo hints, *servinfo, *p;
    int rv;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE; // use my IP address

    if ((rv = getaddrinfo(NULL, "50000", &hints, &servinfo)) != 0)
    {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        exit(1);
    }

    // loop through all the results and bind to the first we can
    for(p = servinfo; p != NULL; p = p->ai_next) 
    {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
        p->ai_protocol)) == -1) 
        {
            perror("socket");
            continue;
        }
        if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1)
        {
            close(sockfd);
            perror("bind");
            continue;
        }
        break; // if we get here, we must have connected successfully
    }

    if (p == NULL) 
    {
        // looped off the end of the list with no successful bind
        fprintf(stderr, "failed to bind socket\n");
        exit(2);
    }

    freeaddrinfo(servinfo); // all done with this structure

}

Client.c

/*
** client.c
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>

int main(int argc, char *argv[])
{
    // code for a client connecting to a server
    // namely a stream socket to www.example.com on port 80 (http)
    // either IPv4 or IPv6
    int sockfd;
    struct addrinfo hints, *servinfo, *p;
    int rv;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
    hints.ai_socktype = SOCK_STREAM;
    if ((rv = getaddrinfo("192.168.2.4", "50000", &hints, &servinfo)) != 0) 
    {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        exit(1);
    }
    // loop through all the results and connect to the first we can
    for(p = servinfo; p != NULL; p = p->ai_next)
    {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
        p->ai_protocol)) == -1) 
        {
            perror("socket");

        continue;
        }
        if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) 
        {
            close(sockfd);
            perror("connect");
            continue;
        }
        break; // if we get here, we must have connected successfully
    }

    if (p == NULL)
    {
        // looped off the end of the list with no connection
        fprintf(stderr, "failed to connect\n");
        exit(2);
    }

    freeaddrinfo(servinfo); // all done with this structure

}

您的服务器代码丢失listen() and accept()代码通过调用listen()“等待”连接,然后执行accept()接受新的连接。您使用的示例没有显示如何做到这一点吗?通常,您还会为每个新连接创建一个新线程。

See http://www.linuxhowtos.org/C_C++/socket.htm http://www.linuxhowtos.org/C_C++/socket.htm了解更多信息。

这是更完整的实现的链接:http://www.thegeekstuff.com/2011/12/c-socket-programming/ http://www.thegeekstuff.com/2011/12/c-socket-programming/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C、套接字:连接被拒绝错误 的相关文章

随机推荐