问题
this is my second go at this, I've written a server application as below which outputs whatever the client sends. I have one server.exe version where it listens to anything using INADDR_ANY
, Having done that I can use my client and connect to the server if I specify the client connect to localhost (Which all works fine).
but if I use my own IP address instead of localhost for the client, I cannot connect to the server?. Shouldn't I be able to connect to the server this way?.
I used another server.exe which was hard-coded to use my IP address without having INADDR_ANY
but it wasn't able to bind()
or listen()
to incoming connections. I'm confused as to how to proceed.
(I'm running the server and client on the same machine, is that an issue?)
server
#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
/*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;
}
回答1:
If you have a router you will have to set up Port forwarding. It really depends on what kind of router you have, so you will have to look it up for your specific one.
You cannot bind to your ISP's IP address, since you do not own it (the router does). You have to bind
to an IP address that your computer actually owns, which is found via ipconfig /all
in windows. Then make sure to forward traffic in your router for your servers portnumber, to the internal ipaddress that your computer has.
回答2:
When you're trying to connect back to the server, are you using localhost/127.0.0.1, or the IP address you're listening on?
It may be that trying to connect to 'localhost' attempts to use the loopback interface, which your program isn't listening on. I'm not 100% sure how it works in Windows.
Can I ask a more important question that may prompt you to change your design: why do you want to bind to a particular IP (and localhost)?
回答3:
you may try using cmd to ping to server IP from your client side to see whether it is a connection problem.
Furthermore, i think this
#define PORT "3490"
in client side is wrong since port should be an integer.
来源:https://stackoverflow.com/questions/4263913/connecting-to-an-ip-address-instead-of-localhost