00001
00002
00003
00004
00005
00006 #include <stdio.h>
00007 #include <j2k/Net/include.hpp>
00008 #include <j2k/Net/net.hpp>
00009
00010
00011 int main( int argc, char** argv )
00012 {
00013 int i = 0;
00014 int n = 0;
00015 char buffer[101];
00016 TcpSocket *ts = NULL;
00017 Socket *s = NULL;
00018 BOOL halt = FALSE;
00019 TcpServerSocket server(12345);
00020
00021 memset( buffer, 0, 101 );
00022
00023 if ( argc > 1 )
00024 {
00025 printf( "Launching server... \n " );
00026
00027 printf( "Server launched! \n" );
00028 }
00029
00030 printf( "started...\n" );
00031 fflush( stdout );
00032
00033 while( halt != TRUE )
00034 {
00035 printf( "#" );
00036 fflush( stdout );
00037
00038 s = server.accept();
00039 ts = new TcpSocket(*s);
00040
00041 printf( "Sent welcome \n" );
00042 fflush( stdout );
00043
00044 ts->send("Welcome to echo server 0.1.0\n(C)Wolf Bergenheim 2001\n",53);
00045 ts->send("This server will echo everything you type back to you\n",54);
00046 ts->send("Send CLOSE to close this connection and HALT to shut down the server\n",69);
00047
00048 while ( 1 )
00049 {
00050 printf( "Reading 100 bytes... \n" );
00051 fflush( stdout );
00052
00053 n = ts->read(buffer, 100);
00054 if (n == -1)
00055 perror("testserver:main:recieve");
00056 if(ts->send(buffer, n) == -1)
00057 {
00058 perror("testserver:main:recieve");
00059 fprintf(stderr,"Would have sent this:\n");
00060 for(i=0;i<n;i++)
00061 {
00062 fputc(buffer[i],stderr);
00063 }
00064 }
00065 if(strncmp(buffer,"CLOSE",5) == 0)
00066 {
00067 ts->close();
00068 break;
00069 }
00070 else if(strncmp(buffer,"HALT",4) == 0)
00071 {
00072 ts->close();
00073 halt = true;
00074 break;
00075 }
00076 else
00077 memset(buffer,'\0',101);
00078 }
00079 }
00080
00081 return 0;
00082 }