Doc Roms
Doc Roms

Reputation: 3308

Use Sockets with Windows phone 7 - DNS resolve?

I want to connect to a URL with the socket method (http://msdn.microsoft.com/en-us/library/system.net.sockets.socket(v=VS.96).aspx);

But I don't connect.

I always get an Error : HostNotFound, but, I try with "google.com", for Url, and port "80".

I have tried with lots of Urls, (http://google.com, www.google.com, http://www.google.fr) and I don't connect.

I've seen lot of web tutorials and I notice they are not DNS Resolve in MSDN Tutorial, is there a problem?

Any ideas?

Upvotes: 1

Views: 1050

Answers (2)

Ajay
Ajay

Reputation: 6590

Try this.

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)
        {
        }
    }

Upvotes: 0

Pedro Lamas
Pedro Lamas

Reputation: 7233

You are supposed to set the SocketAsyncEventArgs.RemoteEndPoint to an instance of DnsEndPoint; is this what you are currently doing?

You can check here for a sample!

Upvotes: 2

Related Questions