Hi,
Your server code blocks on the call to read 65535 bytes of data.
At some point in time, the client sends the server a request packet,
consisting
of 100 bytes (for example). On the server side, the call while((i =
stream.Read(byt es, 0, bytes.Length))! =0)
will then return (unblock) and i will equal 100. So that solves your
blocking problem.
Now, about parsing the command that the client has sent you. I would use
integers instead
of strings to represent the commands. Let us assume the client can send us 5
possible commands as follows:
1 = GET_RECORDERS_L IST
2 = GET_DRUMS_LIST
3 = GET_GUITARS_LIS T
4 = GET_FLUTES_LIST
5 = GET_SAXPHONES_L IST
I would first declare a delegate as follows: delegate int
ClientCmdHandle r();
Then I would declare an array of event handlers to hold my 5 commands, as
follows:
ClientCmdHandle r[] executeClientCm d = new ClientCmdHandle r[5];
executeClientCm d[0] = new ClientCmdHandle r(getRecordersL ist);
executeClientCm d[1] = new ClientCmdHandle r(getDrumsList) ;
executeClientCm d[2] = new ClientCmdHandle r(getGuitarsLis t);
executeClientCm d[3] = new ClientCmdHandle r(getFlutesList );
executeClientCm d[4] = new ClientCmdHandle r(getSaxphonesL ist);
then I would declare the following methods as follows
public int (getRecordersLi st) { do something useful here }
public int (getDrumsList) { do something useful here }
public int (getGuitarsList ) { do something useful here }
public int (getFlutesList) { do something useful here }
public int (getSaxphonesLi st) { do something useful here }
Once I have retrieved the cmd from the client packet, I would do as follows
if( (cmd > 0) && (cmd < 6) )
{
executeClientCm d[cmd - 1](); // this will call the corresponding
commands method
}
hope this helps
LK
even though you are blocking on the buffer size of 65536 bytes, the call
will return when fewer bytes are read
"MuZZy" <le*******@yaho o.com> wrote in message
news:I4******** ************@rc n.net...
Hi,
The app i develop supose to have a remote control/reporting feature,
so that if a client connects to it, he can obtain information about
application state and to send some commands.
I started with using TcpListener in a separate thread of the app.
Something like this:
// =============== ============= CODE BEGIN =============== ========
public void ListenerThreadF unction()
{
RemServer = new TcpListener(IPA ddress.Parse("1 27.0.0.1"),9999 );
RemServer.Start ();
// Buffer for reading data
Byte[] bytes = new Byte[65535];
String data = null;
while(true)
{
// Get the client connection
TcpClient client = RemServer.Accep tTcpClient();
// Get the connection stream
NetworkStream stream = client.GetStrea m();
int i;
// Loop to get all the data client sent
while((i = stream.Read(byt es, 0, bytes.Length))! =0)
{
// Convert dada to string
data = System.Text.Enc oding.ASCII.Get String(bytes, 0, i);
data = data.ToUpper(). Trim();
// Now check what command arrived:
// Client asks to provide list of recorders
if (data == "GET_RECORDERS_ LIST")
{
// Form return string in format
}
}
client.Close();
}
}
// =============== ====== CODE END =============== =========
It's partially taken from a MSDN example.
The idea how i see it should be this: client connects, sends a request,
listener responds, then client sends another request, server responds, and
so on.
The first problem i see is that server will be blocked waiting for 65535
bytes of data from client in stream.Read() Second, i'm not sure how i should correctly design the loops in the
function.
Any ideas would be appreciated!
Thank you,
Andrey