Last year I wrote a home automation application that i could use to control a server from a laptop – the server application used the following resources
using System.Net;
using System.Net.Sockets;
using System.IO;
And then created TCPListener to recieve commands from a local network.
TcpListener server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
while (true)
{
TcpClient client = server.AcceptTcpClient();
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
then processing the stream for correct command and parameters and this worked ok for both the client and server.
I started writing a metro app for the remote control, and because of this, I’ve had to use
using Windows.Networking.Sockets;
using Windows.Networking;
Then created an async void to send the command. (sorry this is very messy)
async void sendTest()
{
StreamSocket socket = new StreamSocket();
await socket.ConnectAsync(new HostName("192.168.88.1"), "13000",SocketProtectionLevel.PlainSocket);
DataWriter writer;
writer = new DataWriter(socket.OutputStream);
// Write first the length of the string as UINT32 value followed up by the string. Writing data to the writer will just store data in memory.
string stringToSend = "Hello";
writer.WriteUInt32(writer.MeasureString(stringToSend));
writer.WriteString(stringToSend);
await writer.StoreAsync();
}
I get an exception (no response in the timeout period)
I know my server is not giving a response, but i’ve tried a number of things such as AcceptSocket, however these still don’t detect that the client has connected.
I’m wondering if Metro has another network library that I can use, or whether someone can suggest an alternative code for my server Listener.
Thanks in advance
Is seems you are looking for a StreamSocketListener. What about this?