×

Discussion Board

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

    Question Socket Client Problem

    Hi everyone,

    I have a problem for my new application on Windows Phone 7 & 8. I have a server on C# which create a socket listener and I want my Windows Phone app and my Windows 8 app connect to it to transfert some data between them.

    My connection with my W8 app works, but the WP8 app throw a System.Net.Sockets.SocketError.ConnectionRefused.

    I want to try on Windows Phone 7 but there are "no connection" so I have to try my app on my Lumia 800, but I think it will be the same error.

    This is my code for the server C#:

    Code:
     private void StartListener()
            {
                _listenerThread = new Thread(RunListener);
                _listenerThread.Start();
            }
    
            private void RunListener()
            {
                _listener = new TcpListener(IPAddress.Any, 12350);
                _listener.Start();
    
                while(true)
                {
                    try
                    {
                        TcpClient client = _listener.AcceptTcpClient();
                    }
                    catch (Exception) { }
                }
            }
    Now the Windows Phone app:
    Code:
               string result = string.Empty;
                IPAddress address = string.Empty;
    
                address = IPAddress.Parse("127.0.0.1");
    
                IPEndPoint endPoint = new IPEndPoint(address, 12350);
    
                // Create a stream-based, TCP socket using the InterNetwork Address Family. 
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    
                // Create a SocketAsyncEventArgs object to be used in the connection request
                SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
                socketEventArg.RemoteEndPoint = endPoint;
    
                // Inline event handler for the Completed event.
                socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
                {
                    // Retrieve the result of this request
                    result = e.SocketError.ToString(); 
    
                   // ------------------------------------------------------                            result equal to ConnectionRefused                      --------------------------------------------
    
                    // Signal that the request is complete, unblocking the UI thread
                    _clientDone.Set();
                });
    
                // Sets the state of the event to nonsignaled, causing threads to block
                _clientDone.Reset();
    
                // Make an asynchronous Connect request over the socket
                _socket.ConnectAsync(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);
    
                return result;
            }
    An idea of what the problem is?

    Thanks in advance.

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,734

    Re: Socket Client Problem

    127.0.0.1 is the localhost address, it means the same device which issues the connection request. The Windows 8 app can connect to the server, because it is running on the same Windows 8 machine. However even the emulator runs on a separate virtualized machine with a separate network stack. And if you try with a real phone, it is going to be more obvious that it is a completely separate machine.
    So use the real IP address of your Windows 8 host. ipconfig is a command which can tell you your IP address(es), but it is probably available via the GUI too, just I am not too familiar with Windows 8 yet.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Re: Socket Client Problem

    Hi,

    It works with my IP adress !

    My Windows Phone emulator runs on a separate virtualized machine with a separate network stack so I understand, but my Windows 8 app was also launched in emulator like the Windows Phone emulator but it worked with 127.0.0.1, strange.

    Anyway, thank you very much!

Similar Threads

  1. Simple Socket(Client) Problem
    By asadi.alireza in forum Symbian Networking & Messaging (Closed)
    Replies: 15
    Last Post: 2011-04-04, 12:20
  2. Socket Server Client Problem
    By nyerunkar in forum Mobile Java Networking & Messaging & Security
    Replies: 4
    Last Post: 2010-11-23, 16:40
  3. Problem in Sending data using Symbian Socket client to the PC based socket server
    By pankaj.dubey in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2008-02-26, 15:09
  4. Problem with Java Client Socket with C# Server
    By scottleaw in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2007-09-25, 06:56
  5. Replies: 1
    Last Post: 2007-09-25, 03: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
  •  
×