all groups > vj# > april 2005 >
You're in the

vj#

group:

Sockets and threads in J#


Sockets and threads in J# Roger Garrett
4/10/2005 11:05:07 AM
vj#:
I have a couple of questions about sockets and threads in J#,

Any feedback is quite welcome.


I create a server socket:

ServerSocket serverSocket = new ServerSocket(m_nPortNumberServerSocket);

and then wait for incoming data with:

Socket ClientIncoming = ServerSocket.accept();

Once the accept() returns I then set up the streams:

DataOutputStream OutputStreamToClient = new
DataOutputStream(ClientIncoming.getOutputStream());
DataInputStream InputStreamFromClient = new
DataInputStream(ClientIncoming.getInputStream());


As it is now it only handles one "connection" to one client at a time.


QUESTION(S) 1:

Q: Is it possible for a ServerSocket to actually handle multiple incoming
connections at
the same time?

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?

QUESTION(S) 2:


This is just about threads in general. I know that if I have a
thread-derived object I can
start up the thread by invoking its start() method. For example:

MyThreadDerivedClass myThread = new MyThreadDerviedClass();

MyThread().start();

And I know that I can stop the thread with:

MyThread().stop();

Q: Do I also need to get rid of it with a

MyThread.destroy():


Q: Is it possible for the thread itself to "get rid of itself"?
If it simply exits (eventually) from the run() method within the thread, does
that automatically cause it to cease its existence?
Should the thread invoke its own staop() method in order to get rid of itself?


- Roger Garrett
Re: Sockets and threads in J# Lars-Inge Tønnessen [VJ# MVP]
4/11/2005 12:00:00 AM

Hi Roger,


[quoted text, click to view]

I hope this will help. I have made sample code you you too. =:o)


[quoted text, click to view]

Correct.


[quoted text, click to view]


Yes, you have to use threads. See the example I have made for you.


[quoted text, click to view]


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]

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]


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]

Yes, this is what you should do.


[quoted text, click to view]

Yepp. (If "cease" means "to come to an end", sorry I'm not English =:o) ).


[quoted text, click to view]

Nope. When It goes throuth the "run" method it has died on it's own.



Best Regards,
Lars-Inge Tønnessen

AddThis Social Bookmark Button