Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
as96

Improve code? (TCP/C++)

Recommended Posts

Posted · Original PosterOP

I started writing a server for my games with SFML (C++), this is the server class code:

#include "TCP_Server.h"TCP_Server::TCP_Server(unsigned short int port){	listener_.setBlocking(false);	if (listener_.listen(port) != sf::Socket::Done)	{		std::cout << "[TCP_Server][Error] Couldn't bind to port " << port << std::endl;		system("pause");		system("exit");	}	for (int i = 0; i < MAX_CONNECTIONS; i++)	{		clients_[i].setBlocking(false);	}	Update();	//Start server loop}TCP_Server::~TCP_Server(){}void TCP_Server::Update(){	while (true)	{		Accept();		if (connected_ != 0)		{			Receive();			Send();		}	}}void TCP_Server::Accept(){	if (listener_.accept(clients_[connected_]) != sf::Socket::Done)	{		//Can't accept	}	else	{		connected_++;	}}void TCP_Server::Receive(){	for (int i = 0; i <= connected_; i++)	{		packets_[i].clear();		clients_[i].receive(packets_[i]);		// Debug ->		if (packets_[i].getDataSize() != 0)			std::cout << "[TCP_Server][Debug] Received packet from " << clients_[i].getRemoteAddress() << " (" << clients_[i].getRemotePort() << ")" << std::endl;		// <- Debug	}}void TCP_Server::Send(){	for (int i = 0; i <= connected_; i++)	{		for (int x = 0; x <= connected_; x++)		{			if (i != x)				clients_[i].send(packets_[x]);		}	}}

as you can see is not completed yet, I still have to write a room system, and a lot of other stuff, though before I continue I would like to improve it a little bit so I don't have to re-write too much code in the future.

 

What it does is receive the packets from each client, and send those packets to every other client (this system will only be used to sync the players position), each packet contains 2 floats (x,y).

 

The code is working:

IUat6UD.png?1but I really want to know if I can improve something (I think I have a lot to improve, this is my first attempt to make a multi-player game)

Link to post
Share on other sites
Posted · Original PosterOP

Use UDP instead

My game is going to be a simple puzzle, I prefer more reliability, even if there is more delay it shouldn't impact in anyway the gameplay, if it does then I'm going for UDP

Link to post
Share on other sites

My game is going to be a simple puzzle, I prefer more reliability, even if there is more delay it shouldn't impact in anyway the gameplay, if it does then I'm going for UDP

 

Was going to suggest making it asynchronous but it sounds like it will be so simple it doesn't really matter how you implement it.

Link to post
Share on other sites
Posted · Original PosterOP

I just realized that after I reached the limit of connections I'm not actually stopping "Accept()"

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×