2012-04-05 62 views
1

我想使用套接字方法(http://msdn.microsoft.com/en-us/library/system.net.sockets.socket(v=VS.96).aspx)連接到URL;在Windows Phone 7中使用套接字 - DNS解析?

但我沒有連接。

我總是得到一個錯誤:HostNotFound,但我嘗試使用「google.com」,Url和端口「80」。

我嘗試了很多Url(http://google.com,www.google.com,http://www.google.fr),但我沒有連接。

我見過很多網頁教程,我注意到他們不是DNS解析MSDN教程,有沒有問題?

任何想法?

回答

0

試試這個。

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
socket.SetNetworkRequirement(NetworkSelectionCharacteristics.NonCellular); 
string serverName = "www.google.com"; 
int portNumber = 80; 
DnsEndPoint hostEntry = new DnsEndPoint(serverName, portNumber); 
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs(); 
socketEventArg.RemoteEndPoint = hostEntry; 
socketEventArg.UserToken = socket; 
socketEventArg.Completed += socketEventArg_Completed; 
// Make an asynchronous Connect request over the socket. 
socket.ConnectAsync(socketEventArg); 

    void socketEventArg_Completed(object sender, SocketAsyncEventArgs e) 
    { 
     Socket socket = e.UserToken as Socket; 
     if (e.SocketError == SocketError.Success) 
     { 
     } 
    }