gpt4 book ai didi

C++ 服务器连接问题

转载 作者:行者123 更新时间:2023-11-28 07:35:09 26 4
gpt4 key购买 nike

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

在办公室,我们有本地网络,另外 3 台计算机无法连接到我的服务器。特别要看看那条线。 26010 是我想听的随机端口号。根据我在其他题目中查到的资料,NULL和127.0.0.1是 native ip,我试了自己的ip号,没有用NULL,还是不行。我可以将数据从我的客户端代码发送到其他计算机,但无法建立任何连接。

代码正在正确监听连接,如果我打开另一个 3 终端并尝试通过我的客户端代码从我的计算机连接它,则可以获得信息。如何解决?

提前致谢。

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

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 ;
}

最佳答案

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

我不确定您为什么要使用 getaddrinfo() 来监听连接。您可以自己构建 sockaddr。很简单:

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))

关于C++ 服务器连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16887426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com