Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Server socket Network
Hello, I have a client and server chat program that works fine on my local network, but I'm wanting to run the server.exe on a computer outside my network (a friends computer) and use the client to connect to it. The problem is I don't know how to connect to it?. I've tried having the server bind to the ISP assigned IP address but the bind() call fails and it never listen()s or accept()s any connections when I specify my client connect to that IP address. I've also tried INADDR_ANY in my server but still no luck.
 
Basically how do I connect to computer running my server outside my network?. What do I have to do to the server and client?.
 
my code is as follows.
 
server (Right now it's just set to my local address
#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
using namespace std;
 
#define PORT 3490
#define IP "192.168.1.65"
#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 "192.168.1.65"
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;
}
 
 

-thanks
Posted 23-Nov-10 21:36pm
silent7110
Comments
Kozlov_Sergey at 24-Nov-10 2:53am
   
Server must be multithreaded: when client connects, server creates thread to communicate with this client.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There is nothing you can do to your code to do this it is all about network routing.
 
The server binds to the local IP address of the computer it is running on, you then need to put port forwarding rules into any modem/routers/firewalls between that computer and the internet (public IP or ISP assigned IP) for the port you are using to route any connection requests to the server computer. How you do this in dependent on the hardware as it differs between manufacturers.
 
The client component connects to the public IP of your friends network and so long as you set-up the port forwarding rules correctly the connection request will be routed to the server.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 24 Nov 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100