Beispiel #1
0
bool NET::Connect(string host, int port)
{
	server = false;
	bool err = false;
	
	if (host == "Server")
		server = true;
	
	if (remote_socket != NULL)
	{
		SDLNet_UDP_Close(remote_socket);
		remote_socket = NULL;
	}
	
	if (server)
		remote_socket = SDLNet_UDP_Open(port);
	else
		remote_socket = SDLNet_UDP_Open(0);
	
	if (!server)
	{
		SDLNet_ResolveHost(&remote_ip, host.c_str(), port);
		
		SDLNet_UDP_Unbind(remote_socket, 0);
		if (SDLNet_UDP_Bind(remote_socket, 0, &remote_ip)==-1)
		{
			err = true;
			
			if (NET_DEBUG)
			{
				cout << "net:  couldn't bind UDP socket to host " << host << " port " << port << endl;
				printf("SDLNet_UDP_Bind: %s\n",SDLNet_GetError());
			}
		}
	}
	
	//try a handshake
	if (pout == NULL)
		pout = SDLNet_AllocPacket(MAX_PACKET_SIZE);
	if (pin == NULL)
		pin = SDLNet_AllocPacket(MAX_PACKET_SIZE);

	if (server)
	{
	}
	else
	{
		err = ClientHandshake();
	}
	
	return !err;
}
Beispiel #2
0
int main(int argc, char **argv) {
	SOCKET s;
	char *remote;

	if(argc > 1)
		remote = argv[1];
	else
		remote = REMOTE_ADDR;
	
	printf("Starting Winsock... ");
	if(StartWinsock() != 0) {
		printf("Failed.\n");
		return EXIT_FAILURE;
	}
	printf("OK.\n");

	printf("Connecting to %s:%d... ", remote, SERVER_PORT);

	if((s = CreateConnectSocket(remote, SERVER_PORT)) == SOCKET_ERROR) {
		printf("Failed.\n");
		return EXIT_FAILURE;
	}
	printf("OK.\n\n");

	if(ClientHandshake(s))
		CommandLoop(s);

	WSACleanup();

	if(conn.dh_remote_key)
		free(conn.dh_remote_key);
	if(conn.dh_shared_key)
		free(conn.dh_shared_key);
	if(conn.nonce)
		free(conn.nonce);

#ifdef _DEBUG
	showmemstats(stdout);
#endif

	return EXIT_SUCCESS;
}