예제 #1
0
void test()
{
	SocketAddress addr;
	addr.SetAddress("localhost:5000");
	GenericSocket genSock(SOCK_DGRAM);
	genSock.Bind(addr);
}
예제 #2
0
int UDPSocket::Recibir(void * data, int lenData, SocketAddress & from)
{
	sockaddr address;
	int longr = sizeof(address);
	recvfrom(GenericSocket::GetSock(), (char*)data, lenData, 0, &address, &longr);
	from.SetAddress(address);
	return 0;
}
예제 #3
0
//Al principio el estado de la red es UNINITIALIZED
NetworkClient::NetworkClient(std::string _addressServer, std::string _addressClient, std::string _nick):nick(_nick), networkState(NetworkState::UNINITIALIZED)
{
	saServer.SetAddress(_addressServer);
	SocketAddress myAddress;
	myAddress.SetAddress(_addressClient);
	int errBind = udpSocket.Bind(myAddress);
	int errBlock = udpSocket.NonBlocking(true);
	if (errBind > -1 && errBlock > -1)
	{
		//Si podemos hacer BIND y NONBLOCKING, pasamos a estado SAYINGHELLO
		//Este cliente empezará a buscar al servidor.
		networkState = NetworkState::SAYINGHELLO;
	}
}
예제 #4
0
Server::Server(std::string _serverAddress, int _numPlayers)
{
	aWords.LoadDefault();
	//Inicializar el socket de dispatcher

	//Ponerlo a Bind y a Listen

	SocketAddress sa;
	sa.SetAddress(_serverAddress);
	int err = dispatcher.Bind(sa);
	if (err == -1)
	{
		throw std::exception("Error en Server::Server");
	}
	err = dispatcher.Listen(_numPlayers);
	if (err == -1)
	{
		throw std::exception("Error en listen");
	}
}
예제 #5
0
void Cliente(std::string direccionDestino)
{
	UDPSocket udpSocket;
	SocketAddress saDestino;
	saDestino.SetAddress(direccionDestino);
	udpSocket.NonBlocking(true);
	while (true)
	{
		char datos[1300];
		int len = StringTools::PideDatosTeclado(datos);

		int bytesSend = udpSocket.SendTo(datos, len, saDestino);
		if (bytesSend > 0)
		{
			if (strcmp(datos, "exit") == 0)
			{
				break;
			}
		}
	}
}
예제 #6
0
void routine(std::string direccion,int conexiones) {

	//Creamos el socket de dispatching para saber cuando se nos conectan
	TCPSpcket dispatchSock;
	SocketAddress addressReceive;
	addressReceive.SetAddress(direccion);
	dispatchSock.Bind(addressReceive);
	std::vector<std::shared_ptr<TCPSpcket>> clientList;
	dispatchSock.Listen(conexiones);

	SocketAddress comesFrom;

	for (int i = 0; i < conexiones; i++)
	{
		clientList.push_back(dispatchSock.Accept(comesFrom));

	}
	
	sendToAll(clientList,"BEGIN",conexiones);
	

}
예제 #7
0
NetworkServer::NetworkServer(std::string _strServerAddress)
{

	SocketAddress saServer;
	saServer.SetAddress(_strServerAddress);
	//TODO: No se están controlando errores de Bind y NonBlocking
	udpSocket.Bind(saServer);
	udpSocket.NonBlocking(true);

	//Forzamos que los jugadores estén desconectados
	//También nos aseguramos de que las posiciones iniciales de los jugadores
	//en el servidor corresponden con las de los clientes.
	//Esto es muy importante para asegurarnos de que el movimiento relativo
	//y el dispatch "absolutista" del servidor tienen los valores correctos.
	for (int i = 0; i < 4; i++)
	{
		playerList[i].connected = false;
		playerList[i].position = 10;
		playerList[i].antiCheatCount = 0;
	}

	

}
예제 #8
0
void Servidor(std::string direccion)
{
	SocketAddress sa;
	sa.SetAddress(direccion);
	UDPSocket udpSocket;
	udpSocket.Bind(sa);
	udpSocket.NonBlocking(true);
	while (true)
	{
		char datos[1300];
		SocketAddress from;
		
		int bytesReceived = udpSocket.ReceiveFrom(datos, 1300, from);
		if (bytesReceived > 0)
		{
			datos[bytesReceived] = '\0';
			std::cout << "Recibo de "<<from<<": " << datos << std::endl;
			if (strcmp(datos, "exit") == 0)
			{
				break;
			}
		}
	}
}