这是我的第二步,我已经编写了一个服务器应用程序,如下所示输出客户端发送的任何内容。我有一个server.exe版本,它使用INADDR_ANY
监听任何内容,完成后我可以使用我的客户端并连接到服务器,如果我指定客户端连接到localhost(这一切正常)。
但如果我为客户端使用自己的IP地址而不是localhost,我无法连接到服务器?我不应该以这种方式连接到服务器吗?。
我使用了另一个硬编码的server.exe来使用我的IP地址而没有INADDR_ANY
但是它无法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;
}
答案 0 :(得分:4)
如果您有路由器,则必须设置Port forwarding。这实际上取决于您拥有的路由器类型,因此您必须查找具体的路由器。
您无法绑定到ISP的IP地址,因为您不拥有它(路由器会这样做)。您必须bind
到您的计算机实际拥有的IP地址,这可以通过Windows中的ipconfig /all
找到。然后确保将路由器中用于服务器端口的流量转发到计算机所具有的内部 ipaddress。
答案 1 :(得分:0)
您可以尝试使用cmd从客户端ping服务器IP以查看它是否是连接问题。
此外,我认为这个
#define PORT "3490"
客户端的是错误的,因为端口应该是一个整数。
答案 2 :(得分:0)
当您尝试连接回服务器时,您使用的是localhost / 127.0.0.1,还是您正在侦听的IP地址?
尝试连接到'localhost'可能会尝试使用您的程序未侦听的环回接口。我不是100%确定它在Windows中是如何工作的。
我是否可以提出一个更重要的问题,可能会提示您更改设计:为什么要绑定到特定的IP(和localhost)?