gpt4 book ai didi

c++ - 连接到 IP 地址而不是本地主机?

转载 作者:行者123 更新时间:2023-11-30 02:10:52 24 4
gpt4 key购买 nike

这是我的第二次尝试,我编写了一个服务器应用程序,如下所示,它输出客户端发送的任何内容。我有一个 server.exe 版本,它使用 INADDR_ANY 监听任何内容,完成后我可以使用我的客户端并连接到服务器,如果我指定客户端连接到 localhost(一切正常)。

但是如果我使用我自己的 IP 地址而不是客户端的本地主机,我无法连接到服务器?。我不应该能够以这种方式连接到服务器吗?

我使用了另一个 server.exe,它被硬编码为在没有 INADDR_ANY 的情况下使用我的 IP 地址,但它无法 bind() listen() 传入连接。我对如何继续感到困惑。

(我在同一台机器上运行服务器和客户端,这是个问题吗?)

服务器

#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
using namespace std;

#define PORT 3490
#define IP "118.93.0.164"
#define BACKLOG 10

const int winsockversion = 2;

int main(void){
WSADATA wsadata;
if ( (WSAStartup(MAKEWORD(winsockversion,0),&wsadata)) == 0){
cout<<"-WSAStartup Initialized." << endl;

struct sockaddr_in serv;
memset(&serv,0,sizeof serv);
serv.sin_family = AF_INET;
serv.sin_port = htons(PORT);
serv.sin_addr.s_addr = inet_addr(IP);//INADDR_ANY;//inet_addr(IP);
//---------------------------------------
struct addrinfo serv_addrinfo;
serv_addrinfo.ai_family = AF_INET;
serv_addrinfo.ai_socktype = SOCK_STREAM;
serv_addrinfo.ai_protocol = IPPROTO_TCP;
serv_addrinfo.ai_addrlen = sizeof(serv);
serv_addrinfo.ai_addr = (sockaddr*)&serv;


//---------------------------------------
SOCKET serv_con;
serv_con = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (serv_con != INVALID_SOCKET){
cout<<"-Server Socket created." << endl;
}

if (bind(serv_con,serv_addrinfo.ai_addr,serv_addrinfo.ai_addrlen) != -1){
cout<<"-Binding Successful." << endl;
}

if( listen(serv_con,BACKLOG) != -1){
cout<<"-Listening for incoming connections." << endl;
}
//--------------------------------------------------------------------
SOCKET recv_socket;
struct sockaddr_in client_info;
int client_info_size = sizeof(client_info);

char *con_addr = inet_ntoa(client_info.sin_addr);




recv_socket = accept(serv_con,(sockaddr*)&client_info,&client_info_size);

if( recv_socket != INVALID_SOCKET ){
cout<<"-Connection Established!. " << endl;
cout<<"-Connected to: [" << con_addr << "] " << endl;
//----------------------------------------------------------------------
char buffer[80];
int bytes_in;
while(true){

bytes_in = recv(recv_socket,buffer,sizeof(buffer),0);
if ( bytes_in > 0 ){
cout<<"[" << con_addr << "]" << buffer << endl;
}

if (bytes_in == 0 ){
cout<<"[" << con_addr << "] has disconnected." << endl;
break;
}
if (bytes_in == -1 ){
cout<<"-Possible Abrupt disconnecton from [" << con_addr << "]" << endl;
break;
}
}

closesocket(recv_socket);
closesocket(serv_con);

}

}else{
cout<<"-WSAStartup Initialization failed." << endl;
}

if( WSACleanup()!= -1){
cout<<"-WSACleanp Successful." << endl;
}

WSAGetLastError();


return 0;
}

客户

/*client*/
#define _WIN32_WINNT 0x501
#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
using namespace std;

#define PORT "3490"
#define SERVER "118.93.0.164" // and localhost
const int winsockVersion = 2;


int main(void){

WSADATA wsadata;
if ( (WSAStartup(MAKEWORD(2,0),&wsadata)) == 0){
cout<<"-WSAStartup Initialized." << endl;

struct addrinfo hints, *res;
int sockfd;

memset(&hints,0,sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

if (getaddrinfo(SERVER,PORT,&hints,&res) != 0){
cout<<"-getaddrinfo unsuccessful." << endl;
}

if ( (sockfd = socket(res->ai_family,res->ai_socktype,res->ai_protocol)) == -1 ){
cout<<"-Unable to create socket." << endl;
}

if ( (connect(sockfd,res->ai_addr,res->ai_addrlen)) != -1 ){
cout<<"-Connection Established." << endl;
}

cout<<"-Client connecting to: " << res->ai_addr << endl;

while(true){
string text_buff;
cout<<"Enter text: ";
getline(cin,text_buff);
if( (send(sockfd,text_buff.c_str(),text_buff.length()+1,0)) != -1 ){
cout<<"-text_buff sent!." << endl;
}

}

}else{
cout<<"-WSAStartup Initialization failed." << endl;
if(WSACleanup()!=0){
cout<<"-WSACleanup Successful." << endl;
}else{
cout<<"-WSACleanup Failed." << endl;
}
}

return 0;
}

最佳答案

如果您有路由器,则必须设置 Port forwarding .这实际上取决于您拥有的路由器类型,因此您必须查找您的特定路由器。

您不能绑定(bind)到您的 ISP 的 IP 地址,因为您不拥有它(路由器拥有)。您必须绑定(bind)到您的计算机实际拥有的IP地址,该地址可通过windows中的ipconfig/all找到。然后确保将路由器中服务器端口号的流量转发到计算机拥有的内部 ip 地址。

关于c++ - 连接到 IP 地址而不是本地主机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4263913/

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