void ngl_net_open_client(char num) { if (num==0) { if ((csd0 = SDLNet_TCP_Accept(sd))) { waiting0=0; reading0=1; printf("\nConectado el 0\n"); } } if (num==1) { if ((csd1 = SDLNet_TCP_Accept(sd))) { waiting1=0; reading1=1; printf("\nConectado el 1\n"); } } if (num==2) { if ((csd2 = SDLNet_TCP_Accept(sd))) { waiting2=0; reading2=1; printf("\nConectado el 2\n"); } } }
void waitForClients(TCPsocket *sd) { bool quit = false, quit2 = false; bool connectedClient = false; //printf("Waiting for connection\n"); while (!quit) { //Väntar på klienter if ((csd[0] = SDLNet_TCP_Accept(*sd))) { quit = true; while(!quit2) { if((csd[1] = SDLNet_TCP_Accept(*sd))) { quit2 = true; } } } } }
void NetworkManager::NetworkHost(void) //http://content.gpwiki.org/index.php/SDL:Tutorial:Using_SDL_net <- good code here, adapt messages accordingly { //http://jcatki.no-ip.org:8080/SDL_net/SDL_net.html <- good documentation here TCPsocket sd; /* Call this function once Host button is selected */ IPaddress ip; char buffer[512]; server = true; if (SDLNet_Init() < 0) { fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError()); exit(EXIT_FAILURE); } /* Resolving the host using NULL make network interface to listen */ if (SDLNet_ResolveHost(&ip, NULL, 2000) < 0) { fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError()); exit(EXIT_FAILURE); } /* Open a connection with the IP provided (listen on the host's port) */ if (!(sd = SDLNet_TCP_Open(&ip))) { fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError()); exit(EXIT_FAILURE); } /* Wait for a connection*/ while (!(targetSocket = SDLNet_TCP_Accept(sd))) { } std::cout<<"accepted client"; }
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 */ } }
/* ================ Thread Functions ================ */ static void TcpLoop(bool *abort, ConnectionListener *listener, NewConnectionCallback cb) { IPaddress ip; TCPsocket server_socket = 0; if (SDLNet_ResolveHost(&ip, NULL, TCP_SERVER_PORT) == -1) { string msg = (string)"SDLNet_ResolveHost() failed : " + SDLNet_GetError(); Log::Error(msg); throw std::runtime_error(msg); } server_socket = SDLNet_TCP_Open(&ip); if (!server_socket) { string msg = (string)"SDLNet_TCP_OPEN() failed: " + SDLNet_GetError(); Log::Error(msg); throw std::runtime_error(msg); } while (!(*abort)) { TCPsocket new_socket = SDLNet_TCP_Accept(server_socket); if (new_socket) { Log::Debug("New TCP connection!"); (*listener.*cb)(new_socket); } // Sleep for 100 milliseconds std::this_thread::sleep_for(std::chrono::microseconds(100000)); } }
int game_host::waitForClient_test() { cout << "waiting for first player to connect...\n"; while(!(player1sd = SDLNet_TCP_Accept(sd))) //wait for first connection, with 50ms delay to keep CPU down SDL_Delay(50); string ipMess = ""; while(SDLNet_UDP_Recv(p1UDPsock, UDPpack1) == 0) SDL_Delay(5); SDLNet_UDP_Bind(p1UDPsock,10,&(UDPpack1->address)); cout << this->recieveMessagep1() << "\n"; //on successful connect, client sends a message SDLNet_TCP_AddSocket(socketset, player1sd); //could error check here this->player1ip = SDLNet_TCP_GetPeerAddress(player1sd); //SDLNet_ResolveHost(&(UDPpack1->address), cl.c_str(), port); //player2ip->host = SDLNet_TCP_GetPeerAddress(player1sd)->host; //player2ip->port = port; //UDPpack1->address = *player1ip; sendtoP1_test("1SIG:START"); cout << "client connected, continuing...\n"; return 1; }
void NetworkThread::__run__() { TCPsocket csd; IPaddress* remoteIP; bool quit = false; while (!quit) { std::cout << "<<<<<<<<<<<<< Server::waitForConnection() - WAITING..." << std::endl; SDL_Delay(1000); //emit testSignal(); //quit = true; /* This check the sd if there is a pending connection. * If there is one, accept that, and open a new socket for communicating */ if ((csd = SDLNet_TCP_Accept(sd))) { /* Now we can communicate with the client using csd socket * sd will remain opened waiting other connections */ /* Get the remote address */ if ((remoteIP = SDLNet_TCP_GetPeerAddress(csd))) { /* Print the address, converting in the host format */ //std::cout << "<<<<<<<<<<<<< Host connected: " << SDLNet_Read32(&remoteIP->host) << " port: " << SDLNet_Read16(&remoteIP->port) << std::endl; //std::cout << "<<<<<<<<<<<<< Host connected: " << remoteIP->host << " port: " << remoteIP->port << std::endl; //clientVector.push_back(&csd); clientSocketList->addClient(csd); emit newNetworkConnection("<b>[NET]</b> A new Client connected<br>"); } else { std::cout << "<<<<<<<<<<<<< SDLNet_TCP_GetPeerAddress: " << SDLNet_GetError() << std::endl; } } } }
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; } } } }
void Users::process(){ int n = SDLNet_CheckSockets(set, 0); if (n < 1) return; if (SDLNet_SocketReady(sock)){ printf("new user...\n"); TCPsocket newuser = SDLNet_TCP_Accept(sock); printf("Accepted...\n"); User *u = new User(newuser); push_back(u); SDLNet_TCP_AddSocket(set,newuser); } User *u; for( iterator i = begin(); i != end(); i++ ){ u = *i; if (SDLNet_SocketReady(u->sock)){ int len; char buf[MAXLEN]; if ((len = readLine(u->sock,buf,sizeof(buf))) > 0){ fprintf(stderr,"got len=%d text from '%s': '%s'\n",len,u->name,buf); handle_command(buf,u); } else { printf("dead connection\n"); SDLNet_TCP_DelSocket(set, u->sock); SDLNet_TCP_Close(u->sock); i = erase(i); } } } }
/* Setup TCP Server, and wait for a single * client to connect */ int setup_server(unsigned port) { #if !defined(PSP) && !defined(EMSCRIPTEN) && !defined(DREAMCAST) && !defined(THREE_DS) is_server = 1; log_message(LOG_INFO, "Starting server on port %u\n",port); //SDL_INIT(SDL_INIT_EVERYTHING); SDLNet_Init(); IPaddress ip; SDLNet_ResolveHost(&ip, NULL, port); server = SDLNet_TCP_Open(&ip); log_message(LOG_INFO, "Waiting for client to connect\n"); while (client == NULL) { client = SDLNet_TCP_Accept(server); SDL_Delay(1000); } const char * message = "Welcome to GB server"; SDLNet_TCP_Send(client, message, strlen(message) + 1); log_message(LOG_INFO, "Client successfully connected\n"); socketset = SDLNet_AllocSocketSet(1); SDLNet_TCP_AddSocket(socketset, client); connection_up = 1; return 1; #endif return 0; }
void NetServer::handleserver() { int which; unsigned char data; TCPsocket newsock = SDLNet_TCP_Accept(tcpsock); if (newsock == NULL) return; /* Look for unconnected person slot */ for (which = 0; which < MAXCLIENTS; ++which) { if (!clients[which].sock) { break; } } if (which == MAXCLIENTS) { /* Look for inactive person slot */ for (which = 0; which < MAXCLIENTS; ++which) { if (clients[which].sock && !clients[which].active) { /* Kick them out.. */ data = NET_MSG_REJECT; SDLNet_TCP_Send(clients[which].sock, &data, 1); SDLNet_TCP_DelSocket(socketset, clients[which].sock); SDLNet_TCP_Close(clients[which].sock); numclients--; #ifdef _DEBUG printf("Killed inactive socket %d\n", which); #endif break; } } } if (which == MAXCLIENTS) { /* No more room... */ data = NET_MSG_REJECT; SDLNet_TCP_Send(newsock, &data, 1); SDLNet_TCP_Close(newsock); #ifdef _DEBUG printf("Connection refused -- server full\n"); #endif } else { /* Add socket as an inactive person */ clients[which].sock = newsock; clients[which].peer = *SDLNet_TCP_GetPeerAddress(newsock); SDLNet_TCP_AddSocket(socketset, clients[which].sock); numclients++; #ifdef _DEBUG printf("New inactive socket %d\n", which); #endif } }
/** * @brief Acepta conexiones entrantes * * @return En caso de no poder aceptar ninguna conexión, devolverá false. Si se puede aceptar una conexión entrante, * devolverá true. * * Se intentará entablar una conexión con un cliente remoto por medio de la función SDLNet_TCP_Accept(). * Si todo va bien, se entablará la conexión. */ bool CHostSocket::Accept (CClientSocket& the_client_socket) { TCPsocket cs; if ((cs = SDLNet_TCP_Accept(m_Socket))) { the_client_socket.SetSocket(cs); return true; } else return false; }
static __inline__ TCPsocket snTCPAccept(TCPsocket s) { TCPsocket socket = NULL; lockSDLNet(); socket = SDLNet_TCP_Accept(s); unlockSDLNet(); return socket; }
void CServer::OpenServer() { if (SDLNet_ResolveHost (&addr, NULL, 6699) < 0) { printf ("ERR ResolveHost: %s\n", SDLNet_GetError ()); SDLNet_Quit (); exit (-1); } server = SDLNet_TCP_Open (&addr); if (server == NULL) { printf ("ERR TCP_Open: %s\n", SDLNet_GetError ()); SDLNet_Quit (); exit (-1); } client = SDLNet_TCP_Accept (server); cout << "Server wurde Gestartet !" << endl; int x = 0; while (client == NULL) { x++; SDL_Delay (1000); client = SDLNet_TCP_Accept (server); if (x == 10) { pMenu->start = false; pMenu->bServer = false; pFramework->done = true; SAFE_DELETE (remoteIP); SDLNet_TCP_Close(server); SDLNet_Quit(); cout << "Server wurde Beendet !" << endl; return; } } cout << "Client hat verbunden !" << endl; }
TCPConnection* TCPServer::accept() { TCPConnection* conn = nullptr; if (sd) { TCPsocket newsd = SDLNet_TCP_Accept(sd); if (newsd) { conn = new TCPConnection(Address(0, 0)); ((TCPServer*)conn)->sd = newsd; } } return conn; }
/* posloucha na serversock pro prichozi spojeni, pokud se mu podari vytvorit spojeni, * zapise ho do clientsock uvnitr tridy klientskeho socketu*/ bool TTCPServerSocket::accept (TCPClientSocket client_socket) { TCPsocket newclient; newclient = SDLNet_TCP_Accept(serversock); if(!newclient) { std::cerr << "SDLNet_TCP_Accept: " << SDLNet_GetError() << std::endl; } else { client_socket->clientsock = newclient; client_socket->connected = true; } return ( newclient? true:false); }
void TCOD_noteye_check() { if(!tnoteye.mode) return; TCOD_noteye_init(); if(delayed) TCOD_noteye_sendscreen(tbuffer); int i; for(i=0; i < MAXCLIENT; i++) if(client[i] == NULL) break; if(i < MAXCLIENT && i < tnoteye.maxClients) { TCPsocket skt = SDLNet_TCP_Accept(server->socket); if(skt) { client[i] = new NTCPStream(skt); client[i]->writeStr("NotEye stream"); client[i]->writeInt(NOTEYEVER); client[i]->writeStr(tnoteye.name); if(scr) { TCOD_noteye_render(); client[i]->writeInt(nepScreen); client[i]->writeScr(scr); client[i]->flush(); } } } for(int i=0; i < tnoteye.maxClients; i++) while(client[i] && client[i]->ready()) { if(client[i]->eof()) { delete client[i]; client[i] = NULL; } else { int nep = client[i]->readInt(); if(nep == nepKey) { SDL_Event ev; SDL_KeyboardEvent& kev(ev.key); kev.keysym.sym = SDLKey(client[i]->readInt()); kev.keysym.mod = SDLMod(client[i]->readInt()); ev.type = client[i]->readInt() == evKeyDown ? SDL_KEYDOWN : SDL_KEYUP; kev.keysym.unicode = client[i]->readInt(); SDL_PushEvent(&ev); } else if(nep == nepMouse) { tnoteye.x = client[i]->readInt(); tnoteye.y = client[i]->readInt(); tnoteye.state = client[i]->readInt(); tnoteye.active = true; } else if(nep == nepMessage) { string s = client[i]->readStr(); TCOD_noteye_writestr(s.c_str()); } else fprintf(stderr, "Unknown NEP: %d\n", nep); } } }
bool CHostSocket::Accept(CClientSocket& clientSocket) { TCPsocket clientSock; if((clientSock = SDLNet_TCP_Accept(_socket))) { clientSocket.SetSocket(clientSock); return true; } else{return false;} }
int game_host::waitForClients() { cout << "waiting for first player to connect...\n"; while(!(player1sd = SDLNet_TCP_Accept(sd))) //wait for first connection, with 50ms delay to keep CPU down SDL_Delay(50); string ipMess = ""; while(SDLNet_UDP_Recv(p1UDPsock, UDPpack1) == 0) SDL_Delay(5); SDLNet_UDP_Bind(p1UDPsock,10,&(UDPpack1->address)); cout << this->recieveMessagep1() << "\n"; //client sends its IP SDLNet_TCP_AddSocket(socketset, player1sd); //could error check here this->player1ip = SDLNet_TCP_GetPeerAddress(player1sd); //UDPpack1->address.port = player1ip.port; //UDPpack1->address.host = player1ip.host; cout << "waiting for second player to connect...\n"; while(!(player2sd = SDLNet_TCP_Accept(sd))) SDL_Delay(50); while(!SDLNet_UDP_Recv(p1UDPsock, UDPpack2)) SDL_Delay(5); SDLNet_UDP_Bind(p1UDPsock,10,&(UDPpack2->address)); //cout << this->recieveMessagep2() << "\n"; SDLNet_TCP_AddSocket(socketset, player2sd); //could error check here as well player2ip = SDLNet_TCP_GetPeerAddress(player2sd); //UDPpack2->address.port = player2ip.port; //UDPpack2->address.host = player2ip.host; if(SDLNet_TCP_Send(player1sd, "1SIG:START", 16 /* buff.length()+1*/) < 16) { cout << "Message to client 1 failed to send...\n"; return -1; } if(SDLNet_TCP_Send(player2sd, "2SIG:START", 16 /*buff.length()+1*/) < 16) { cout << "Message to client 2 failed to send...\n"; return -1; } cout << "both clients connected, continuing...\n"; return 1; }
TCPClientSocket* TCPServerSocket::Accept() { TCPsocket new_tcpsock; new_tcpsock=SDLNet_TCP_Accept(mysock); if(!new_tcpsock) { //printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError()); return 0; } return new TCPClientSocket(new_tcpsock); }
void HandleServer(void) { TCPsocket newsock; int which; unsigned char data; newsock = SDLNet_TCP_Accept(servsock); if ( newsock == NULL ) { return; } /* Look for unconnected person slot */ for ( which=0; which<CHAT_MAXPEOPLE; ++which ) { if ( ! people[which].sock ) { break; } } if ( which == CHAT_MAXPEOPLE ) { /* Look for inactive person slot */ for ( which=0; which<CHAT_MAXPEOPLE; ++which ) { if ( people[which].sock && ! people[which].active ) { /* Kick them out.. */ data = CHAT_BYE; SDLNet_TCP_Send(people[which].sock, &data, 1); SDLNet_TCP_DelSocket(socketset, people[which].sock); SDLNet_TCP_Close(people[which].sock); #ifdef DEBUG fprintf(stderr, "Killed inactive socket %d\n", which); #endif break; } } } if ( which == CHAT_MAXPEOPLE ) { /* No more room... */ data = CHAT_BYE; SDLNet_TCP_Send(newsock, &data, 1); SDLNet_TCP_Close(newsock); #ifdef DEBUG fprintf(stderr, "Connection refused -- chat room full\n"); #endif } else { /* Add socket as an inactive person */ people[which].sock = newsock; people[which].peer = *SDLNet_TCP_GetPeerAddress(newsock); SDLNet_TCP_AddSocket(socketset, people[which].sock); #ifdef DEBUG fprintf(stderr, "New inactive socket %d\n", which); #endif } }
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); }
//------------------------------------------------------------------------------------- void GalactiCombatServer::listenForConnections() { if(verbose) std::cout << "Entering listenForConnections" << std::endl; TCPsocket TCPsock = SDLNet_TCP_Accept(TCPServerSock); if(TCPsock) { char *msg = NULL; if(NetworkUtil::TCPReceive(TCPsock, &msg)) { if(verbose) std::cout << "Received new connection with message: " << msg << std::endl; Packet pack = NetworkUtil::charArrayToPacket(msg); if(pack.type != CONNECTION) //FIXME: THIS SOMETIMES HAPPENS? WHY? { std::cerr << "Connection Error. Someone sent something other than a CONNECTION packet as a new socket." << std::endl; return; } free(msg); //bind UDP //IPaddress *clientIP = SDLNet_TCP_GetPeerAddress(TCPsock); //int channel = NetworkUtil::UDPBind(UDPServerSock, -1, clientIP); //if(channel == -1) exit(4); //error has occurred int channel = -1;//TODO: Remove this line when working on UDP. //add the client std::string name = pack.message; free(pack.message); // Server crashes when names conflict. int sameName = 0; std::string oldName = name; while(this->findClientByName(name) != -1) { if(verbose) std::cout<<"There is already a user named "<<name<<"."<<std::endl; std::stringstream ss; ss<<oldName<<sameName; sameName++; name = ss.str(); } Client* client = this->addClient(TCPsock, channel, name); if(verbose) std::cout << name << " has logged in!" << std::endl; if(verbose) std::cout << clients.size() << " players are logged in." << std::endl; } else { std::cerr << "Failed to receive from new connection. Closing connection." << std::endl; SDLNet_TCP_Close(TCPsock); } } if(verbose) std::cout << "Exiting listenForConnections" << std::endl << std::endl; }
bool TCPConnectionServer::AcceptConnection() { TCPsocket socket = SDLNet_TCP_Accept( tcpSocket ); if ( socket == nullptr ) { // Connection failed, this just means no new client is trying to connect return false; } serverSocket.push_back(socket ); isSocketConnected[ serverSocket.size() - 1 ] = true; return true; }
bool C_Server::FindConnection() { m_clientSocket = SDLNet_TCP_Accept(m_serverSocket); if(m_clientSocket){ m_connected = true; m_socketsUsed = SDLNet_TCP_AddSocket(m_socketSet, m_clientSocket); if(m_socketsUsed == -1) printf("SDLNet_TCP_AddSocket: %s\n\n", SDLNet_GetError()); } else m_connected = false; return m_connected; }
/*/////////////////////////////////////////////////////////// Function for operation on threads /////////////////////////////////////////////////////////////*/ void * handle(void * data) { struct threadID * temp_thread=(struct threadID *) data; int quit1=1; int *temp=(int *) data; char buffer[512]; TCPsocket client_socket; while(1) // to make sure thread doesn't terminate { if(temp_thread->status==0) { sem_wait(&(temp_thread->binary_sem)); } printf("\n Activating thread"); // here thread start receiving data from client while(quit1) { if(client_socket=SDLNet_TCP_Accept(temp_thread->client.server_socket)) { printf("\nGetting request from client"); quit1=1; while(quit1) { if(temp_thread->client.client_ipaddress=SDLNet_TCP_GetPeerAddress(client_socket)) printf("\n Listening from client:%x",SDLNet_Read32(&temp_thread->client.client_ipaddress->host)); if(SDLNet_TCP_Recv(client_socket,buffer,512)) { printf("\nClient send:%s",buffer); if(!strcmp(buffer,"exit")) { printf("\n Client requesting for closing connection"); quit1=0; } if(!strcmp(buffer,"quit")) { quit1=0; quit=0; } } } printf("\nClosing client socket"); SDLNet_TCP_Close(client_socket); } } quit1=1; //so that this thread can be used again temp_thread->status=0; push_thread(temp_thread); }// here it ends printf("\n Terminating thread"); //TODO }
std::auto_ptr<Connection> Connection::accept(){ ScopedMutexLock(pimpl_->dataAccess); if( pimpl_->userSocket == NULL){ std::cerr << __FILE__ << " " << __LINE__ << ": " << "Connection::accept(): cant accept from a null socket" << std::endl; return std::auto_ptr<Connection> (NULL); } TCPsocket sock = SDLNet_TCP_Accept(pimpl_->userSocket); if( sock == NULL ){ std::cerr << __FILE__ << " " << __LINE__ << ": " << "SDLNet_TCP_Accept: " << SDLNet_GetError() << std::endl; return std::auto_ptr<Connection> (NULL); } std::auto_ptr<Connection> newConnection( new Connection(sock) ); return newConnection; }
/* perform initialization and start up game loop */ int main(int argc, char *argv[]) { int rval; /* start up SDL and create window */ if (init() < 0) { fprintf(stderr, "failed to initialize!\n"); rval = 1; goto exit; } /* load media */ if (load_media() < 0) { fprintf(stderr, "failed to load media!\n"); rval = 1; goto exit; } /* networking setup */ /* these definitions need to be put in the global space */ IPaddress ip; TCPsocket server = NULL; TCPsocket client = NULL; if (SDLNet_ResolveHost(&ip, NULL, 9999) < 0) { fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError()); rval = 1; goto exit; } server = SDLNet_TCP_Open(&ip); if (!server) { fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError()); rval = 1; goto exit; } /* now listen for client */ goto exit; while (!client) { SDL_Delay(0); client = SDLNet_TCP_Accept(server); } /* start game loop */ printf("starting loop\n"); game_loop(); rval = 0; exit: /* free resources and close SDL */ cleanup(); return rval; }
void * main_thread(void * data) { int flag=1,choice,n; int temp; TCPsocket client_socket,server_socket; if(!intialize_thread()) { printf("\n Therer is some error while initializing thread"); return 0; } printf("\n Value of active ports is:%d",get_active_threads()); // first server receive request from client to connect and open master server socket. To be created only once.-permanent if(common_connect_server(&host_ipaddress,&server_socket,global_port,(const char *)NULL)==0) return (void *)1; while(quit) { // Open client socket on server side for sending dynamic port address-temprary if((client_socket=SDLNet_TCP_Accept(server_socket))) { // send port address to client pthread_mutex_lock(&mutex_variable); printf("\n Value of active ports:%d",get_active_threads()); if(get_active_threads()==0) { int temp=0; SDLNet_TCP_Send(client_socket,&(temp),2); SDLNet_TCP_Close(client_socket); pthread_mutex_unlock(&mutex_variable); } else if(SDLNet_TCP_Send(client_socket,&(thread_header->client.port),2)==2) { printf("\nNew Port is send to client"); // close temprary client socket SDLNet_TCP_Close(client_socket); // opening port so that server can accept content from client in different thread; pthread_mutex_unlock(&mutex_variable); printf("\n Activating thread"); activate_thread(); } } } printf("\nEverything is OK now exiting"); SDLNet_TCP_Close(server_socket); cleanup_thread(); return NULL; }
int new_client(t_tmp **newclt) { TCPsocket newsock; NETDEBUG("Un nouveau client s'est connecté\n"); newsock = SDLNet_TCP_Accept(cnt->server.sock); if (!newsock) fprintf(stderr, "accept: %s\n", SDLNet_GetError()); else { put_in_client(newclt, newsock, STATE_NEW); call_handler((*newclt)->c, NULL); return (1); } return (0); }