C、套接字 : Connection Refused error

标签 c sockets

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

这就是我所做的:

  1. 这里提到的网络IP是我配置的STATIC IP

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

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

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

我在这里忘记了什么吗?帮助!

我在此处粘贴了经过非常轻微修改(IP 和端口号不同)的 Beej 的服务器端和客户端 C 代码:

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

}

客户端.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()accept() 代码以通过调用 listen() 然后执行 accept 来“等待”连接() 接受新连接。您使用的示例没有显示如何执行此操作吗?通常,您还会为每个新连接创建一个新线程。

参见 http://www.linuxhowtos.org/C_C++/socket.htm获取更多信息。

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

关于C、套接字 : Connection Refused error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11497716/

相关文章:

c - 使用 malloc 分配 0 字节

c - 如何在 C 的 sublime text 中使用外部头文件进行编译?

c - 在从 dos 格式转换为 unix 格式的文本文件的输入流上使用 fread() 的问题

c 函数参数评估和传递

javascript - 在多个端口上运行 node.js http 服务器

sockets - 为什么HTTP客户端在三次握手的ACK报文中发送空数据?

c++ - 发送或接收结构时的字节对齐

c++ - C++ 中的 Windows VSS(卷影复制)

客户端的java swing

c - C中的套接字编程