Advertisement

SDL net programming

Started by November 22, 2005 05:48 AM
1 comment, last by lconnaug 19 years, 3 months ago
Hey, my school group is making a simple SDL game. I'm in charge of adding the multiplayer aspect to the game. I decided to use the SDL net libraries just to stay consistent with my team. In my main function, I set up the SDL net and get my server to listen by a thread calling my accept function. The accept function is just a loop that listens for each user that connects. Anyway, the accept function ends up creating a new thread ('receive') for each client that connects, and receives any information when they send it. Code for accept below I would like to just pass the new thread the information the client socket created, but in SDL_CreateThread(receive, NULL); it doesn't look like i can use that second parameter to send information. Just wondering if anyone can shed some light onto this subject. (The linked list is just to remember the clients and send the updates to all of them). Basically i just want to know the best way of sending data to a specific thread, only that one can access.

int accept(void* Data)
{
   TCPsocket tempsock;
   IPaddress* remoteIP;
   int quit2 = 0;
   while (quit2 == 0)
   {	
      if ((tempsock = SDLNet_TCP_Accept(tcpsock))){
         if ((remoteIP = SDLNet_TCP_GetPeerAddress(tempsock))){
            client* temp = new client;
            temp->socket= tempsock;
            temp->IP = remoteIP;
            temp->nxt= NULL;
            if (start_ptr == NULL){
               start_ptr = temp;
            }else{
               client* shift;
               shift = start_ptr;
               while (shift->nxt != NULL) {  
                  shift = shift->nxt;
               }
               shift->nxt = temp;
            }
            printf("Host connected: %x %d\n", SDLNet_Read32(&remoteIP->host), SDLNet_Read16(&remoteIP->port));
            SDL_Thread* T2 = SDL_CreateThread(receive, NULL);
         }
         else
            fprintf(stderr, "SDLNet_TCP_GetPeerAddress: %s\n", SDLNet_GetError());
         }
	 SDL_Delay(50);
      }	
   return 0;
}

You want to pass "temp" instead of NULL to the create thread function, so that the argument passed to your thread will be the client record. Else, how does the "receive" function know where to read data from?
enum Bool { True, False, FileNotFound };
Advertisement
Great that works, thanks.

Im trying to incorporate my code with the rest of my teams, the values that we will probably be sending to the other users is the position, speed, and direction, maybe facing as well. Most of those values in their code are floating points. Im wondering how you go about sending floating points over the network using the SDLNet_TCP_Send() function. Here is some example code, you can ignore the Uint32, I was just testing with different methods.

int Network::send_update(void* Data){	int quit = 0;	char data[1024];		Uint32 number;	while (!quit)	{		int len,result;		number = (Uint32)speed;  // <- tyring to send speed which is FP#		SDLNet_Write32(number,data);		printf("number:%s\n",data);		len=strlen(data)+1; // add one for the terminating NULL		result=SDLNet_TCP_Send(tcpsock,data,len);		if(result<len) {			printf("SDLNet_TCP_Send: %s\n", SDLNet_GetError());		}		SDL_Delay(100);	}	return 0;}

This topic is closed to new replies.

Advertisement