Search Unity

Network does not work when build target is WP8

Discussion in 'Editor & General Support' started by Tracy-Ma, Apr 28, 2014.

  1. Tracy-Ma

    Tracy-Ma

    Joined:
    Nov 22, 2012
    Posts:
    21
    I know TcpClient isn't supported in WP8 SDK, so i use the SocketClient from this tutorial http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202858%28v=vs.105%29.aspx

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Net;
    5. using System.Net.Sockets;
    6. using System.Threading;
    7. using System.Text;
    8.  
    9. public class SocketClient
    10. {
    11.     // Cached Socket object that will be used by each call for the lifetime of this class
    12.     Socket _socket = null;
    13.  
    14.     // Signaling object used to notify when an asynchronous operation is completed
    15.     static ManualResetEvent _clientDone = new ManualResetEvent(false);
    16.  
    17.     // Define a timeout in milliseconds for each asynchronous call. If a response is not received within this
    18.     // timeout period, the call is aborted.
    19.     const int TIMEOUT_MILLISECONDS = 5000;
    20.  
    21.     // The maximum size of the data buffer to use with the asynchronous socket methods
    22.     const int MAX_BUFFER_SIZE = 2048;
    23.  
    24.     /// <summary>
    25.     /// Attempt a TCP socket connection to the given host over the given port
    26.     /// </summary>
    27.     /// <param name="hostName">The name of the host</param>
    28.     /// <param name="portNumber">The port number to connect</param>
    29.     /// <returns>A string representing the result of this connection attempt</returns>
    30.     public string Connect(string ip, int port)
    31.     {
    32.         string result = string.Empty;
    33.  
    34.         // Create DnsEndPoint. The hostName and port are passed in to this method.
    35.         //DnsEndPoint hostEntry = new DnsEndPoint(hostName, portNumber);
    36.  
    37.         IPEndPoint ipEP = new IPEndPoint(IPAddress.Parse(ip),port);
    38.  
    39.         // Create a stream-based, TCP socket using the InterNetwork Address Family.
    40.         _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    41.  
    42.         // Create a SocketAsyncEventArgs object to be used in the connection request
    43.         SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
    44.         socketEventArg.RemoteEndPoint = ipEP; // FIXME
    45.  
    46.         // Inline event handler for the Completed event.
    47.         // Note: This event handler was implemented inline in order to make this method self-contained.
    48.         socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
    49.         {
    50.             // Retrieve the result of this request
    51.             result = e.SocketError.ToString();
    52.  
    53.             // Signal that the request is complete, unblocking the UI thread
    54.             _clientDone.Set();
    55.         });
    56.  
    57.         // Sets the state of the event to nonsignaled, causing threads to block
    58.         _clientDone.Reset();
    59.  
    60.         // Make an asynchronous Connect request over the socket
    61.         _socket.ConnectAsync(socketEventArg);
    62.  
    63.         // Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds.
    64.         // If no response comes back within this time then proceed
    65.         _clientDone.WaitOne(TIMEOUT_MILLISECONDS);
    66.  
    67.         return result;
    68.     }
    69.  
    70.     /// <summary>
    71.     /// Send the given data to the server using the established connection
    72.     /// </summary>
    73.     /// <param name="data">The data to send to the server</param>
    74.     /// <returns>The result of the Send request</returns>
    75.     public string Send(string data)
    76.     {
    77.         string response = "Operation Timeout";
    78.  
    79.         // We are re-using the _socket object initialized in the Connect method
    80.         if (_socket != null)
    81.         {
    82.             // Create SocketAsyncEventArgs context object
    83.             SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
    84.  
    85.             // Set properties on context object
    86.             socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;
    87.             socketEventArg.UserToken = null;
    88.  
    89.             // Inline event handler for the Completed event.
    90.             // Note: This event handler was implemented inline in order
    91.             // to make this method self-contained.
    92.             socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
    93.             {
    94.                 response = e.SocketError.ToString();
    95.  
    96.                 // Unblock the UI thread
    97.                 _clientDone.Set();
    98.             });
    99.  
    100.             // Add the data to be sent into the buffer
    101.             byte[] payload = Encoding.UTF8.GetBytes(data);
    102.             socketEventArg.SetBuffer(payload, 0, payload.Length);
    103.  
    104.             // Sets the state of the event to nonsignaled, causing threads to block
    105.             _clientDone.Reset();
    106.  
    107.             // Make an asynchronous Send request over the socket
    108.             _socket.SendAsync(socketEventArg);
    109.  
    110.             // Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds.
    111.             // If no response comes back within this time then proceed
    112.             _clientDone.WaitOne(TIMEOUT_MILLISECONDS);
    113.         }
    114.         else
    115.         {
    116.             response = "Socket is not initialized";
    117.         }
    118.  
    119.         return response;
    120.     }
    121.  
    122.     /// <summary>
    123.     /// Receive data from the server using the established socket connection
    124.     /// </summary>
    125.     /// <returns>The data received from the server</returns>
    126.     public string Receive()
    127.     {
    128.         string response = "Operation Timeout";
    129.  
    130.         // We are receiving over an established socket connection
    131.         if (_socket != null)
    132.         {
    133.             // Create SocketAsyncEventArgs context object
    134.             SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
    135.             socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;
    136.  
    137.             // Setup the buffer to receive the data
    138.             socketEventArg.SetBuffer(new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE);
    139.  
    140.             // Inline event handler for the Completed event.
    141.             // Note: This even handler was implemented inline in order to make
    142.             // this method self-contained.
    143.             socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
    144.             {
    145.                 if (e.SocketError == SocketError.Success)
    146.                 {
    147.                     // Retrieve the data from the buffer
    148.                     response = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);
    149.                     response = response.Trim('\0');
    150.                 }
    151.                 else
    152.                 {
    153.                     response = e.SocketError.ToString();
    154.                 }
    155.  
    156.                 _clientDone.Set();
    157.             });
    158.  
    159.             // Sets the state of the event to nonsignaled, causing threads to block
    160.             _clientDone.Reset();
    161.  
    162.             // Make an asynchronous Receive request over the socket
    163.             _socket.ReceiveAsync(socketEventArg);
    164.  
    165.             // Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds.
    166.             // If no response comes back within this time then proceed
    167.             _clientDone.WaitOne(TIMEOUT_MILLISECONDS);
    168.         }
    169.         else
    170.         {
    171.             response = "Socket is not initialized";
    172.         }
    173.  
    174.         return response;
    175.     }
    176.  
    177.     /// <summary>
    178.     /// Closes the Socket connection and releases all associated resources
    179.     /// </summary>
    180.     public void Close()
    181.     {
    182.         if (_socket != null)
    183.         {
    184.             _socket.Close();
    185.         }
    186.     }
    187.  
    188. }
    During the build post processing, get the error:

    Code (csharp):
    1.  
    2. Error building Player: Exception: Error: method `System.IAsyncResult System.Net.Sockets.Socket::BeginConnect(System.Net.EndPoint,System.AsyncCallback,System.Object)` doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.Void TcpConnectAsync::Send(System.String).
    3. Error: method `System.Void System.Net.Sockets.Socket::EndConnect(System.IAsyncResult)` doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.Void TcpConnectAsync::ConnectCallback(System.IAsyncResult).
    4. Error: type `System.Net.Sockets.SocketFlags` doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.Void TcpConnectAsync::Receive(System.Net.Sockets.Socket).
    5. Error: method `System.IAsyncResult System.Net.Sockets.Socket::BeginReceive(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.AsyncCallback,System.Object)` doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.Void TcpConnectAsync::Receive(System.Net.Sockets.Socket).
    6. Error: method `System.Int32 System.Net.Sockets.Socket::EndReceive(System.IAsyncResult)` doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.Void TcpConnectAsync::ReceiveCallback(System.IAsyncResult).
    7. Error: method `System.Text.Encoding System.Text.Encoding::get_ASCII()` doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.Void TcpConnectAsync::ReceiveCallback(System.IAsyncResult).
    8. Error: type `System.Net.Sockets.SocketFlags` doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.Void TcpConnectAsync::ReceiveCallback(System.IAsyncResult).
    9. Error: method `System.IAsyncResult System.Net.Sockets.Socket::BeginReceive(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.AsyncCallback,System.Object)` doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.Void TcpConnectAsync::ReceiveCallback(System.IAsyncResult).
    10. Error: type `System.Net.Sockets.SocketFlags` doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.Void TcpConnectAsync::SendMessage(System.Net.Sockets.Socket,System.String).
    11. Error: method `System.IAsyncResult System.Net.Sockets.Socket::BeginSend(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.AsyncCallback,System.Object)` doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.Void TcpConnectAsync::SendMessage(System.Net.Sockets.Socket,System.String).
    12. Error: method `System.Int32 System.Net.Sockets.Socket::EndSend(System.IAsyncResult)` doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.Void TcpConnectAsync::SendCallback(System.IAsyncResult).
    13.  
    This is no such TcpConnectAsync::SendCallback func calls in the class, what happens?

    TIA!
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    1. Visit this page:

    http://docs.unity3d.com/Documentation/Manual/wp8-faq.html

    2. Read the explanation for what the message means. Follow the link to:

    http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207211(v=vs.105).aspx

    3. Search on that page for System.Net.Sockets, and click the link to:

    http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.net.sockets(v=vs.105).aspx

    This is the class for Sockets.

    4. Follow the link to the Socket class:

    http://msdn.microsoft.com/en-us/lib...elop/system.net.sockets.socket(v=vs.105).aspx

    This page tells you what is supported on WP8 for the Socket class.

    5. Now search for BeginConnect which is the API that fails for you. You won't find anything, which is the MSDN way of saying that this method is not provided on WP8. So then work out how to re-write your communication code.
     
  3. Tracy-Ma

    Tracy-Ma

    Joined:
    Nov 22, 2012
    Posts:
    21
    There are no BeginConnect, EndConnect API calls in my class, I still dont the error's meaning. :(

    Can you point out which func(s) fail the compiling?
     
  4. Tracy-Ma

    Tracy-Ma

    Joined:
    Nov 22, 2012
    Posts:
    21
    there is a another network script in my project, after removing it, works now. Tank you!