Advertisement

SDL_net

Started by December 16, 2002 11:37 PM
1 comment, last by flucknugget 22 years, 1 month ago
I just found SDL_net, and I''m really excited because the thought of making network games makes me giddy, and SDL_net seems to be very simple. I have no network experience though. I''ve looked through the networking section in the articles on this site, and most of the articles are on WinSock. I read a lot of a general every day networking article just to try to understand what''s going on, but it''s really confusing. Does anyone know where I can find any really simple sample network programs (that preferably use SDL_net) that are extremely simple- like maybe just a two computer chat program using TCP or something. Thanks. - f l u c k y p o o
- f l u c k y p o o
AFAIK SDL_net comes with an example of a simple chat program (look into chat.cpp and chat.h)
baumep
Advertisement
here my simple server/client with SDL_net.
the server is just a "listener", the client it is just a "sender".

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

  /*this is the server program.its a simple listener*/#define DIM 1024 //the max dimension we want#define PORTA 1234 //the port to listen on#define NUMSOCKET 10 //the number of clients we want to connect/*standard headers*/#include <stdlib.h>#include <stdio.h>#include <process.h>#include <direct.h>#include <string.h>/*SDL headers*/#include "SDL.h"#include "SDL_net.h"/******//*main*//******/int main(int argc, char *argv[]){char *host;//where we store the name of hostIPaddress ipaddress;//our ip addressTCPsocket tcpsock;//the "server" socketTCPsocket new_tcpsock;//a "temp" socketTCPsocket clients[NUMSOCKET];//an array of socket for the clientsSDLNet_SocketSet socketset = NULL; //a set of socketsint i=0;//generic counterint j=0;//generic counterint result=0;//to store the result of some functionschar messaggio [ DIM ];//this is our buffer/************************///init for sdlif (SDL_Init(0) < 0 ){printf("no init sdl !!!\n"); exit ( -1);}else { printf("sdl init : ok\n");}//init for sdl_netif (SDLNet_Init() < 0 ){printf("no init sdlnet !!!\n"); exit ( -1);}else { printf("init sdlnet : ok\n");}//create our socket setsocketset = SDLNet_AllocSocketSet(NUMSOCKET);if (socketset == NULL) {printf("no sockets created !!!\n"); exit ( -1);}else { printf("socket created number : %d\n",NUMSOCKET);}//init to NULL all the clients socketsfor (i=0;i<NUMSOCKET;i++) {clients =NULL;};<br>printf("sockets inizializzati\n");<br><br><br>//try to resolve address<br>if ( SDLNet_ResolveHost(&ipaddress,NULL,PORTA) &lt; 0 ){printf("WARNING : no resolve ! ! !\n"); <br>														printf("try to continue anyway\n");}<br>else { printf("resolve : ok\n");}<br><br><br><br>//try to resolve the name<br>if ( (host = SDLNet_ResolveIP (&ipaddress) ) == NULL ){printf("WARNING : no name ! ! !\n"); <br>														printf("try to continue anyway\n");}<br>else { printf("local name : %s\n",host);}<br><br><br>//simply say which port we are listening on  <br>printf("porta (default) : %d\n",PORTA);<br><br><br>//we try to open the server sock<br>//if it fails no way : WE MUST QUIT !!!<br>tcpsock = SDLNet_TCP_Open(&ipaddress);<br>if (!tcpsock){printf("no TCP\n"); exit ( -1);}<br>else { printf("TCP : ok\n");}<br><br><br>//add the server socket to the socket set<br>SDLNet_TCP_AddSocket(socketset, tcpsock);<br><br><br>printf("waiting incoming clients…\n" );<br><br><br><br>//this is the main loop<br><br>while (1) { SDLNet_CheckSockets(socketset, 0);//check for activity on our socket set<br><br>	if ( SDLNet_SocketReady(tcpsock) ) { //there is a new client socket !!!<br>										//accept it and store it in our array, and add to the set socket<br>										printf("socket connected\n");<br><br>										<br>										new_tcpsock  = SDLNet_TCP_Accept(tcpsock);<br>										<br><br>										clients[j] = new_tcpsock;<br>										<br><br>										SDLNet_TCP_AddSocket(socketset, clients[j]);<br>										<br><br>										j++;									<br>										<br><br>										};<br>	//now we need to check all clients sockets for activity<br>	for ( i=0; i&lt;NUMSOCKET; i++ ) {<br>		if ( SDLNet_SocketReady(clients) ) { //if there is activity on clients socket it is an incoming message<br>													result=SDLNet_TCP_Recv(clients, messaggio, DIM);<br><br>													if (result &lt;0) {printf ("the socket %d dosconnected\n",i);<br>																	clients=NULL;<br>																	}<br>													else{printf("riceived : %d bytes\nriceived %s\n",result,messaggio);}<br><br>												<br><br>													};<br>			};<br><br><br><br><br>};<br><br><br><br>system("pause");<br><br>SDL_Quit();<br><br>return 0;<br>}<br><br>/********************************/<br>/*if you want also do the MIRROR on every sockets clients<br>you need another loop ,something like this :*/<br>/*<br><br>  for ( i=0; i&lt;NUMSOCKET; i++ ) {<br>			if ( SDLNet_SocketReady(clients) ) {<br>													result=SDLNet_TCP_Recv(clients, messaggio, DIM);<br>													if (result &lt;0) {printf ("il socket %d disconnected\n",i);<br>																	clients=NULL;<br>																	}<br>													else{printf("printf("riceived : %d bytes\nriceived %s\n",result,messaggio);}<br><br>													//this is the second loop<br>													len=strlen(messaggio)+1;<br>													for ( i=0; i&lt;NUMSOCKET; i++ ) {if(clients!=NULL) {result=SDLNet_TCP_Send(clients,messaggio,len);<br>																					printf("mirror on socket : %d\n",i);<br>																					printf("byte sended %d\n",result);	};//close if<br>																					};//close for<br>/**/<br><br>/*anyway to accept plus send data the client must have some kind of functions to check events.<br>to use events you must use sdl init on video flag.*/</font><br>  </pre></DIV><!–ENDSCRIPT–><br><br><br>$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$<br>    <!–STARTSCRIPT–><BR><DIV CLASS=source><pre>  <br><font color="gray">/*a simple client<br>it connect to the server and start to send messages*/<br><br>#define DIM 1024	//our max dimension<br>#define PORTA 1234//the port which we want to listen on<br><br><br>/*standard headers*/<br>#include &lt;stdlib.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;process.h&gt;<br>#include &lt;direct.h&gt;<br>#include &lt;string.h&gt;<br><br>/*sdl headers*/<br>#include "SDL.h"<br>#include "SDL_net.h"<br><br>/************************/<br><br><br>int main(int argc, char *argv[])<br><br>{<br><br>char *host;//where we store the name of host<br><br>IPaddress ipaddress;//the ip address<br>TCPsocket tcpsock;//the socket to use<br><br>char nomeserver[50];//max dimension of the name of server<br><br>int result;//to store the result of some functions<br>int len;//the lenght of the message to send<br>char messaggio [ DIM ];//this is our buffer<br>/************************/</font><br><br><br><font color="gray">//init sdl<br></font><br><font color="blue">if</font> (SDL_Init(0) &lt; 0 ){printf(<font color="darkred">"no init sdl\n"</font>); exit ( -1);}<br><font color="blue">else</font> { printf(<font color="darkred">"sdl init : ok\n"</font>);}<br><br><br><font color="gray">//init sdl_net<br></font><br><font color="blue">if</font> (SDLNet_Init() &lt; 0 ){printf(<font color="darkred">"no init sdlnet\n"</font>); exit ( -1);}<br><font color="blue">else</font> { printf(<font color="darkred">"init sdlnet : ok\n"</font>);}<br><br><font color="gray">//scanf for the name of server<br></font><br>printf(<font color="darkred">"Name (IP) server : \n"</font>);<br>scanf(<font color="darkred">"%s"</font>,&nomeserver);<br>printf(<font color="darkred">"porta (default) : %d\n"</font>,PORTA);<br><br><font color="gray">//try to resolve the name<br></font><br><font color="blue">if</font> ( SDLNet_ResolveHost(&ipaddress,nomeserver,1234) &lt; 0 ){printf(<font color="darkred">"no resolve\n"</font>); <br>														printf(<font color="darkred">"<font color="blue">continue</font> anyway\n"</font>);}<br><font color="blue">else</font> { printf(<font color="darkred">"resolve : ok\n"</font>);}<br><br><br><br><font color="gray">//try to resolve the ip<br></font><br><font color="blue">if</font> ( (host = SDLNet_ResolveIP (&ipaddress) ) == NULL ){printf(<font color="darkred">"no ip\n"</font>); <br>													printf(<font color="darkred">"<font color="blue">continue</font> anyway\n"</font>);}<br><font color="blue">else</font> { printf(<font color="darkred">"connesso a : %s\n"</font>,host);}<br><br><br><br><font color="gray">//try to open the socket<br></font><br><font color="gray">//if it fails no way : WE MUST QUIT !!!<br></font><br>tcpsock = SDLNet_TCP_Open(&ipaddress);<br><br><font color="blue">if</font> (!tcpsock){printf(<font color="darkred">"no TCP\n"</font>); exit ( -1);}<br><font color="blue">else</font> { printf(<font color="darkred">"TCP : ok\n"</font>);}<br><br><br><br>printf(<font color="darkred">"dear user start to write your messages\n"</font>);<br>printf(<font color="darkred">"and press enter to send it :\n"</font>);<br><br><br><br><font color="gray">//this is the main loop<br></font><br><font color="blue">while</font> (1)<br>{<br><font color="gray">//scanf for the message to send<br></font><br>scanf(<font color="darkred">"%s"</font>,&messaggio);<br><br><font color="gray">//calculate lenght and send it<br></font><br>len=strlen(messaggio)+1;<br>result = SDLNet_TCP_Send(tcpsock,messaggio,len);<br><br>printf(<font color="darkred">"send byte  : %d\n"</font>, result);<br><br>printf(<font color="darkred">"send : %s\n"</font>,messaggio);<br><br><br>printf(<font color="darkred">"write a <font color="blue">new</font> message :\n"</font>);<br>																<br>};<font color="gray">//chiude ciclo infinito<br></font><br><br><br><br><br>system(<font color="darkred">"pause"</font>);<br><br>SDL_Quit();<br><br><font color="blue">return</font> 0;<br>}<br>  </pre></DIV><!–ENDSCRIPT–>     <br><br>     

This topic is closed to new replies.

Advertisement