🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

[HELP] Client not answering back

Started by
12 comments, last by d3vil401 9 years, 1 month ago

So i finally made a server but the client is...not answering back.

This is how it works: Clients connect to server, server sends a welcome packet and client answer back with user's credentials.

I do the exact same things but the client is not answering back after receiving the server's packet.

I'm using Boost::Asio and the error is "End of File" which means the client is shutting down everything.

The problem is i don't understand why, i use netsh to redirect client's communications to localhost for testing purpose and Wireshark can't even trace packets so technically the connections does not exist but the server and client can communicate until that error.

Anyone has an idea?

Thanks.

Advertisement
It sounds like you have a bug in your code.
Is the End of File error on the client or on the server?
And why do you use netsh? You could just run client and server on the local machine, and connect to 127.0.0.1:serverport
enum Bool { True, False, FileNotFound };

Hi.

sounds like your worker thread stops you need to post worker thread and other related code.

Have you been through the tutorials Boost::asio tuts

It sounds like you have a bug in your code.
Is the End of File error on the client or on the server?
And why do you use netsh? You could just run client and server on the local machine, and connect to 127.0.0.1:serverport

It's the server.

Actually i can't just let the client connects to localhost.

Hi.

sounds like your worker thread stops you need to post worker thread and other related code.

Have you been through the tutorials Boost::asio tuts


void SockHandler::Start() 
{
printf("Client (%s) successfully connected.\n", REMOTE_IP);
 
this->StartHeaderReading();
 
}
 
void SockHandler::StartHeaderReading()
{
this->m_recvBuffer.reset(new unsigned char[2]);
 
boost::asio::async_read(this->tcpSocket, boost::asio::buffer(this->m_recvBuffer.get(), 2), boost::asio::transfer_at_least(1), boost::bind(&SockHandler::StartBodyReading, shared_from_this(), boost::asio::placeholders::error));
}
 
void SockHandler::StartBodyReading(const boost::system::error_code& error) 
{
if (!error) 
{
unsigned short packetLen = ((m_recvBuffer[1] << 8) | m_recvBuffer[0]) - 2;
printf("Packet Size: %d\n", packetLen);
this->m_recvBuffer.reset(new unsigned char[packetLen]);
boost::asio::async_read(this->tcpSocket, boost::asio::buffer(this->m_recvBuffer.get(), packetLen), boost::bind(&SockHandler::ReadHandler, shared_from_this(), packetLen, boost::asio::placeholders::error));
} else {
printf("[!] StartBodyReading Error: %s.\n", error.message().c_str());
}
}
You should set transfer_at_least to 2, not 1. As it is, you may get half of the length of the data.

Is topSocket actually the new socket you created and passed into accept()?

How are you calling io.run()?
enum Bool { True, False, FileNotFound };

You should set transfer_at_least to 2, not 1. As it is, you may get half of the length of the data.

Is topSocket actually the new socket you created and passed into accept()?

How are you calling io.run()?

Yes and Yes.

void Server::Start()
{
Session tSession(new SockHandler(IOService));
this->Acceptor.async_accept(tSession->getSocket(), boost::bind(&Server::AcceptanceHandler, this, tSession, boost::asio::placeholders::error));
}
 
void Server::AcceptanceHandler(Session pSession, const boost::system::error_code& error) 
{
if (!error) 
{
pSession->Init();
pSession->Start();
this->Start();
} else {
printf("[!] AcceptanceHandler Error: %s.\n", error.message().c_str());
}
}
 
bool Server::Initialize(const char* IP, short Port, ConfigManager* CMHandler) 
{
this->CManager = CMHandler;
this->Start();
this->IOService.run();
return true;
}
void Server::Start()
{
Session tSession(new SockHandler(IOService));
this->Acceptor.async_accept(tSession->getSocket(), boost::bind(&Server::AcceptanceHandler, this, tSession, boost::asio::placeholders::error));
}[/quote]

The Session dies as soon as Start() returns.
enum Bool { True, False, FileNotFound };

void Server::Start(){Session tSession(new SockHandler(IOService));this->Acceptor.async_accept(tSession->getSocket(), boost::bind(&Server::AcceptanceHandler, this, tSession, boost::asio::placeholders::error));
}[/quote]

The Session dies as soon as Start() returns.

What would you do to fix this?

It's your code, and your Session class.
Perhaps create it with "new" and pass around a pointer instead of a copy?
enum Bool { True, False, FileNotFound };

Hi worker thread should look a bit like this.

.


		
 void WorkerThread( boost::shared_ptr< boost::asio::io_service > &io_service )
{
//	std::stringstream ss;
//	ss << "[" << boost::this_thread::get_id() << "] Thread Start";

//	global_stream_lock.lock();
	//send the data to the client list box
	//SendMessage(GlobalList, (UINT)LB_ADDSTRING , 0, (LPARAM) ss.str().c_str()); 

	//std::cout << "[" << boost::this_thread::get_id()
	//	<< "] Thread Start" << std::endl;

	//global_stream_lock.unlock();

	

	while( true )
	{
		//may not need this the io_service will return and the thread will stop
		if(ForceStop)
			break;

		try
		{
			boost::system::error_code ec;
			io_service->run( ec );//it will wait in here calling io_service->stop(); will make this return
			if( ec )
			{
				//ss.str("");
				//ss.clear();
				//ss << "[" << boost::this_thread::get_id() << "] Error: " << ec.message();

				//global_stream_lock.lock();
				//send the data to the client list box
				//SendMessage(GlobalList, (UINT)LB_ADDSTRING , 0, (LPARAM) ss.str().c_str()); 

			//	std::cout << "[" << boost::this_thread::get_id()
			//		<< "] Error: " << ec << std::endl;
			//	global_stream_lock.unlock();

				//Sleep(10000);
			}
			//MessageBox(NULL,"io_service Run Returned","Thread will End Now",MB_OK);
			break;
		}
		catch( std::exception & ex )
		{
		//	ss.str("");
		//	ss.clear();
		//	ss  << "[" << boost::this_thread::get_id() << "] Exception: " << ex.what();

		//	global_stream_lock.lock();
			
			//send the data to the client list box
		//	SendMessage(GlobalList, (UINT)LB_ADDSTRING , 0, (LPARAM) ss.str().c_str()); 

MessageBox(NULL,	ex.what(),"Thread Exception Catched",MB_OK);

			//std::cout << "[" << boost::this_thread::get_id()
			//	<< "] Exception: " << ex.what() << std::endl;
		//	global_stream_lock.unlock();
		}
	}


	//MessageBox(NULL,"Thread End","Thread End",MB_OK);
}//end WorkerThread
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

with out the worker thread io_services will run until the connect call and then there will be no work to do so it quits.

try boost::shared_ptr<cSession> Session;

Refresher.

This topic is closed to new replies.

Advertisement