2013-10-15 74 views
1

我想實現WP8的gps跟蹤。但我不知道哪些代碼將允許我訪問套接字以將數據發送到服務器。Windows Phone 8:發送數據到遠程服務器

我已經在網絡形式嘗試過這種

udpClient.Connect(IP, Port) 

' Sends a message to the host to which you have connected. 
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(message) 
udpClient.Send(sendBytes, sendBytes.Length) 
udpClient.Close() 

但對於Windows Phone的。它有點複雜。

請提供給我一個解決方案

+0

你在Windows Phone上嘗試過什麼?另外,您是否查看了文檔(http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202864(v=vs.105).aspx)? – Sascha

回答

2

我給你一個小樣本的你怎麼能帶插座連接發送數據: 但是我建議你,去看看微軟的完整的一個很好的官方文檔。

使用TCP - 套接字

How to create and use a TCP socket client app for Windows Phone

使用UDP - 插座

How to create and use a UDP socket client app for Windows Phone

容易在你的代碼來實現

// Cached Socket object that will be used by each call for the lifetime of this class 
    Socket _socket = null; 
    // Signaling object used to notify when an asynchronous operation is completed 
    static ManualResetEvent _clientDone = new ManualResetEvent(false); 
    // Define a timeout in milliseconds for each asynchronous call. If a response is not received within this 
    // timeout period, the call is aborted. 
    const int TIMEOUT_MILLISECONDS = 5000; 
    // The maximum size of the data buffer to use with the asynchronous socket methods 
    const int MAX_BUFFER_SIZE = 2048; 

    /// <summary> 
    /// SocketClient Constructor 
    /// </summary> 
    public SocketClient() 
    { 
     // The following creates a socket with the following properties: 
     // AddressFamily.InterNetwork - the socket will use the IP version 4 addressing scheme to resolve an address 
     // SocketType.Dgram - a socket that supports datagram (message) packets 
     // PrototcolType.Udp - the User Datagram Protocol (UDP) 
     _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 
    } 


    /// <summary> 
    /// Send the given data to the server using the established connection 
    /// </summary> 
    /// <param name="serverName">The name of the server</param> 
    /// <param name="portNumber">The number of the port over which to send the data</param> 
    /// <param name="data">The data to send to the server</param> 
    /// <returns>The result of the Send request</returns> 
    public string Send(string serverName, int portNumber, string data) 
    { 
     string response = "Operation Timeout"; 
     // We are re-using the _socket object that was initialized in the Connect method 
     if (_socket != null) 
     { 
      // Create SocketAsyncEventArgs context object 
      SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs(); 
      // Set properties on context object 
      socketEventArg.RemoteEndPoint = new DnsEndPoint(serverName, portNumber); 
      // Inline event handler for the Completed event. 
      // Note: This event handler was implemented inline in order to make this method self-contained. 
      socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e) 
      { 
       response = e.SocketError.ToString(); 
       // Unblock the UI thread 
       _clientDone.Set(); 
      }); 
      // Add the data to be sent into the buffer 
      byte[] payload = Encoding.UTF8.GetBytes(data); 
      socketEventArg.SetBuffer(payload, 0, payload.Length); 
      // Sets the state of the event to nonsignaled, causing threads to block 
      _clientDone.Reset(); 
      // Make an asynchronous Send request over the socket 
      _socket.SendToAsync(socketEventArg); 
      // Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds. 
      // If no response comes back within this time then proceed 
      _clientDone.WaitOne(TIMEOUT_MILLISECONDS); 
     } 
     else 
     { 
      response = "Socket is not initialized"; 
     } 
     return response; 
    } 
+0

我有一個小問題:這個套接字是不是在模擬器上初始化? – gayan1991

+0

我不確定我瞭解你的問題。套接字可以完全從仿真器初始化。我將缺少的代碼添加到描述中 –

+0

不,這是我的錯。現在它的作品謝謝 – gayan1991

相關問題