首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++服务器连接问题

C++服务器连接问题
EN

Stack Overflow用户
提问于 2013-06-02 21:18:52
回答 1查看 367关注 0票数 0

首先,我将共享的代码是我的代码的基础(在另一个站点,开源中找到的),我后来添加了函数和线程来改进。

在办公室,我们有本地网络,另外3台计算机无法连接到我的服务器。尤其是看看那条线。26010是我想听的随机端口号。根据我在其他主题中发现的数据,NULL和127.0.0.1是本地主机ip.I,我尝试了我自己的ip号而不是NULL,但是它没有工作。我可以将数据从我的客户端代码发送到其他计算机,但无法获得任何连接。

代码正在正确地侦听连接,如果我打开另一个3终端并试图通过我的客户端代码从我的计算机连接它,就可以获得信息。怎么解决这个问题?

提前谢谢。

status = getaddrinfo(NULL,"26010",&host_info,&host_info_list);

代码语言:javascript
复制
int main()
{

    int status;
    struct addrinfo host_info;       // The struct that getaddrinfo() fills up with data.
    struct addrinfo *host_info_list; // Pointer to the to the linked list of host_info's.

    memset(&host_info, 0, sizeof host_info);

    std::cout << "Setting up the structs..."  << std::endl;

    host_info.ai_family = AF_UNSPEC;     // IP version not specified. Can be both.
    host_info.ai_socktype = SOCK_STREAM; // Use SOCK_STREAM for TCP or SOCK_DGRAM for UDP.
    host_info.ai_flags = AI_PASSIVE;     

    **status = getaddrinfo(NULL, "26010", &host_info, &host_info_list);**
    // getaddrinfo returns 0 on succes, or some other value when an error occured.
    // (translated into human readable text by the gai_gai_strerror function).
    if (status != 0)  std::cout << "getaddrinfo error" << gai_strerror(status) ;


    std::cout << "Creating a socket..."  << std::endl;
    int socketfd ; // The socket descripter
    socketfd = socket(host_info_list->ai_family, host_info_list->ai_socktype,
                      host_info_list->ai_protocol);
    if (socketfd == -1)  std::cout << "socket error " ;

    std::cout << "Binding socket..."  << std::endl;

    int yes = 1;
    status = setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
    status = bind(socketfd, host_info_list->ai_addr, host_info_list->ai_addrlen);
    if (status == -1)  std::cout << "bind error" << std::endl ;

    std::cout << "Listen()ing for connections..."  << std::endl;
    status =  listen(socketfd, 5);
    if (status == -1)  std::cout << "listen error" << std::endl ;


    int new_sd;
    struct sockaddr_storage their_addr;
    socklen_t addr_size = sizeof(their_addr);
    new_sd = accept(socketfd, (struct sockaddr *)&their_addr, &addr_size);
    if (new_sd == -1)
    {
        std::cout << "listen error" << std::endl ;
    }
    else
    {
        std::cout << "Connection accepted. Using new socketfd : "  <<  new_sd << std::endl;
    }


    std::cout << "Waiting to recieve data..."  << std::endl;
    ssize_t bytes_recieved;
    char incomming_data_buffer[1000];
    bytes_recieved = recv(new_sd, incomming_data_buffer,1000, 0);
    // If no data arrives, the program will just wait here until some data arrives.
    ...


    std::cout << "send()ing back a message..."  << std::endl;     
return 0 ;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-02 22:11:31

你的问题是给getaddrinfo()打电话。此函数返回host_info_list成员中的信息。您正在阅读的是提示(host_info),它不会被getaddrinfo()更改。您需要使用host_info_list来读取getaddrinfo返回的内容。您从未使用它,也从未释放它(通过调用freeaddrinfo)。

我不知道为什么要使用getaddrinfo()来监听连接。你可以自己建造sockaddr。这很简单:

代码语言:javascript
复制
struct sockaddr_in listenAddr;
memset(&listenAddr, 0, sizeof(listenAddr));
/* IPv4 address */
listenAddr.sin_family = AF_INET;
/* your port number */
listenAddr.sin_port = htons(26010);
/* listen on all interfaces */
listenAddr.sin_addr.s_addr = htonl(INADDR_ANY);

/* TCP socket */
socketfd = socket(PF_INET, SOCK_STREAM, 0);

/* your error code, omitted here */

status = bind(SocketFD,(struct sockaddr *) &listenAddr, sizeof(listenAddr))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16887426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档