void connect(Uint16 port) { IPaddress ip; if(SDLNet_ResolveHost(&ip,NULL,port)==-1) { printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError()); exit(1); } serversock = SDLNet_TCP_Open(&ip); if(!serversock) { printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError()); exit(2); } socketset = SDLNet_AllocSocketSet(32); if (!socketset) { printf("SDLNet_AllocSocketSet: %s\n", SDLNet_GetError()); exit(1); //most of the time this is a major error, but do what you want. } SDLNet_TCP_AddSocket(socketset, serversock); while(true) { int num = 0; if ((num = SDLNet_CheckSockets(socketset, 10000)) == -1) { printf("SDLNet_CheckSockets: %s\n", SDLNet_GetError()); //most of the time this is a system error, where perror might help you. perror("SDLNet_CheckSockets"); } else { accept_connections(); for(int i = 0; i < int(clients.size()); ++i) { if (clients[i]) clients[i]->update(); } for(int i = 0; i < int(clients.size()); ++i) { if (clients[i] && clients[i]->is_invalid()) { std::cout << "# client " << clients[i]->id << " got disconnected" << std::endl; SDLNet_TCP_DelSocket(socketset, clients[i]->tcpsock); SDLNet_TCP_Close(clients[i]->tcpsock); delete clients[i]; clients[i] = 0; } } } } }
void init_net() { if (SDLNet_Init() < 0) do_error("Failed initializing SDL_net (\"%s\")", SDLNet_GetError()); sockset = SDLNet_AllocSocketSet(NUM_PLAYERS); if (!sockset) do_error(SDLNet_GetError()); online = 0; server = 0; }
void I_WaitForPacket(int ms) { SDLNet_SocketSet ss = SDLNet_AllocSocketSet(1); SDLNet_UDP_AddSocket(ss, udp_socket); SDLNet_CheckSockets(ss,ms); SDLNet_FreeSocketSet(ss); // build script doesn't allow this //#if (defined _WIN32 && !defined PRBOOM_SERVER) // I_UpdateConsole(); //#endif }
CSocketSet::CSocketSet(ushort size): m_socketSet(NULL), m_errorFlag(false) { m_socketSet = SDLNet_AllocSocketSet(size); if( !m_socketSet ) { CLog::instance()->log(CLog::msgFlagNetwork, CLog::msgLvlCritical,_("Error allocating socket set.\n")); m_errorFlag = true; }; }
connection accept_connection() { if(!server_socket) { return 0; } // A connection isn't considered 'accepted' until it has sent its initial handshake. // The initial handshake is a 4 byte value, which is 0 for a new connection, // or the handle of the connection if it's trying to recover a lost connection. /** * A list of all the sockets which have connected, * but haven't had their initial handshake received. */ static std::vector<TCPsocket> pending_sockets; static SDLNet_SocketSet pending_socket_set = 0; const TCPsocket sock = SDLNet_TCP_Accept(server_socket); if(sock) { DBG_NW << "received connection. Pending handshake...\n"; if(pending_socket_set == 0) { pending_socket_set = SDLNet_AllocSocketSet(32); } if(pending_socket_set != 0) { int res = SDLNet_TCP_AddSocket(pending_socket_set,sock); if (res != -1) { pending_sockets.push_back(sock); } else { ERR_NW << "Pending socket set is full! Disconnecting " << sock << " connection\n"; ERR_NW << "SDLNet_GetError() is " << SDLNet_GetError() << "\n"; SDLNet_TCP_Close(sock); } } else { ERR_NW << "Error in SDLNet_AllocSocketSet\n"; } } if(pending_socket_set == 0) { return 0; } const int set_res = SDLNet_CheckSockets(pending_socket_set,0); if(set_res <= 0) { return 0; } return accept_connection_pending(pending_sockets, pending_socket_set); }
int game_host::init_net() { if (SDLNet_Init() < 0) { fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError()); exit(EXIT_FAILURE); //maybe go through here and replace them with more easy to notice errors? } if (SDLNet_ResolveHost(&ip, NULL, this->port) < 0) { fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError()); exit(EXIT_FAILURE); } if (!(sd = SDLNet_TCP_Open(&ip))) { fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError()); exit(EXIT_FAILURE); } if (!(this->p1UDPsock = SDLNet_UDP_Open(this->port))) { cout << "UDP socket failed to open" << SDLNet_GetError() << "\n"; exit(EXIT_FAILURE); } if (!(this->UDPpack1 = SDLNet_AllocPacket(512))) { fprintf(stderr, "SDLNet_AllocPacket: %s\n", SDLNet_GetError()); exit(EXIT_FAILURE); } if (!(this->UDPpack2 = SDLNet_AllocPacket(512))) { fprintf(stderr, "SDLNet_AllocPacket: %s\n", SDLNet_GetError()); exit(EXIT_FAILURE); } if (!(this->UDPrpack = SDLNet_AllocPacket(512))) { fprintf(stderr, "SDLNet_AllocPacket: %s\n", SDLNet_GetError()); exit(EXIT_FAILURE); } socketset = SDLNet_AllocSocketSet(4); messageSizeMod = 0; cout << "Init completed\n"; return 0; // temporary }
TCPClientSocket::TCPClientSocket(int platformsocket) { sendbuffer=0; nativetcpstruct = new Bit8u[sizeof(struct _TCPsocketX)]; mysock = (TCPsocket)nativetcpstruct; isopen = false; if(!SDLNetInited) { if(SDLNet_Init()==-1) { LOG_MSG("SDLNet_Init failed: %s\n", SDLNet_GetError()); return; } SDLNetInited = true; } // fill the SDL socket manually ((struct _TCPsocketX*)nativetcpstruct)->ready=0; ((struct _TCPsocketX*)nativetcpstruct)->sflag=0; ((struct _TCPsocketX*)nativetcpstruct)->channel=(SOCKET) platformsocket; sockaddr_in sa; socklen_t sz; sz=sizeof(sa); if(getpeername(platformsocket, (sockaddr *)(&sa), &sz)==0) { ((struct _TCPsocketX*)nativetcpstruct)-> remoteAddress.host=/*ntohl(*/sa.sin_addr.s_addr;//); ((struct _TCPsocketX*)nativetcpstruct)-> remoteAddress.port=/*ntohs(*/sa.sin_port;//); } else { mysock=0; return; } sz=sizeof(sa); if(getsockname(platformsocket, (sockaddr *)(&sa), &sz)==0) { ((struct _TCPsocketX*)nativetcpstruct)-> localAddress.host=/*ntohl(*/sa.sin_addr.s_addr;//); ((struct _TCPsocketX*)nativetcpstruct)-> localAddress.port=/*ntohs(*/sa.sin_port;//); } else { mysock=0; return; } if(mysock!=0) { listensocketset = SDLNet_AllocSocketSet(1); if(!listensocketset) return; SDLNet_TCP_AddSocket(listensocketset, mysock); isopen=true; return; } mysock=0; return; }
void Networking::serverConnect(){ SDLNet_Init(); if(SDLNet_ResolveHost(&serv_ip, NULL, PORT) == -1) { printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError()); exit(0); } server_socket = SDLNet_TCP_Open(&serv_ip); if(!server_socket){ printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError()); exit(0); } server_socketset = SDLNet_AllocSocketSet(3); }
void Server::connect(Universe *_u){ u = _u; SDLNet_Init(); SDLNet_ResolveHost(&ip,server,port); sock = SDLNet_TCP_Open(&ip); if (!sock){ printf("could not connect to %s:%d\n",server,port); return; } set = SDLNet_AllocSocketSet(1); SDLNet_TCP_AddSocket(set,sock); send("name %s\n",myname); last_ping_time = SDL_GetTicks(); }
//string mythVirtualServer::GetLocalAddress(){ // unsigned char iphost[4] = { 0 }; // IPaddress serverip; // SDLNet_GetLocalAddress(&serverip); // SDL_memcpy(iphost, &serverip.host, 4); // char tmpstr[100] = { 0 }; // sprintf(tmpstr, "%d.%d.%d.%d", iphost[0], iphost[1], iphost[2], iphost[3]); // return tmpstr; //} int mythVirtualServer::initalsocket(int port){ IPaddress serverIP; int i; unsigned char iphost[4] = {0}; /* Initialize SDL */ if ( SDL_Init(0) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); exit(1); } /* Initialize the network */ if ( SDLNet_Init() < 0 ) { fprintf(stderr, "Couldn't initialize net: %s\n", SDLNet_GetError()); SDL_Quit(); exit(1); } #pragma omp parallel for /* Initialize the channels */ for ( i=0; i<CHAT_MAXPEOPLE; ++i ) { people[i] = PEOPLE::CreateNew(); people[i]->addtionaldata = NULL; people[i]->active = 0; people[i]->sock = NULL; } /* Allocate the socket set */ socketset = SDLNet_AllocSocketSet(CHAT_MAXPEOPLE+1); if ( socketset == NULL ) { fprintf(stderr, "Couldn't create socket set: %s\n", SDLNet_GetError()); cleanup(2); } /* Create the server socket */ SDLNet_ResolveHost(&serverIP, NULL, port); //iphost[0] = &serverIP.host; SDL_memcpy(iphost, &serverIP.host, 4); printf("Server IP: %d.%d.%d.%d --- %d\n", iphost[0], iphost[1], iphost[2], iphost[3], port); servsock = SDLNet_TCP_Open(&serverIP); if ( servsock == NULL ) { fprintf(stderr, "Couldn't create server socket: %s\n", SDLNet_GetError()); cleanup(2); } SDLNet_TCP_AddSocket(socketset, servsock); return 0; }
TCP_Socket::TCP_Socket(TCPsocket socket) : sock(socket), sockset(0) { if(!sock) throw TCP_Socket_Init_Failure(); sockset = SDLNet_AllocSocketSet(1); if(!sockset || SDLNet_TCP_AddSocket(sockset, sock) == -1) { SDLNet_TCP_Close(sock); throw TCP_Socket_Init_Failure(); } }
SDLNet_SocketSet Client::create_sockset() { static SDLNet_SocketSet set = NULL; if(set) SDLNet_FreeSocketSet(set); set = SDLNet_AllocSocketSet(1); if(!set) { std::cerr << "SDLNet_AllocSocketSet: " << SDLNet_GetError() << std::endl; return 0; } SDLNet_TCP_AddSocket(set, sock); return set; }
void NetworkCommandBuffer::Think() { SDLNet_SocketSet set; set = SDLNet_AllocSocketSet ( 1 ); if ( !set ) { RaiseSocketError ( "SDLNet_AllocSocketSet" ); } SocketSetEncapsulator free_on_quit ( set ); if ( SDLNet_TCP_AddSocket ( set, socket ) < 0 ) { RaiseSocketError ( "SDLNet_TCP_AddSocket" ); } int numready = SDLNet_CheckSockets ( set, 0 ); for ( ; numready == 1; numready = SDLNet_CheckSockets ( set, 0 ) ) { char msg; int nbread = SDLNet_TCP_Recv ( socket, &msg, 1 ); if ( nbread < 0 ) { RaiseSocketError ( "SDLNet_TCP_Recv: " ); } else if ( nbread == 0 ) { std::cout << "SDLNet_TCP_Recv: Unexpected read of size 0\n"; throw ReadSocketError(); } if ( debug_all_message ) { if ( msg < 32 ) std::cout << "Read: 0x" << ( int ) msg << "\n"; else std::cout << "Read: " << msg << "\n"; } if (current_command_size > 0) AddCharToBuffer ( msg ); else AddCharToCommandSize( msg ); } if ( numready < 0 ) { std::cout << "SDLNet_CheckSockets: " << SDLNet_GetError() << "\n"; perror ( "SDLNet_CheckSockets" ); throw ReadSocketError(); } }
bool FTP_Socket::onEnter() { std::cout << "FTP_Socket::onEnter()" << std::endl; IPaddress ip; set = SDLNet_AllocSocketSet(1); if(!set) { printf("SDLNet_AllocSocketSet: %s\n", SDLNet_GetError()); onExit(); return false; } /* Resolve the argument into an IP address type */ std::cout << "Connecting to " << host.c_str() << " on port " << port << std::endl; if(SDLNet_ResolveHost(&ip,host.c_str(),port) == -1) { printf("SDLNet_ResolveHost: %s\n",SDLNet_GetError()); onExit(); return false; } //open the socket controlSocket = SDLNet_TCP_Open(&ip); if(!controlSocket) { printf("SDLNet_TCP_Open: %s\n",SDLNet_GetError()); onExit(); return false; } if(SDLNet_TCP_AddSocket(set,controlSocket) == -1) { printf("SDLNet_TCP_AddSocket: %s\n",SDLNet_GetError()); onExit(); return false; } /* reference Only if(SDLNet_TCP_DelSocket(set,controlSocket) == -1) { printf("SDLNet_DelSocket: %s\n",SDLNet_GetError()); onExit(); return false; } */ return true; }
Peer::Peer() { if(SDL_Init(0)==-1) { // Print("SDL_Init: %s\n", SDL_GetError()); exit(1); } if(SDLNet_Init()==-1) { Print("SDLNet_Init: %s\n", SDLNet_GetError()); exit(2); } watch = SDLNet_AllocSocketSet(16); if (!watch) { Print("failed to create socket set\n"); } }
int recv_client_req(t_client *clt, t_trame *req) { SDLNet_SocketSet sset; sset = SDLNet_AllocSocketSet(1); SDLNet_TCP_AddSocket(sset, clt->sock); while (32) { if (SDLNet_CheckSockets(sset, (Uint32)-1) < 0) return (put_error("recv_client_req failed")); if (SDLNet_SocketReady(clt->sock) && get_msg(clt, req)) return (0); else if (clt->loss > NET_MAXLOSS) return (1); } return (0); }
/* create a socket set that has the server socket and all the client sockets */ SDLNet_SocketSet create_sockset() { static SDLNet_SocketSet set=NULL; int i; if(set) SDLNet_FreeSocketSet(set); set=SDLNet_AllocSocketSet(num_clients+1); if(!set) { printf("SDLNet_AllocSocketSet: %s\n", SDLNet_GetError()); exit(1); /*most of the time this is a major error, but do what you want. */ } SDLNet_TCP_AddSocket(set,server); for(i=0;i<num_clients;i++) SDLNet_TCP_AddSocket(set,clients[i].sock); return(set); }
manager::manager(size_t min_threads, size_t max_threads) : free_(true) { DBG_NW << "NETWORK MANAGER CALLED!\n"; // If the network is already being managed if(socket_set) { free_ = false; return; } if(SDLNet_Init() == -1) { ERR_NW << "could not initialize SDLNet; throwing error..." << std::endl; throw error(SDL_GetError()); } socket_set = SDLNet_AllocSocketSet(512); worker_pool_man = new network_worker_pool::manager(min_threads, max_threads); }
void client_engine::join(char * ipaddr, char * port) { IPaddress ip; if ( SDLNet_ResolveHost(&ip, ipaddr, atoi(port) ) ) { cerr<<"could not resolve host: "<<SDLNet_GetError()<<endl; exit(1); } _host = SDLNet_TCP_Open(&ip); if ( ! _host ) { cerr<<"Could not connect to host..."<<SDLNet_GetError()<<endl; exit(1); } _host_set = SDLNet_AllocSocketSet(1); if ( ! _host_set ) { cerr<<"Could not allocate socket set "<<SDLNet_GetError()<<endl; exit(1); } SDLNet_TCP_AddSocket(_host_set, _host); _to_host = new message_handler(_host, MAX_MESSAGES); assert(_to_host); // get the number of the local player int rec; char line[MAX_LEN]; rec = SDLNet_TCP_Recv(_host, line, MAX_LEN-1); if ( rec > 0 ) { line[rec] = '\0'; istringstream id(line); id>>line; // should say YOUARENUMBER id>>_local_player; cout<<"got "<<line<<_local_player<<endl; }
// Most of this function by ZZZ. // Incoming port number should be in network byte order. OSErr NetDDPOpenSocket(short *ioPortNumber, PacketHandlerProcPtr packetHandler) { //fdprintf("NetDDPOpenSocket\n"); assert(packetHandler); // Allocate packet buffer (this is Christian's part) assert(!sUDPPacketBuffer); sUDPPacketBuffer = SDLNet_AllocPacket(ddpMaxData); if (sUDPPacketBuffer == NULL) return -1; //PORTGUESS // Open socket (SDLNet_Open seems to like port in host byte order) // NOTE: only SDLNet_UDP_Open wants port in host byte order. All other uses of port in SDL_net // are in network byte order. sSocket = SDLNet_UDP_Open(SDL_SwapBE16(*ioPortNumber)); if (sSocket == NULL) { SDLNet_FreePacket(sUDPPacketBuffer); sUDPPacketBuffer = NULL; return -1; } // Set up socket set sSocketSet = SDLNet_AllocSocketSet(1); SDLNet_UDP_AddSocket(sSocketSet, sSocket); // Set up receiver sKeepListening = true; sPacketHandler = packetHandler; sReceivingThread = SDL_CreateThread(receive_thread_function, "NetDDPOpenSocket_ReceivingThread", NULL); // Set receiving thread priority very high bool theResult = BoostThreadPriority(sReceivingThread); if(theResult == false) fdprintf("warning: BoostThreadPriority() failed; network performance may suffer\n"); //PORTGUESS but we should generally keep port in network order, I think? // We really ought to return the "real" port we bound to in *ioPortNumber... // for now we just hand back whatever they handed us. //*ioPortNumber = *ioPortNumber; return 0; }
void IRCLobby::readIRCLine(char *buf, size_t buf_len) { char *buf_end=buf+buf_len-1; char ch; char *buf_upto=buf; SDLNet_SocketSet sock_set=SDLNet_AllocSocketSet(1); SDLNet_TCP_AddSocket(sock_set,irc_server_socket); int no_activity=0; try { while(buf_upto < buf_end) { SDLNet_CheckSockets(sock_set, 1000); if(!SDLNet_SocketReady(irc_server_socket)) { if(++no_activity>=(60*1) && !expected_ping) { sendPingMessage(); } if(expected_ping && expected_ping<time(NULL)) { throw Exception("no pong received after ping"); } continue; } if(SDLNet_TCP_Recv(irc_server_socket,&ch,1)<=0) throw Exception("Couldn't read TCP: %s", SDLNet_GetError()); if(ch=='\r') { continue; } if(ch=='\n') { break; } *buf_upto++=ch; } } catch(std::exception& e) { buf[0]=0; SDLNet_FreeSocketSet(sock_set); throw; } SDLNet_FreeSocketSet(sock_set); *buf_upto=0; }
TCP_Socket::TCP_Socket(IPaddress ip) : sock(0), sockset(0) { get_Net(); if(!ip.host) throw TCP_Socket_Init_Failure(); sock = SDLNet_TCP_Open(&ip); sockset = SDLNet_AllocSocketSet(1); if(!sock || !sockset || SDLNet_TCP_AddSocket(sockset, sock) == -1) { SDLNet_TCP_Close(sock); SDLNet_FreeSocketSet(sockset); throw TCP_Socket_Init_Failure(); } }
bool TCPConnection::Init( const std::string &host, unsigned short port, bool server ) { isConnected = false; isServer = server; hostName = host; portNr = port; socketSet = SDLNet_AllocSocketSet( 1 ); if ( !ResolveHost() ) return false; if ( !OpenConnectionToHost() ) { logger->Log( __FILE__, __LINE__, "Opening connection to host faield : ", SDLNet_GetError() ); return false; } isConnected = !server; return true; }
// Creates a socket set that has the server socket and all the client sockets SDLNet_SocketSet Server::create_sockset() { static SDLNet_SocketSet set = NULL; if(set) SDLNet_FreeSocketSet(set); set = SDLNet_AllocSocketSet(num_clients + 1); if(!set) { std::cerr << "SDLNet_AllocSocketSet: " << SDLNet_GetError() << std::endl; return 0; } SDLNet_TCP_AddSocket(set, server); typedef std::map< std::string, TCPsocket >::iterator it_type; for (it_type i = clients.begin(); i != clients.end(); i++) { SDLNet_TCP_AddSocket(set, i->second); } return set; }
//------------------------------------------------------------------------------------- SDLNet_SocketSet GalactiCombatServer::createSockSet() { if(verbose) std::cout << "Entering createSockSet" << std::endl; static SDLNet_SocketSet set = NULL; if(set) SDLNet_FreeSocketSet(set); set = SDLNet_AllocSocketSet(clients.size() + 1); if(!set) { std::cerr << "SDLNet_AllocSocketSet done goofed: " << SDLNet_GetError() << std::endl; exit(1); } SDLNet_TCP_AddSocket(set, TCPServerSock); for(int i = 0; i < clients.size(); i++) if(SDLNet_TCP_AddSocket(set, clients[i]->sock) == -1) std::cerr<<"SDLNet_TCP_AddSocket done goofed: "<<SDLNet_GetError()<<std::endl; // SDLNet_UDP_AddSocket(set, UDPServerSock); //TODO: Uncomment when working on UDP. if(verbose) std::cout << "Exiting createSockSet" << std::endl << std::endl; return set; }
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; }
bool Networking::clientConnect(int *id){ printf("in connect\nPlease enter your hostname (default: pastamancer.cs.utexas.edu):"); std::string host; getline(std::cin, host); if (host.length() == 0) host = std::string("pastamancer.cs.utexas.edu"); printf("trying to connect to player 2...\n"); SDLNet_Init(); if(SDLNet_ResolveHost(&client_ip, host.c_str(), PORT) == -1){ printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError()); exit(0); return false; } client_socket = SDLNet_TCP_Open(&client_ip); while(!client_socket){ client_socket = SDLNet_TCP_Open(&client_ip); if(!client_socket){ printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError()); printf("trying again...\n"); } } client_socketset = SDLNet_AllocSocketSet(1); if (SDLNet_TCP_AddSocket(client_socketset, client_socket) == -1) { printf("SDLNet_TCP_AddSocket: %s\n", SDLNet_GetError()); // Probably need to make the socketset bigger return false; } //connected = true ConnectAck ack; SDLNet_TCP_Recv(client_socket, &ack, sizeof(ack)); *id = ack.id; for (int i = 0; i < 4; i++) { client_ids[i] = ack.ids[i]; } // printf("myID: %d\n", myId); return true; }
/* initialize the connection to the game server. Return value is non-zero in * case of an error, this value can be given to exit_connection () to clean * up */ int init_connection (char *hostname, short unsigned int port) { IPaddress ipad; /* initialize the SDLNet library */ if (SDLNet_Init () == -1) { log_error ("Failed to initialize SDL_net: %s\n", SDLNet_GetError()); return EXIT_SDL_QUIT; } /* resolve server IP address */ if (SDLNet_ResolveHost (&ipad, hostname, port) == -1) { log_error ("Cannot resolve host %s: %s\n", hostname, SDLNet_GetError()); return EXIT_SDLNET_QUIT; } /* try to connect to game server */ thesock = SDLNet_TCP_Open (&ipad); if (thesock == NULL) { log_error ("Unable to connect to the game server: %s\n", SDLNet_GetError ()); return EXIT_SDLNET_QUIT; } /* allocate a set for only one socket */ theset = SDLNet_AllocSocketSet (1); if (theset == NULL) { log_error ("Cannot allocate socket set: %s\n", SDLNet_GetError ()); return EXIT_CLOSE_SOCKET; } /* And add the socket */ if (SDLNet_TCP_AddSocket (theset, thesock) == -1) { log_error ("Cannot add socket to set: %s\n", SDLNet_GetError ()); return EXIT_FREE_SET; } buf_in_use = 0; return 0; }
//Returns a string response from the server if it could connect, else an error bool NetworkClient::Connect(int timeoutSeconds) { this->sockets = SDLNet_AllocSocketSet(1); if(this->sockets == NULL) { cgl::Cout("Failed to allocate sockets"); } this->clientsocket = SDLNet_TCP_Open(&this->serverIP); if(!this->clientsocket) { cgl::Cout("Failed to establish a connection. Server might be down, IP and port combo is wrong or server is full"); return false; } else { SDLNet_TCP_AddSocket(this->sockets, this->clientsocket); int activesockets = SDLNet_CheckSockets(this->sockets, timeoutSeconds); 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, this->messageOnSERVEROK.c_str()) == 0) { this->shutdown = false; //We are connected, do not shutdown the socket connection return true; } else { return false; } } else { cgl::Cout("No response from server"); return false; } } }
void* serverstart(void* ptr){ server* myserver = (server*)ptr; printf("Starting http-server on thread %d\n", pthread_self()); if (SDLNet_Init() < 0) printf("Init SDL_Net"); if (SDLNet_ResolveHost(&myserver->ip, NULL, 80) < 0) printf("Resolving host"); if (!(myserver->socket = SDLNet_TCP_Open(&myserver->ip))) printf("Opening port (already in use?)"); myserver->numsessions=0; myserver->clients=SDLNet_AllocSocketSet(1000); if(!myserver->clients) printf("SDLNet_AllocSocketSet failed\n"); printf("myserver started port: %d\n", SDLNet_Read16(&myserver->ip.port)); while(myserver->running){ TCPsocket socket; IPaddress *ip; if ((socket = SDLNet_TCP_Accept(myserver->socket))){ if ((ip = SDLNet_TCP_GetPeerAddress(socket))){ printf("Client connected: %x %d\n", SDLNet_Read32(&(ip->host)), SDLNet_Read16(&(ip->port))); }else{ printf("SDLNet_TCP_GetPeerAddress: %s\n", SDLNet_GetError()); } } } pthread_exit((void*) 0); }