简体   繁体   中英

Metro app using socketstream to write to Win32 TCPListener in C#

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?

private StreamSocketListener listener;

private async void Test()
{
    listener = new StreamSocketListener();
    listener.ConnectionReceived += OnConnection;
    await listener.BindServiceNameAsync(port.ToString());
}

private async void OnConnection(StreamSocketListener sender,
    StreamSocketListenerConnectionReceivedEventArgs args)
{
    // Streams for reading a writing.
    DataReader reader = new DataReader(listener.InputStream);
    DataWriter writer = new DataWriter(socket.OutputStream);

    writer.WriteString("Hello");
    await writer.StoreAsync();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM