×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    1

    Can't get a tcp socket connection established on wp8 emulator

    Hi,

    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:
    Code:
     public MainPage()
            {
                InitializeComponent();
                clientSocket = new StreamSocket();
                Login();
            }
    
            private async void Login()
            {
                if (connected)
                {
                    return;
                }
    
                try
                {
                    await clientSocket.ConnectAsync(new HostName("google.de"), "80");
                    connected = true;
    
                }
                catch (Exception exception)
                {
                    if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
                    {
                        throw;
                    }
                    closing = true;
                    // the Close method is mapped to the C# Dispose
                    clientSocket.Dispose();
                    clientSocket = null;
                }
            }
    Error:
    Code:
    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.
    For testing purpose i have written some test code with 'System.Net.Sockets' whitch is available for WP7.1 .
    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 .

    Code:
    void connect()
    {
                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);
                sock.Bind(ipe);
    ////////////////////////////////
                
    
                socketEventArg.UserToken = sock;
    
                    sock.ConnectAsync(socketEventArg);
    
    }
    
            void SocketEventArg_Completed(object sender, SocketAsyncEventArgs e)
            {
    
                switch (e.LastOperation)
                {
                    case SocketAsyncOperation.Connect:
                        ProcessConnect(e);
                        break;
                    case SocketAsyncOperation.Receive:
                        ProcessReceive(e);
                        break;
                    case SocketAsyncOperation.Send:
                        ProcessSend(e);
                        break;
                    default:
                        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;
                    sock.SendAsync(e);
                }
                else
                {
                    ResponseReceivedEventArgs args = new ResponseReceivedEventArgs();
                    args.response = e.SocketError.ToString();
                    args.isError = true;
                    OnResponseReceived(args);
    
                }
            }
    I have spend now more then a day to figure out what's happening here but i'm running out of ideas :/

    I hope someone can help me with this?

  2. #2
    Registered User
    Join Date
    Jul 2011
    Posts
    20

    Re: Can't get a tcp socket connection established on wp8 emulator

    When you start WP8 emulator, the emulator has no internet connection during first one or two minutes. Please check if you have internet initialized inside the emulator using IE icon.

Similar Threads

  1. Replies: 4
    Last Post: 2010-10-09, 10:22
  2. Replies: 3
    Last Post: 2009-11-10, 08:33
  3. Pass Audio after SIP connection is established..
    By Abhishek_karmakar in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2008-11-14, 12:02
  4. How to display a dialog until the connection got established?
    By vinsofts in forum Symbian User Interface
    Replies: 0
    Last Post: 2005-08-31, 08:22
  5. How to determine connection established?
    By bsekerci in forum Symbian Networking & Messaging (Closed)
    Replies: 2
    Last Post: 2004-10-22, 10:42

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •