dcsimg
a multithreaded server
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   A_D
Posted On:   Friday, March 24, 2006 01:03 PM

hello i've just created a server that accepts several clients inside a while loop but i want to accept only a determinated number of clients that ipass in the command-line arguments too for example: java ServerEx -max_num_clients 3 -port 1982 how can i make to the server don't allow a fourth client for this example? this is the code many thanks import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.net.Socket; import java.net.ServerSocket; import java.net.SocketException; import java.util.Link   More>>

hello i've just created a server that accepts several clients inside a while loop but i want to accept only a determinated number of clients that ipass in the command-line arguments too for example:

java ServerEx -max_num_clients 3 -port 1982


how can i make to the server don't allow a fourth client for this example?

this is the code many thanks

			

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.SocketException;
import java.util.LinkedList;
import java.util.ListIterator;

public class ServerEx
{

private LinkedList reg_thread = null;
private ServerSocket socketServer = null;
private Socket socketClient = null;

/*_____________________________________________________________________________
*
*
* M A I N
*
*___________________________________________________________________________*/

public static void main (String argv [ ]) throws IOException
{
int Port = 0,
count = 0,
maxThreads = 0,
i = 0,
j = 0;
ServerEx server = null;
SocketThread conn = null;
boolean listening_connection = true;

server = new ServerEx ();

/*---command line arguments---*/

if( argv.length != 4 )
{
System.err.println( "usage: java ServerEx -port <#> " +
"-max_num_clients <#>" );
System.exit ( 0 );
}
else
for ( i = 0; i < argv.length; i++ )
{
if ( argv [ i ].compareTo ( "-port" ) == 0 )
{
try
{
Port = Integer.parseInt ( argv [ i+1 ] );
}
catch( NumberFormatException e)
{
System.err.println ("
The port must be a number: "+
e.toString ());
System.err.println( "usage: java ServerEx -port <#> " +
"-max_num_clients <#>" );

System.exit ( 0 );
}
}
else if ( argv [ i ].compareTo ( "-max_num_clients" ) == 0 )
{
try
{
maxThreads = Integer.parseInt ( argv [ i+1 ] );
}
catch ( NumberFormatException e )
{
System.err.println ( "
This field must be in number: "+
e.toString ());

System.err.println( "usage: java ServerEx -port <#> " +
"-max_num_clients <#>" );
System.exit ( 0 );
}
}
}

for ( i = 0; i < 4; i += 2)
{
if ( argv[ i ].compareTo ( "-port" ) != 0 &&
argv[ i ].compareTo ( "-max_num_clients" ) != 0 )
{
System.err.println ("

invalid option: '" + argv [ i ] + "'
" +
"usage: java ServerEx -port <# > " +
"-max_num_clients <#> ");
System.exit ( 0 );
}
}
for (i = 2; i >= 0; i-= 2)
{
for (j = 0; j <= i-1; j += 2)
if ( argv [ i ].compareTo (argv [ j ]) == 0)
{
System.err.println ("

duplicated option: '" +
argv [ i ]+"'
" +
"usage: java ServerEx -port <# > " +
"-max_num_clients <#> ");
System.exit ( 0 );
}
}

if ( Port < 1 || Port > 65535 )
{

System.err.println( "
port must be between 1 and 65535

" +
"usage: java ServerEx -port <#> " +
"-max_num_clients <#> ");
System.exit ( 0 );

}
else if (maxThreads < 0 )
{
System.err.println ("
max connections must be in positive

" +
"usage: java ServerEx -port <#> " +
"-max_num_clients <#> ");
System.exit ( 0 );
}

try
{
server.socketServer = new ServerSocket( Port );
}
catch (IOException e)

{
System.err.println("could not listen on that port " + e.toString());
System.exit (0);
}


System.out.println("Server created. Waiting client connections...");
System.out.println("only " + maxThreads + " connections will be allowed " +
"at the same time");

server.new_list();





//SHOULD I CHANGE THIS FOR A "FOR LOOP???"
while ( listening_connection )
{
try
{
server.socketClient = server.socketServer.accept();

conn = new SocketThread (server.socketClient,
count, server);
conn.thisReference( conn );

conn.start();

System.out.println("

client: " + count + " accepted " );
count++;
server.reg_thread.addLast ( conn );




/*-----THIS DONT WORK----------------*/
if ( server.reg_thread.size() > maxThreads )
{
System.err.println ("server is busy");
listening_connection = false;
}

}
catch (SocketException e)
{
System.err.println ("the server has been closed" );
listening_connection = false;
}
}
}/*____________________________ END MAIN __________________________________*/



/*_________________________________Shutdown__________________________________*/
public void Shutdown (SocketThread this_socket)
{
ListIterator ite = reg_thread.listIterator(0);
SocketThread conn = null;

while ( ite.hasNext ())
{
conn = (SocketThread) ite.next ();


if( conn != this_socket )
{
conn.Broadcast();
conn.Stop();
}

}

}/*_____________________________end Shutdown_______________________________*/


/*_________________________________new_list__________________________________*/
public void new_list()
{
reg_thread = new LinkedList();
}/*____________________________end new_list________________________________*/


/*________________________________Interrupt__________________________________*/
public void Interrupt() throws IOException
{
socketServer.close();

}/*___________________________end Interrupt________________________________*/

}/*_________________________ END ServerEx class______________________________*/

/*===========================================================================*/


   <<Less

Re: a multithreaded server

Posted By:   Robert_Lybarger  
Posted On:   Thursday, April 6, 2006 07:55 PM

I'm not clear about a few things (notably the 'threaded' nature of what you're doing, as 'SocketThread' isn't provided). That said, I would probably have a "SocketThreadManager" class (which runs in its own thread?) that has a way of determining how many SocketThreads are alive and doing useful work, and also provides a method that returns this number. When you "accept" from your ServerSocket object, have it poll this manager so it can determine whether to pass the client's connection to the manager or otherwise immediately tell the client to go away. Granted that leaves some implementation details up in the air for how the manager does its job correctly; hopefully this line of thought gives you a different direction to pursue.
About | Sitemap | Contact