ana/test/sdl_server.cpp

Go to the documentation of this file.
00001 /* $Id: sdl_server.cpp 46030 2010-08-27 20:35:02Z mordante $ */
00002 
00003 /**
00004  * @file
00005  * @brief Server application using SDL_net to test compatibility with ana.
00006  *
00007  * Language : C
00008  *
00009  * Code was taken from:
00010  *        <http://gpwiki.org/index.php/SDL:Tutorial:Using_SDL_net>
00011  *
00012  * Documentation at gpwiki is published under the GNU Free Documentation License 1.2.
00013  *    See <http://www.gnu.org/copyleft/fdl.html>
00014  *
00015  * ana is free software: you can redistribute it and/or modify
00016  * it under the terms of the GNU General Public License as published by
00017  * the Free Software Foundation, either version 2 of the License, or
00018  * (at your option) any later version.
00019  *
00020  * ana is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License
00026  * along with ana.  If not, see <http://www.gnu.org/licenses/>.
00027  *
00028  */
00029 
00030 #include <SDL/SDL_net.h>
00031 
00032 const int DEFAULT_PORT = 2000;
00033 
00034 int main(int argc, char **argv)
00035 {
00036     TCPsocket sd, csd; /* Socket descriptor, Client socket descriptor */
00037     IPaddress ip, *remoteIP;
00038     int quit, quit2;
00039     char buffer[512];
00040 
00041     if (SDLNet_Init() < 0)
00042     {
00043         fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError());
00044         exit(EXIT_FAILURE);
00045     }
00046 
00047     /* Resolving the host using NULL make network interface to listen */
00048     if (SDLNet_ResolveHost(&ip, NULL, DEFAULT_PORT) < 0)
00049     {
00050         fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError());
00051         exit(EXIT_FAILURE);
00052     }
00053 
00054     /* Open a connection with the IP provided (listen on the host's port) */
00055     if (!(sd = SDLNet_TCP_Open(&ip)))
00056     {
00057         fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError());
00058         exit(EXIT_FAILURE);
00059     }
00060 
00061     /* Wait for a connection, send data and term */
00062     quit = 0;
00063     while (!quit)
00064     {
00065         /* This check the sd if there is a pending connection.
00066         * If there is one, accept that, and open a new socket for communicating */
00067         if ((csd = SDLNet_TCP_Accept(sd)))
00068         {
00069             /* Now we can communicate with the client using csd socket
00070             * sd will remain opened waiting other connections */
00071 
00072             /* Get the remote address */
00073             if ((remoteIP = SDLNet_TCP_GetPeerAddress(csd)))
00074                 /* Print the address, converting in the host format */
00075                 printf("Host connected: %x %d\n", SDLNet_Read32(&remoteIP->host), SDLNet_Read16(&remoteIP->port));
00076             else
00077                 fprintf(stderr, "SDLNet_TCP_GetPeerAddress: %s\n", SDLNet_GetError());
00078 
00079             quit2 = 0;
00080             while (!quit2)
00081             {
00082                 // Added this, from here
00083                 char buf[4];
00084 
00085                 const int message_length = *( reinterpret_cast<int*>(buf) );
00086 
00087                 printf("Next message length %d\n", message_length);
00088                 // ... to here
00089 
00090                 if (SDLNet_TCP_Recv(csd, buffer, 512) > 0)
00091                 {
00092                     buffer[message_length] = '\0';     // Added this, interpret the message correctly ugly fix
00093                     printf("Client says: %s\n", buffer);
00094 
00095                     if(strcmp(buffer, "exit") == 0) /* Terminate this connection */
00096                     {
00097                         quit2 = 1;
00098                         printf("Terminate connection\n");
00099                     }
00100                     if(strcmp(buffer, "quit") == 0) /* Quit the program */
00101                     {
00102                         quit2 = 1;
00103                         quit = 1;
00104                         printf("Quit program\n");
00105                     }
00106                 }
00107             }
00108 
00109             /* Close the client socket */
00110             SDLNet_TCP_Close(csd);
00111         }
00112     }
00113 
00114     SDLNet_TCP_Close(sd);
00115     SDLNet_Quit();
00116 
00117     return EXIT_SUCCESS;
00118 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:45 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs