Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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;
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
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
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
00062 quit = 0;
00063 while (!quit)
00064 {
00065
00066
00067 if ((csd = SDLNet_TCP_Accept(sd)))
00068 {
00069
00070
00071
00072
00073 if ((remoteIP = SDLNet_TCP_GetPeerAddress(csd)))
00074
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
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
00089
00090 if (SDLNet_TCP_Recv(csd, buffer, 512) > 0)
00091 {
00092 buffer[message_length] = '\0';
00093 printf("Client says: %s\n", buffer);
00094
00095 if(strcmp(buffer, "exit") == 0)
00096 {
00097 quit2 = 1;
00098 printf("Terminate connection\n");
00099 }
00100 if(strcmp(buffer, "quit") == 0)
00101 {
00102 quit2 = 1;
00103 quit = 1;
00104 printf("Quit program\n");
00105 }
00106 }
00107 }
00108
00109
00110 SDLNet_TCP_Close(csd);
00111 }
00112 }
00113
00114 SDLNet_TCP_Close(sd);
00115 SDLNet_Quit();
00116
00117 return EXIT_SUCCESS;
00118 }