Hi Roger,
[quoted text, click to view] > Any feedback is quite welcome.
I hope this will help. I have made sample code you you too. =:o)
[quoted text, click to view] > As it is now it only handles one "connection" to one client at a time.
Correct.
[quoted text, click to view] > Q: Is it possible for a ServerSocket to actually handle multiple incoming
> connections at
> the same time?
Yes, you have to use threads. See the example I have made for you.
[quoted text, click to view] > Q: Could I, once it returns from the accept(), spawn off a thread for the
> particular client
> that just connected, setting up its own Input and Output Streams, and then
> immediately
> go back waiting for another incoming Client?
Yes, this is how you should do synchronious sockets with multiple clients.
The Server:
****************************************************************
/**
* Summary description for Class1.
*/
public class Class1
{
public Class1()
{
System.Collections.ArrayList _socketList = new
System.Collections.ArrayList();
System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(
System.Net.Sockets.AddressFamily.InterNetwork,
System.Net.Sockets.SocketType.Stream,
System.Net.Sockets.ProtocolType.Tcp);
System.Net.IPEndPoint endpoint = new System.Net.IPEndPoint(
System.Net.IPAddress.Any, 2002 );
socket.Bind( endpoint );
socket.Listen( 10 ); // max 10 in a waitning queue
while ( 1 > 0 )
{
yourThread th = new yourThread();
// Receives a "new" socket to the client.
th.socket = socket.Accept();
th._myThread = new System.Threading.Thread( new
System.Threading.ThreadStart( th.thread_here ) );
_socketList.Add( th );
th._myThread.Start();
}
// Do stuff with all the threads. Eg. join them etc.
// They are all stored in the _socketList array list.
}
/** @attribute System.STAThread() */
public static void main(String[] args)
{
new Class1();
}
}
class yourThread
{
public System.Threading.Thread _myThread = null;
public System.Net.Sockets.Socket socket = null;
public void thread_here()
{
if ( this.socket != null )
{
ubyte[] dataReceived = new ubyte[200];
socket.Receive( dataReceived, 0, 200,
System.Net.Sockets.SocketFlags.None );
System.Console.WriteLine( "Thread-> " +
System.Text.Encoding.get_ASCII().GetString( dataReceived ) );
ubyte[] out = System.Text.Encoding.get_ASCII().GetBytes("Hello Back");
// Just do a sleep so you can see it is actually doing more than one
client connection at the same time.
System.Threading.Thread.Sleep( 10000 );
socket.Send( out );
socket.Close();
System.Console.WriteLine( "Thread done" );
}
}
}
***********************************************************
The client:
************************************************************
/**
* Summary description for Class1.
*/
public class Class1
{
public Class1()
{
System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(
System.Net.Sockets.AddressFamily.InterNetwork,
System.Net.Sockets.SocketType.Stream,
System.Net.Sockets.ProtocolType.Tcp );
System.Net.IPEndPoint endpint = new System.Net.IPEndPoint(
System.Net.IPAddress.Loopback, 2002 );
socket.Connect( endpint );
ubyte[]out = new ubyte[100];
out = System.Text.Encoding.get_ASCII().GetBytes( "hello" );
socket.Send( out );
ubyte[] in = new ubyte[2000];
socket.Receive(in);
System.Console.WriteLine( "->" +
System.Text.Encoding.get_ASCII().GetString( in ) );
socket.Close();
}
/** @attribute System.STAThread() */
public static void main(String[] args)
{
new Class1();
}
}
****************************************************************
[quoted text, click to view] > And I know that I can stop the thread with:
>
> MyThread().stop();
You should absolutely _NOT_ stop a thread like that. It must die on its own.
You have no warranty that the thread is killed with that statement. One (of
many) solutions is to make a shared variable the threads are listening on.
If the variable has a specific value they know they should die on their own.
[quoted text, click to view] > Q: Do I also need to get rid of it with a
>
> MyThread.destroy():
Please don't. Let the thread die on its own by a shared variable or "send" a
shutdown "command" to the thread or a message to the socket the thread is
listening on.
[quoted text, click to view] > Q: Is it possible for the thread itself to "get rid of itself"?
Yes, this is what you should do.
[quoted text, click to view] > If it simply exits (eventually) from the run() method within the thread,
> does
> that automatically cause it to cease its existence?
Yepp. (If "cease" means "to come to an end", sorry I'm not English =:o) ).
[quoted text, click to view] > Should the thread invoke its own staop() method in order to get rid of
> itself?
Nope. When It goes throuth the "run" method it has died on it's own.
Best Regards,
Lars-Inge Tønnessen