void new_connection(TCPsocket sock) { TCPsocket tmp; struct socket_node *socket; IPaddress *ip; /* accept the connection temporarily */ tmp = SDLNet_TCP_Accept(sock); if (!tmp) do_error(SDLNet_GetError()); /* are we full or game already started? */ if (get_active_players() + 1 > NUM_PLAYERS || current_turn) SDLNet_TCP_Close(tmp); else { /* nope! */ socket = new_socket(SOCKET_CLIENT); socket->sock = tmp; if (SDLNet_TCP_AddSocket(sockset, tmp) < 0) do_error(SDLNet_GetError()); ip = SDLNet_TCP_GetPeerAddress(tmp); if (!ip) do_error(SDLNet_GetError()); if (SDLNet_ResolveIP(ip)) strcpy(socket->host, SDLNet_ResolveIP(ip)); else sprintf(socket->host, "Unknown IP"); join_player(socket); /* add player to game */ } }
ClientConnection::ClientConnection(TCPsocket sock, ClientManager *manager) : socket(sock), status(0), manager(manager) { printf("ClientConnection()\n"); bindings[SDLK_LEFT] = bindings[SDLK_a] = Binding(&Entity::left, 200); bindings[SDLK_RIGHT] = bindings[SDLK_d] = Binding(&Entity::right, 200); bindings[SDLK_UP] = bindings[SDLK_w] = Binding(&Entity::up, 200); bindings[SDLK_DOWN] = bindings[SDLK_s] = Binding(&Entity::down, 200); bindings[SDLK_SPACE] = bindings[SDLK_e] = Binding(&Entity::prepare_action, &Entity::action); bindings[SDLK_LSHIFT]= bindings[SDLK_f] = Binding(&Entity::prepare_grab, &Entity::grab); bindings[SDLK_LCTRL] = bindings[SDLK_g] = Binding(&Entity::prepare_drop, &Entity::drop); bindings[SDLK_LALT] = bindings[SDLK_q] = Binding(&Entity::prepare_swap, &Entity::swap); name = SDLNet_ResolveIP(SDLNet_TCP_GetPeerAddress(socket)); update_entity_connection(); old_x = get_x(); old_y = get_y(); send_mutex = SDL_CreateMutex(); messages_mutex = SDL_CreateMutex(); send_thread = SDL_CreateThread(send_thread_func, this); recieve_thread = SDL_CreateThread(recieve_thread_func, this); char data[] = { 'b', 151, 130, 42 }; // set background int len = sizeof(data); send(data, len); }
static const char *NET_AddrToStr(IPaddress* sk) { static char s[22]; // 255.255.255.255:65535 strcpy(s, SDLNet_ResolveIP(sk)); if (sk->port != 0) strcat(s, va(":%d", sk->port)); return s; }
//------------------------------------------------------------------------------------- void GalactiCombatServer::startServer(long portNo) { if(verbose) std::cout << "Entering startServer" << std::endl; IPaddress ip; //32-bit IPv4 host, 16-bit port const char *host = NULL; Uint16 port = (Uint16)portNo; isServer = true; if(NetworkUtil::ResolveHost(&ip, NULL, port)==-1) exit(3); TCPServerSock = NetworkUtil::TCPOpen(&ip); if(!TCPServerSock) exit(4); /* UDPServerSock = NetworkUtil::UDPOpen(0); if(!UDPServerSock) exit(4); */ //set up the game environment if(verbose) std::cout << "Setting up game...." << std::endl; mRoot = new Ogre::Root(); chooseSceneManager(); this->createServerMinerals(); this->createServerRoom(); host = SDLNet_ResolveIP(&ip); if(host == NULL) host = "N/A"; if(verbose) std::cout << "Host: " << host << std::endl; if(verbose) std::cout << "Exiting startServer" << std::endl << std::endl; std::cout<<"Ready!"<<std::endl; serverLoop(); }
void accept_connections() { if (SDLNet_SocketReady(serversock)) { TCPsocket client = SDLNet_TCP_Accept(serversock); if (client) { clients.push_back(new ClientConnection(clients.size()+1, client)); std::stringstream out; out << "version 0" << std::endl; out << "your_id " << clients.size() << std::endl; std::string str = out.str(); SDLNet_TCP_Send(client, const_cast<char*>(str.c_str()), str.length()+1); IPaddress* ip = SDLNet_TCP_GetPeerAddress(client); const char* host; if((host = SDLNet_ResolveIP(ip)) != 0) std::cout << "# Got client connection from " << host << " " << ip->port << std::endl; else std::cout << "# Got client connection from " << ip->host << " " << ip->port << std::endl; int numused = SDLNet_TCP_AddSocket(socketset, client); if (numused == -1) { printf("SDLNet_AddSocket: %s\n", SDLNet_GetError()); // perhaps you need to restart the set and make it bigger... } else { std::cout << "# Sockets used: " << numused << std::endl; } } } }
int main(int argc,char* argv[]) { if (SDL_Init(SDL_INIT_EVERYTHING)==-1) { return(0); } atexit(SDL_Quit); if (SDLNet_Init()) { return(0); } atexit(SDLNet_Quit); IPaddress ip; SDLNet_ResolveHost(&ip,NULL,16); fprintf(stdout,"Local Host: %s\n",SDLNet_ResolveIP(&ip)); SDLNet_ResolveHost(&ip,"www.gamedev.net",16); fprintf(stdout,"Remote Host: %s\n",SDLNet_ResolveIP(&ip)); return(0); }
//this function assumes pin already has an incoming packet from the client bool NET::ServerHandshake() { string handshake = HANDSHAKESTR; string handshake_reply = HANDSHAKEREPLYSTR; // int timeout = HANDSHAKETIMEOUT; bool err = false; if (IsEqualToPacket(pin, CONTROL_HANDSHAKE, (void *) handshake.c_str(), handshake.length())) { SDLNet_UDP_Unbind(remote_socket, 0); if (SDLNet_UDP_Bind(remote_socket, 0, &(pin->address))==-1) { err = true; if (NET_DEBUG) { cout << "net: couldn't bind UDP socket to host " << SDLNet_ResolveIP(&(pin->address)) << endl; printf("SDLNet_UDP_Bind: %s\n",SDLNet_GetError()); } } WriteToPacket(pout, CONTROL_HANDSHAKE, (void *) handshake_reply.c_str(), handshake_reply.length()); UDPSend(pout, 0); connected = true; if (NET_DEBUG) cout << "net: incoming connection established" << endl; } else { if (NET_DEBUG) cout << "net: Invalid handshake from " << SDLNet_ResolveIP(&(pin->address)) << endl; err = true; } return err; }
int main (int argc, char *argv[]) { int i; [+IF (=(get "HaveSDL_net") "1")+] IPaddress local = {0x0100007F, 0x50}; [+ENDIF+] printf ("Initializing SDL.\n"); /* Initializes Audio and the CDROM, add SDL_INIT_VIDEO for Video */ if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_CDROM)< 0) { printf("Could not initialize SDL:%s\n", SDL_GetError()); SDL_Quit(); return 1; } printf("Audio & CDROM initialized correctly\n"); /* Trying to read number of CD devices on system */ printf("Drives available: %d\n", SDL_CDNumDrives()); for (i=0; i < SDL_CDNumDrives(); ++i) { printf("Drive %d\"%s\"\n", i, SDL_CDName(i)); } [+IF (=(get "HaveSDL_net") "1")+] if (SDLNet_Init() < 0) { printf("Could not initialize SDL_net:%s\n", SDLNet_GetError()); SDL_Quit(); return 1; } printf("\n\nNetwork initialized correctly\n"); /* Get host name */ printf("Hostname: %s\n", SDLNet_ResolveIP(&local)); SDLNet_Quit(); [+ENDIF+] SDL_Quit(); return(0); }
//Servername examples: localhost, http://someaddress.com, 192.168.0.1 NetworkClient::NetworkClient(std::string serveraddress, int portnumber) { this->shutdown = true; //Always shutting down, except if we get "OK" from server if(SDLNet_Init() < 0) { cgl::Cout("Failed to init SDL"); } else { this->serveraddress = serveraddress; this->portnumber = portnumber; int hostresolved = SDLNet_ResolveHost(&this->serverIP, this->serveraddress.c_str(), this->portnumber); if(hostresolved == -1) { cgl::Cout("Could not resolve the servername"); } else { Uint8* dotQuad = (Uint8*)&this->serverIP.host; std::cout << "Success resolving host to IP: " << (unsigned short)dotQuad[0] << "." << (unsigned short)dotQuad[1] << "." << (unsigned short)dotQuad[2] << "." << (unsigned short)dotQuad[3] << std::endl; } if((host = SDLNet_ResolveIP(&this->serverIP)) == NULL) { cgl::Cout("Failed to resolve IP"); } else { cgl::Cout("Successfully resolved IP", host); } this->messageOnSERVERISFULL = "FULL"; this->messageOnSERVEROK = "OK"; this->messageOnShutdown = "exit"; this->clientsocket = NULL; } }
void C_Server::InitServer() { //init m_connected = false; m_serverSocket = NULL; m_clientSocket = NULL; m_bytesSent = 0; if(SDLNet_Init() == -1) printf("SDLNet_Init: %s\n\n", SDLNet_GetError()); //Setup server IP and port if(SDLNet_ResolveHost(&m_ipaddress, NULL, SERVER_PORT)== -1) printf("SDLNet_Init: %s\n", SDLNet_GetError()); //Output HostIp if(!(m_hostIP = SDLNet_ResolveIP(&m_ipaddress))) printf("SDLNet_ResolveIP: %s\n", SDLNet_GetError()); else printf("Host is: %s \n\n", m_hostIP); //Get a TCPsocket m_serverSocket = SDLNet_TCP_Open(&m_ipaddress); if(!m_serverSocket) printf("SDLNet_TCP_Open: %s\n\n", SDLNet_GetError()); else printf("Server up and running. Awaiting client connection!....\n\n"); //Create socket set AND add socket to monitor(handle up to 16 sockets) m_socketSet = SDLNet_AllocSocketSet(1); if(!m_socketSet) printf("SDLNet_AllocSocketSet: %s\n\n", SDLNet_GetError()); //**update netInfo struct s_netInfo.playerName[0] = m_hostIP; int i = 0; }
void AppStateLobby::Initialize() { player.player_name = username; std::cout << "I am " << player.player_name << '\n'; if (directedGameHost == 0) network = NetworkFactory::getInstance("./conf/networkLobby.conf"); else { IPaddress hostaddress; hostaddress.host = directedGameHost; hostaddress.port = directedGamePort; const char *hostname = SDLNet_ResolveIP(&hostaddress); std::cout << "Redirected to lobby: " << hostname << '\n'; if (hostname == NULL) { std::cerr << "Could not resolve directed host\n"; AppStateEvent::New_Event(APPSTATE_MENU); return; } else network = NetworkFactory::getInstance(TCP, hostname, directedGamePort); } if (network->Init() == -1) { // LobbyServer is not listening... Try the actual game server on udp AppStateEvent::New_Event(APPSTATE_GAME); return; } lobbyIsReady = true; NetString string; string.WriteUChar(NCE_PLAYER); string.WriteString(player.player_name); string.WriteUChar(NCE_END); network->SendData(&string, 0); }
const char* Address::GetName() const { return SDLNet_ResolveIP(&mAddress); }
void do_command(char *msg, Client *client) { char *command,*p; int len; if(!msg || !strlen(msg) || !client) return; len=strlen(msg); p=msg; command=strsep(&p," \t"); /* /NICK : change the clients name */ if(!strcasecmp(command,"NICK")) { if(p && strlen(p)) { char *old_name=client->name; fix_nick(p); if(!strlen(p)) { putMsg(client->sock,"--- Invalid Nickname!"); return; } if(!unique_nick(p)) { putMsg(client->sock,"--- Duplicate Nickname!"); return; } client->name=strdup(p); send_all(mformat("ssss","--- ",old_name," --> ",p)); free(old_name); } else putMsg(client->sock,"--- /NICK nickname"); return; } /* MSG : client to client message */ if(!strcasecmp(command,"MSG")) { char *name; int to; if(p) { name=strsep(&p," "); to=find_client_name(name); if(to<0) { putMsg(client->sock, mformat("sss","--- /MSG nickname ",name," not found!")); return; } else if(p && strlen(p)) { putMsg(client->sock,mformat("ssss",">",clients[to].name,"< ",p)); putMsg(clients[to].sock,mformat("ssss",">",client->name,"< ",p)); return; } } putMsg(client->sock,"--- /MSG nickname message..."); return; } /* /ME : emote! to everyone */ if(!strcasecmp(command,"ME")) { if(p && strlen(p)) { send_all(mformat("sss",client->name," ",p)); } else putMsg(client->sock,"--- /ME message..."); return; } /* /QUIT : quit the server with a message */ if(!strcasecmp(command,"QUIT")) { if(!p || strcasecmp(p,"-h")) { if(p) send_all(mformat("ssss","--- ",client->name," quits : ",p)); else send_all(mformat("sss","--- ",client->name," quits")); remove_client(find_client(client->sock)); } else putMsg(client->sock,"--- /QUIT [message...]"); return; } /* /WHO : list the users online back to the client */ if(!strcasecmp(command,"WHO")) { int i; IPaddress *ipaddr; Uint32 ip; const char *host=NULL; putMsg(client->sock,"--- Begin /WHO "); for(i=0;i<num_clients;i++) { ipaddr=SDLNet_TCP_GetPeerAddress(clients[i].sock); if(ipaddr) { ip=SDL_SwapBE32(ipaddr->host); host=SDLNet_ResolveIP(ipaddr); putMsg(client->sock,mformat("sssssdsdsdsdsd","--- ",clients[i].name, " ",host?host:"", "[",ip>>24,".", (ip>>16)&0xff,".", (ip>>8)&0xff,".", ip&0xff, "] port ",(Uint32)ipaddr->port)); } } putMsg(client->sock,"--- End /WHO"); return; }
//Servername examples: localhost, http://someaddress.com, 192.168.0.1 NetworkClient::NetworkClient(std::string serveraddress, int portnumber) { this->shutdown = true; //Always shutting down, except if we get "OK" from server if(SDLNet_Init() < 0) { cgl::Error("Failed to init SDL"); } this->serveraddress = serveraddress; this->portnumber = portnumber; this->sockets = SDLNet_AllocSocketSet(1); if(this->sockets == NULL) { cgl::Error("Failed to allocate sockets"); } int hostresolved = SDLNet_ResolveHost(&this->serverIP, this->serveraddress.c_str(), this->portnumber); if(hostresolved == -1) { cgl::Cout("Could not resolve the servername"); } else { Uint8* dotQuad = (Uint8*)&this->serverIP.host; std::cout << "Success resolving host to IP: " << (unsigned short)dotQuad[0] << "." << (unsigned short)dotQuad[1] << "." << (unsigned short)dotQuad[2] << "." << (unsigned short)dotQuad[3] << std::endl; } if((host = SDLNet_ResolveIP(&this->serverIP)) == NULL) { cgl::Cout("Failed to resolve IP"); } else { cgl::Cout("Successfully resolved IP", host); } this->clientsocket = SDLNet_TCP_Open(&this->serverIP); if(!this->clientsocket) { //Not reachable, wrong port, server is "full" cgl::Error("Failed to open a connection to the server, server might be down"); } else { cgl::Cout("Connected to server"); SDLNet_TCP_AddSocket(this->sockets, this->clientsocket); int activesockets = SDLNet_CheckSockets(this->sockets, 1000); cgl::Cout("There are currently" + activesockets, " sockets active ATM"); int response = SDLNet_SocketReady(this->clientsocket); if(response != 0) { int serverresponsebytecount = SDLNet_TCP_Recv(this->clientsocket, this->buffer, BUFFERSIZE); cgl::Cout("Message from server:", buffer); if(strcmp(buffer, "OK") == 0) { this->shutdown = false; //Connected with OK message cgl::Cout("Joined the server", this->host); } else { cgl::Cout("Server is full", this->host); } } else { cgl::Cout("No response"); } } }
int main(int argc, char **argv) { IPaddress ip; const char *host; Uint8 *ipaddr; /* check our commandline */ if(argc>1 && !strncmp("-h",argv[1],2)) { printf("%s host|ip\n",argv[0]); exit(0); } /* do a little version check for information */ { SDL_version compile_version; const SDL_version *link_version=SDLNet_Linked_Version(); SDL_NET_VERSION(&compile_version); printf("compiled with SDL_net version: %d.%d.%d\n", compile_version.major, compile_version.minor, compile_version.patch); printf("running with SDL_net version: %d.%d.%d\n", link_version->major, link_version->minor, link_version->patch); } /* initialize SDL */ if(SDL_Init(0)==-1) { printf("SDL_Init: %s\n",SDL_GetError()); exit(1); } /* initialize SDL_net */ if(SDLNet_Init()==-1) { printf("SDLNet_Init: %s\n",SDLNet_GetError()); exit(2); } #ifndef WIN32 /* has no gethostname that we can use here... */ { char localhostname[256]; if((gethostname(localhostname, 256)>=0)) { printf("Local Host: %s\n",localhostname); printf("Resolving %s\n",localhostname); if(SDLNet_ResolveHost(&ip,localhostname,0)==-1) { printf("Could not resolve host \"%s\"\n%s\n", localhostname,SDLNet_GetError()); } else { /* use the IP as a Uint8[4] */ ipaddr=(Uint8*)&ip.host; /* output the IP address nicely */ printf("Local IP Address : %d.%d.%d.%d\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); } } } #endif if(argc<2) exit(0); /* Resolve the argument into an IPaddress type */ printf("Resolving %s\n",argv[1]); if(SDLNet_ResolveHost(&ip,argv[1],0)==-1) { printf("Could not resolve host \"%s\"\n%s\n",argv[1],SDLNet_GetError()); exit(3); } /* use the IP as a Uint8[4] */ ipaddr=(Uint8*)&ip.host; /* output the IP address nicely */ printf("IP Address : %d.%d.%d.%d\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); /* resolve the hostname for the IPaddress */ host=SDLNet_ResolveIP(&ip); /* print out the hostname we got */ if(host) printf("Hostname : %s\n",host); else printf("No Hostname found\n"); /* shutdown SDL_net */ SDLNet_Quit(); /* shutdown SDL */ SDL_Quit(); return(0); }