i have a problem to establish a tcp connection to a web server with Windows.Networking.Sockets.
Trying to connect always results in a timeout.
I'm testing in wp8 emulator , so i can't say if this problem also happens on a real device.
A connection with System.Net.WebClient works perfectly on the wp8 emulator device, but webclient is no option.
Here's some sample code:
clientSocket = new StreamSocket();
private async void Login()
await clientSocket.ConnectAsync(new HostName("google.de"), "80");
connected = true;
catch (Exception exception)
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
closing = true;
// the Close method is mapped to the C# Dispose
clientSocket = null;
For testing purpose i have written some test code with 'System.Net.Sockets' whitch is available for WP7.1 .
System.Exception: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
With this a connection on a WP 7.1 emulator device works, but also not on the WP8 emulator device.
When i don't set a local endpoint i receive also a timeout here, with setting a local endpoint i get a hostnotfound socket exception .
I have spend now more then a day to figure out what's happening here but i'm running out of ideas :/
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
DnsEndPoint hostEntry = new DnsEndPoint(_serverName, _port);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(SocketEventArg_Completed);
socketEventArg.RemoteEndPoint = hostEntry;
// binding results in a hostnotfound exception, not binding in a timeout exception
IPAddress address = IPAddress.Parse("192.168.99.171");
IPEndPoint ipe = new IPEndPoint(address, 80);
socketEventArg.UserToken = sock;
void SocketEventArg_Completed(object sender, SocketAsyncEventArgs e)
throw new Exception("Invalid operation completed");
private void ProcessConnect(SocketAsyncEventArgs e)
// the error is happening here
if (e.SocketError == SocketError.Success)
// Successfully connected to the server
// Send data to the server
byte buffer = System.Text.Encoding.UTF8.GetBytes(dataIn + "<EOF>");
e.SetBuffer(buffer, 0, buffer.Length);
Socket sock = e.UserToken as Socket;
ResponseReceivedEventArgs args = new ResponseReceivedEventArgs();
args.response = e.SocketError.ToString();
args.isError = true;
I hope someone can help me with this?