locked
About Stream Sockets

    Question

  • Hi. I just build a StreamSocket app to run in my Windows 8.1device (client) that has to connect with a TCP Server in a Remote PC. I set the app mannifest to support WAN connections (client and server) and Local connections. This is my Client code.

    private void Connect() { // 1 - Try to connect to the server

    Task<TCPConnectionArgs> connectionTask = Connections.ConnectToServer(myViewModel.ServerHost,myViewModel.ServerPort); connectionTask.Wait(); //If I could not connect, just continu retrying until I can connect while(connectionTask.Result.Result == 0) { connectionTask.Start(); connectionTask.Wait(); } //If I could Connect

    ..... } public static async Task<TCPConnectionArgs> ConnectToServer(HostName serverHost, string serverPort) { TCPConnectionArgs result = new TCPConnectionArgs(); //Try to connect to server on TCP StreamSocket tempSock = new StreamSocket(); if (serverHost == null || string.IsNullOrEmpty(serverPort)) { result.Result = 0; result.ConnectionError = "Server Host or Server Port is null"; } else { try { await tempSock.ConnectAsync(serverHost, serverPort); result.ConnectionSocket = tempSock; result.Result = 1; } catch (Exception ex) { if (SocketError.GetStatus(ex.HResult) == SocketErrorStatus.Unknown) { throw; } //If i have got an error, just close the socket tempSock.Dispose(); result.Result = 0; result.ConnectionError = ex.Message; } } return result; }


    Is it sufficient to do the connection? Do I need to add any settings?


    • Edited by s.flores Saturday, July 26, 2014 11:03 PM
    Saturday, July 26, 2014 10:57 PM

Answers

  • Hi s.flores,

    Have you got your code works? I did not test your code, by I did not find any mistake by my eyes. If there are some errors thrown while debugging, please tell me.

    I would also suggest you to go through the documentation: How to connect with a stream socket

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, July 28, 2014 7:21 AM
    Moderator

All replies

  • Hi s.flores,

    Have you got your code works? I did not test your code, by I did not find any mistake by my eyes. If there are some errors thrown while debugging, please tell me.

    I would also suggest you to go through the documentation: How to connect with a stream socket

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, July 28, 2014 7:21 AM
    Moderator
  • Hi. Thanks for reply.

    I have tested the code in a lan and it works properly. I will test it in a WAN and then, i will tell you about the results.

    Friday, August 1, 2014 4:58 PM