All pastes #375681 Raw Edit

tiav

public text v1 · immutable
#375681 ·published 2007-02-28 14:54 UTC
rendered paste body
void	Server::Do_Server()
{
	SOCKET Socket;
	int		i;
	int		res;

	this->tv.tv_sec = 0;
	this->tv.tv_usec = 1000 * 1000;
	std::cout << "Waiting for client to connect..." << std::endl;
	while (1) 
	{
		FD_ZERO(&(this->readfds));
		FD_ZERO(&(this->writefds));
		FD_SET(this->srv_socket, &this->readfds);
		this->SetClientFd();
		res = select(this->max_fd + 1, &this->readfds, &this->writefds, NULL, &this->tv);
		if (res == -1)
		{
			std::cout << "ERREURRRRR " << std::endl;
			return ; // a voir
		}
		else if (res != 0)
		{
			if (FD_ISSET(this->srv_socket, &this->readfds))
			{
				Socket = accept(this->srv_socket, NULL, NULL);
				this->ClientMap[Socket] = new Client(Socket);
				this->Check_Max_Fd();
			}
			for (i = 0; i < this->max_fd; i++)
			{
				if (FD_ISSET(i, &this->readfds) && i != this->srv_socket)
				{
					if (!this->ClientMap[i]->read())
					{
						delete this->ClientMap[i];
						this->ClientMap.erase(i);
						this->Check_Max_Fd();
					}
				}
				if (FD_ISSET(i, &this->writefds) && i != this->srv_socket)
				{
					if (!this->ClientMap[i]->write())
					{
						delete this->ClientMap[i];
						this->ClientMap.erase(i);
						this->Check_Max_Fd();
					}
				}
			}
		}
	}
}