Advertisement

Trying to test a sever using my public IP

Started by February 04, 2015 01:16 AM
12 comments, last by Brain 9 years, 9 months ago

Hi all.

I'm stuck I can't seem to connect the server to my public IP, I get the IP from whats my IP and type it into my server app just for testing

I know the IP can be reset when ever but for testing I thought it may work.

When I put the IP address in the server it returns a message Bind: The requested address is not valid in its context.

What does that mean.??????

Does anyone know how I can test my server and client.

if I use the 127.0.0.2 the sever connects and the client connects.

Does the port matter for out going sever im using 7777 for port on server.

or is this a router issue not sure.

It's a problem in your code. Post your code.
Advertisement

or is this a router issue not sure.

It could be.
Your public IP is the IP address of your router on the internet.
Have you configured your router to forward any inbound traffic on that port to your server PC?

Hi I think I found it I was setting the sever to the IP of my public address. But if I use 127.0.0.2 for server and the puplic IP in the client it works.

Is that what you are meant to do.

the code I'm using is this here. I have read all the boost docs and this doc made all the difference thanks.

So what address should the server use.

or is this a router issue not sure.

It could be.
Your public IP is the IP address of your router on the internet.
Have you configured your router to forward any inbound traffic on that port to your server PC?


Isn't bind for reserving a local endpoint for your socket? You have to pass it your local endpoint info, not your router's WAN-facing endpoint.

Hi again.

Im using 1 pc at the moment for server and client, I have a laptop Im going to set the server on that later when I can get my head around sending messages.

At the moment its just doing a connection, the client saying its connected.

Advertisement

But if I use 127.0.0.2 for server and the puplic IP in the client it works.


127.*.*.* is the loopback block of IP addresses. 127.0.0.1 is what "localhost" typically resolves to. That will only work when a client connects to another program on the same computer.


Your server should always bind its listening socket to the "any" endpoint regardless of how you're testing. Your client should connect to whatever IP represents the host. On the same computer you can use 127.0.0.1. On different computers, you have to provide a "real" IP address of the server that the client can reach.

Hey if I run the client and not the server the client reports that it connected.

Does that mean the port I used for the sever was not closed.

No just put some break points in the servers OnAccept method and no client connects so who is the client connecting to lol, the client saying its connected ????

or the clien is bung

the clien is just this

.


//CLIENT

#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>

boost::mutex global_stream_lock;

void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service )
{
	global_stream_lock.lock();
	std::cout << "[" << boost::this_thread::get_id()
		<< "] Thread Start" << std::endl;
	global_stream_lock.unlock();

	while( true )
	{
		try
		{
			boost::system::error_code ec;
			io_service->run( ec );
			if( ec )
			{
				global_stream_lock.lock();
				std::cout << "[" << boost::this_thread::get_id()
					<< "] Error: " << ec << std::endl;
				global_stream_lock.unlock();
			}
			break;
		}
		catch( std::exception & ex )
		{
			global_stream_lock.lock();
			std::cout << "[" << boost::this_thread::get_id()
				<< "] Exception: " << ex.what() << std::endl;
			global_stream_lock.unlock();
		}
	}

	global_stream_lock.lock();
	std::cout << "[" << boost::this_thread::get_id()
		<< "] Thread Finish" << std::endl;
	global_stream_lock.unlock();
}

void OnConnect( const boost::system::error_code & ec, boost::shared_ptr< boost::asio::ip::tcp::socket > sock )
{
	if( ec )
	{
		global_stream_lock.lock();
		std::cout << "[" << boost::this_thread::get_id()
			<< "] Error: " << ec.message() << std::endl;
		global_stream_lock.unlock();
	}
	else
	{
		global_stream_lock.lock();
		std::cout << "[" << boost::this_thread::get_id()
			<< "] Connected!" << std::endl;
		global_stream_lock.unlock();
	}
}

int main( int argc, char * argv[] )
{
	boost::shared_ptr< boost::asio::io_service > io_service(
		new boost::asio::io_service
	);
	boost::shared_ptr< boost::asio::io_service::work > work(
		new boost::asio::io_service::work( *io_service )
	);
	boost::shared_ptr< boost::asio::io_service::strand > strand(
		new boost::asio::io_service::strand( *io_service )
	);

	global_stream_lock.lock();
	std::cout << "[" << boost::this_thread::get_id()
		<< "] Press [return] to exit." << std::endl;
	global_stream_lock.unlock();

	boost::thread_group worker_threads;
	for( int x = 0; x < 2; ++x )
	{
		worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );
	}

	boost::shared_ptr< boost::asio::ip::tcp::socket > sock(
		new boost::asio::ip::tcp::socket( *io_service )
	);

	try
	{
		boost::asio::ip::tcp::resolver resolver( *io_service );
		boost::asio::ip::tcp::resolver::query query( 
			"144.133.212.108",//"www.google.com", 
			boost::lexical_cast< std::string >( 800 )
		);
		boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve( query );
		boost::asio::ip::tcp::endpoint endpoint = *iterator;
		
		global_stream_lock.lock();
		std::cout << "Connecting to: " << endpoint << std::endl;
		global_stream_lock.unlock();

		sock->async_connect( endpoint, boost::bind( OnConnect, _1, sock ) );
	}
	catch( std::exception & ex )
	{
		global_stream_lock.lock();
		std::cout << "[" << boost::this_thread::get_id()
			<< "] Exception: " << ex.what() << std::endl;
		global_stream_lock.unlock();
	}

	std::cin.get();

	boost::system::error_code ec;
	sock->shutdown( boost::asio::ip::tcp::socket::shutdown_both, ec );
	sock->close( ec );

	io_service->stop();

	worker_threads.join_all();

	return 0;
}

ping your public IP, if it's ok, then maybe something wrong in your code. else my your public IP cannot access, check the port and firewall

I notice your codes use boost::asio, then you can compile the asio echo example to test

NFrame - agile game server developing framework. Github:https://github.com/ketoo/NoahGameFrame

Hi. thanks for all the help.

I'm going to set the lap top with a static IP and then port foward to that IP for the server app.

I was kind of hoping that I could getaway with not doing any thing to the network, like port forward.

I guess whats going on is its connecting to the router and then it does not know which computer to send the data to.

I say this because when using the loop back IP 127.0.0.1 the clien can connect. It only fails when I pass in bogus IP address, that errors are being reported back.

Im really not sure why the client reports connected.

This topic is closed to new replies.

Advertisement