Re: Remote control with TcpListener

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Laxmikant Rashinkar (LK-at-televital-dot-com)
Date: 01/20/05


Date: Wed, 19 Jan 2005 17:37:47 -0800

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(bytes, 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_LIST
2 = GET_DRUMS_LIST
3 = GET_GUITARS_LIST
4 = GET_FLUTES_LIST
5 = GET_SAXPHONES_LIST

I would first declare a delegate as follows: delegate int
ClientCmdHandler();
Then I would declare an array of event handlers to hold my 5 commands, as
follows:

ClientCmdHandler[] executeClientCmd = new ClientCmdHandler[5];
executeClientCmd[0] = new ClientCmdHandler(getRecordersList);
executeClientCmd[1] = new ClientCmdHandler(getDrumsList);
executeClientCmd[2] = new ClientCmdHandler(getGuitarsList);
executeClientCmd[3] = new ClientCmdHandler(getFlutesList);
executeClientCmd[4] = new ClientCmdHandler(getSaxphonesList);

then I would declare the following methods as follows

public int (getRecordersList) { 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 (getSaxphonesList) { do something useful here }

Once I have retrieved the cmd from the client packet, I would do as follows

if( (cmd > 0) && (cmd < 6) )
{
       executeClientCmd[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" <leyandrew@yahoo.com> wrote in message
news:I4mdnXjQ-5s-R3PcRVn-qA@rcn.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 ListenerThreadFunction()
> {
> RemServer = new TcpListener(IPAddress.Parse("127.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.AcceptTcpClient();
> // Get the connection stream
> NetworkStream stream = client.GetStream();
> int i;
> // Loop to get all the data client sent
> while((i = stream.Read(bytes, 0, bytes.Length))!=0)
> {
> // Convert dada to string
> data = System.Text.Encoding.ASCII.GetString(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



Relevant Pages

  • Re: What doesnt lend itself to OO?
    ... >> proxy and instructs the server to constuct the real object. ... rather than client code. ... If 'clock' is instantiated in the server, ... > for the server interface at the OOA level. ...
    (comp.object)
  • This is going straight to the pool room
    ... or not the client has privilege to do what they're trying to do, ... The server environment is this: ... 3GL User action Routines that Tier3 will execute on your behalf during the ... Routine Name: USER_INIT ...
    (comp.os.vms)
  • Re: bug with sending null elements of DataSet[] ?
    ... On client side, call webmethod like this: ... So, on server side, I expected to see a DataSet array with one element, where that element contains null. ... This should have been a big hint to the client, but apparently, it was not. ... public int ID ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • [Full-Disclosure] R: Full-Disclosure Digest, Vol 3, Issue 42
    ... Full-Disclosure Digest, Vol 3, Issue 42 ... SD Server 4.0.70 Directory Traversal Bug ... Arkeia Network Backup Client Remote Access ...
    (Full-Disclosure)
  • Re: What doesnt lend itself to OO?
    ... > rather than client code. ... no way to do that without also touching the object with clock semantics ... will not encapsulate both clock semantics and network semantics. ... The server can do whatever it wants ...
    (comp.object)