Advertisement

Retrieving the list of clients from the c ++ socket

Started by May 07, 2018 05:38 PM
13 comments, last by hplus0603 6 years, 6 months ago

I have a server, after accept it is being created thread for a client, and i have problem here, when second client connects are happening strange things, packets which are send via server they are distorted, same with receiving, earlier i getting clear packets, and after connecting second client receiving packets they began to have strange characters.

Code:


..sock etc.

//bind
if(bind(server, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
	perror("Fail bind");
	exit(1);
}

std::cout << "=> Looking for clients..." << std::endl << std::endl;

std::vector <int> connection;
int clients = 0;

while( rc >= 0 )
{
	rc = listen( server, 50);
	if(  rc < 0)
	{
		perror("listen");
		close( server );
	}
	else
	{
		size = sizeof(client_addr);
		client = accept( server, ( struct sockaddr * ) & client_addr, & size );

		connection.push_back( client );
		std::thread th(show, connection, clients);

		threads.push_back(std::move(th));
		clients++;
	}
}

show(); - is a function that supports the game client, receives and sends packets all the time, works until the client disconnects.

Any idea how i can fix it?

You should only listen() once on a socket.

Separately, it's impossible for us to know what "works until the client disconnects" means. What happens when the client disconnects?

enum Bool { True, False, FileNotFound };
Advertisement

When the player disconnects, his thread is closed via join();

I discovered that first and second client each other change their variables which, they use the same variables, How i can fix it?

Quote

I discovered that first and second client each other change their variables which, they use the same variables, How i can fix it?

It sounds like you are trying to learn too many new things at once. You're trying to learn threaded programming, you're trying to learn network programming, and you're trying to learn game networking all at once. This won't work. You need to pick one of the three problems (say, threaded programming) and learn that first, until you really know it. Then, pick the second problem (say, network programming,) and learn that, until you really know it. Then you can learn the specifics of game networking without continually tripping over the challenges of the other requirements.

enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement