Ich habe einen Server auf einem Computer ausgeführt und den Port, den es verwendet, an meinen Router weitergeleitet, und einen anderen Computer, der den Client unter Verwendung meiner externen IP-Adresse mit dem Server verbindet Adresse anstelle der lokalen Adresse. Das alles funktioniert gut und es verbindet sich aber wenn ich die Adresse des angeschlossenen Sockets (Client) überprüfe, ist die IP-Adresse, die es anzeigt, völlig anders? es zeigt mir 148.49.68.0
. Ich kann das nicht auf ipconfig finden und verstehe nicht, woher es kommt. Sollte der Kunde meine externe Adresse nicht anzeigen? (Da beide Computer dieselbe externe IP-Adresse verwenden).Ermitteln der IP-Adresse eines verbundenen Clients auf dem Server
[EDIT] hinzugefügt Serverquelle
#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <string.h>
using namespace std;
int PORT;
const int winsock_version = 2;
const int max_con = 10;
string SERVER_ADDRS;
void Bind(SOCKET &serv,struct sockaddr_in &serv_info,int size);
void Listen(SOCKET &serv,int max_con);
void connection_info(struct sockaddr_in &client);
bool communication(SOCKET &client);
SOCKET Accept(SOCKET &serv);
int main(void){
WSADATA wsadata;
if (WSAStartup(MAKEWORD(winsock_version,0),&wsadata) == 0){
cout<<"-[Initialized.]" << endl;
cout<<"-[Server Address (leave blank to scan for all IP's)]: ";
getline(cin,SERVER_ADDRS);
cout<<"-[Port]: ";
cin>>PORT;
struct sockaddr_in serv_info;
serv_info.sin_family = AF_INET;
serv_info.sin_port = htons(PORT);
if(sizeof(SERVER_ADDRS) > 5){
cout<<"-[Listening on: " << SERVER_ADDRS << "]" << endl;
serv_info.sin_addr.s_addr = inet_addr(SERVER_ADDRS.c_str());
}else{
cout<<"-[Scanning for All IP's]" << endl;
serv_info.sin_addr.s_addr = INADDR_ANY;
}
SOCKET serv;
serv = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (serv != INVALID_SOCKET){
//------------------------------------------------------------
Bind(serv,serv_info,sizeof(serv_info));
Listen(serv,max_con);
struct sockaddr_in client_info;
int size = sizeof(client_info);
SOCKET client_sock = Accept(serv);
connection_info(client_info);
if (communication(client_sock) == true){
closesocket(serv);
closesocket(client_sock);
}
//------------------------------------------------------------
}
}else{
cout<<"-[Initialization failed, running cleanup.]" << endl;
}
if (WSACleanup() == 0){
cout<<"-[Cleanup Successful.]" << endl;
}
return 0;
}
void Bind(SOCKET &serv,struct sockaddr_in &serv_info,int size){
if (bind(serv,(sockaddr*)&serv_info,size) != -1){
//Binding complete, now clear the port and allow for reuse if needed using setsockopt
char yes = '1';
if (setsockopt(serv,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) != SOCKET_ERROR){
cout<<"-[Binding Successful.]" << endl;
}
}
}
void Listen(SOCKET &serv,int max_con){
if (listen(serv,max_con) != -1){
cout<<"-[Listening for connections.] " << endl;
}
}
SOCKET Accept(SOCKET &serv){
struct sockaddr_in client_info;
int size = sizeof(client_info);
SOCKET recv;
recv = accept(serv,(sockaddr*)&client_info,&size);
if (recv != INVALID_SOCKET) {
return recv;
}else{
cout<<"-[Invalid Socket.]" << endl;
}
}
void connection_info(struct sockaddr_in &client){
char *connected_ip= inet_ntoa(client.sin_addr);
int port = ntohs(client.sin_port);
cout<<"-[IP:" << connected_ip <<", Connected on PORT:"<< port << "]"<< endl;
}
bool communication(SOCKET &client){
cout<<"[---------------{CHAT}---------------]" << endl;
int bytes_in;
int bytes_out;
char recvd_text[80];
string send_text;
while(true){
cout<<"-[SERVER]: ";
getline(cin,send_text);
if (sizeof(send_text) > 0){
bytes_out = send(client,send_text.c_str(),send_text.length()+1,0);
cout<< endl;
if (bytes_out == SOCKET_ERROR){
cout<<"-[SERVER error in sending.]" << endl;
break;
}
}
bytes_in = recv(client,recvd_text,sizeof(recvd_text),0);
if (bytes_in > 0){
cout<<"-[CLIENT]: " << recvd_text << endl; //output on screen
}
if (bytes_in == 0){
cout<<"-[CLIENT has disconnected.]" << endl;
break;
}
if (bytes_in == SOCKET_ERROR){
cout<<"-[CLIENT closed unexpectedly.]" << endl;
break;
}
}
return true;
}
148.49.68.0 => DoD-Netzwerk-Informationszentrum, Columbus OH. –