M
MuZZy
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
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