Search Unity

[Universal 8.1/10] No Tcplistener, Networkstream or StreamSocket? What to do?

Discussion in 'Windows' started by shivansps, Dec 15, 2015.

  1. shivansps

    shivansps

    Joined:
    Feb 26, 2014
    Posts:
    60
    So, im trying to adapt a 3rd party IRC library, that works under PC-Windows, to work under universal windows store.
    The main problem is, it uses Tcplistener, so im trying to replace it with a SocketClient, but whiout StreamSocket, that does not seems to be present of the namespace that it should its becoming a problem.

    So, originally the code that connects the library to the server used a Tcplistener, and StreamWriters and readers
    writng a command was this:

    writer.WriteLine( command.ToString() );

    and for read server commands was this:
    reader.ReadLine();

    very simple, so my first attempt was to create a SocketClient like this, just to check it whould work using the exact same code,

    Code (CSharp):
    1. using System.Net.Sockets;
    2. using System.Threading;
    3. using System.Text;
    4. using UnityEngine;
    5.  
    6. public class SocketClient
    7. {
    8.     Socket _socket = null;
    9.     NetworkStream stream;
    10.  
    11.     public void Connect(string hostName, int portNumber)
    12.     {
    13.         _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    14.         try
    15.         {
    16.             _socket.Connect(hostName, portNumber);
    17.             stream = new NetworkStream(_socket);
    18.         }
    19.         catch (SocketException e)
    20.         {
    21.             Debug.Log(e);
    22.         }
    23.     }
    24.  
    25.     public void Close()
    26.     {
    27.         if (_socket != null)
    28.         {
    29.             _socket.Close();
    30.         }
    31.     }
    32.  
    33.     public NetworkStream GetStream()
    34.     {
    35.         return stream;
    36.     }
    37. }
    that worked, perfectely, but now i was depending on NetworkStream, that i cant use either, at this point the logical thing to do whould be use StreamSocket that should do the exact same thing as NetworkStream, but i found to way of using it, strange because that should work on WP and Universal.

    Im a final and desperate attempt i tried to do something that im not even sure it could work, and it was just to send and receive menssages with the socket, so i attempted this.

    Code (CSharp):
    1. using System.Net.Sockets;
    2. using System.Threading;
    3. using System.Text;
    4. using UnityEngine;
    5.  
    6. public class SocketClient
    7. {
    8.     Socket _socket = null;
    9.  
    10.     public void Connect(string hostName, int portNumber)
    11.     {
    12.         _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    13.         try
    14.         {
    15.             _socket.Connect(hostName, portNumber);
    16.         }
    17.         catch (SocketException e)
    18.         {
    19.             Debug.Log(e);
    20.         }
    21.     }
    22.  
    23.     public void Send(string data)
    24.     {
    25.         if (_socket != null)
    26.         {
    27.             Debug.Log(data);
    28.             _socket.Send(Encoding.UTF8.GetBytes(data));
    29.         }
    30.     }
    31.  
    32.     public string Receive()
    33.     {
    34.         string response = string.Empty;
    35.         if (_socket != null)
    36.         {
    37.             byte[] bytes = new byte[512];
    38.             _socket.Receive(bytes);
    39.             response = Encoding.UTF8.GetString(bytes);
    40.         }
    41.         Debug.Log(response);
    42.         return response;
    43.     }
    44.  
    45.     public void Close()
    46.     {
    47.         if (_socket != null)
    48.         {
    49.             _socket.Close();
    50.         }
    51.     }
    52. }
    Just the server does not seem to care if a send it something, again im not sure if this should work at all, it was something like "ok lets try it because i cant think of anything else"...
    I do get the messages from the server, but it just does not responds to anything i send it using this way, maybe because this could only work if the server was another Socket as well?
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
  3. shivansps

    shivansps

    Joined:
    Feb 26, 2014
    Posts:
    60
    thats the thing, it does not allow me to use the Windows namespace somehow, StreamSocket is on Windows.Networking.Sockets.
     
  4. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,920
    You cannot access these classes from Editor, but you can access them from Windows Store Apps, just wrap the code around #if NETFX_CORE... #endif
     
  5. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Code (csharp):
    1. #if NETFX_CORE
    2. using Windows.Networking.Sockets;
    3. #else
    4. using System.Net.Sockets;
    5. #endif
    6.  
    7. public class SomeClass
    8. {
    9.   public void Connect()
    10.   {
    11. #if NETFX_CORE
    12.     // do stuff using Windows.Networking.Sockets;
    13. #else
    14.     // do stuff using System.Net.Sockets;
    15. #endif
    16.   }
    17. }
    Alternatively, if you want code complete to work, then you can make stubs for Windows.Networking.Sockets classes wrapped in #if !NETFX_CORE. The stubs only exist on the editor, and the real thing is used when compiling in Visual Studio. (Doesn't work for async methods.)
     
  6. shivansps

    shivansps

    Joined:
    Feb 26, 2014
    Posts:
    60
    Ok, ive already make it work using streamsocket, thank you.
     
    GarthSmith likes this.