Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi fellow programmers,

I am new to UWP so would like to thank you in advance for helping me on this issue.

I am following this MSDN article of "How to create and use a TCP socket client app for Windows Phone 8" https://msdn.microsoft.com/en-us/library/windows/apps/hh202858(v=vs.105).aspx[^]
in VS 2015 to test how the
C#
System.Net.Sockets.Socket
Class works in UWP app.

I basically ported the whole project into an UWP application and it works as expected with the built-in Simple TCP/IP Services on my computer.

However, as soon as I changed the IP and port number to an existing TCP socket server on my LAN (IP: 192.168.200.254, Port: 2223) I get AccessDenied Exception on connecting.

Here is the code for Connect():

C#
public string Connect(string hostName, int portNumber)
       {
           string result = string.Empty;

           // Create DnsEndPoint. The hostName and port are passed in to this method.
           DnsEndPoint hostEntry = new DnsEndPoint(hostName, portNumber);

           // 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 =  hostEntry;

           // Inline event handler for the Completed event.
           // Note: This event handler was implemented inline in order to make this method self-contained.
           socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate (object s, SocketAsyncEventArgs e)
           {
               // Retrieve the result of this request
               result = e.SocketError.ToString();

               // 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;
       }


I tried different non-existence ip and ports, always get the same exception. So looks like a client side issue. The call has never got to the server.

Do I need to do any additional settings on my client side code?

There is another issue when porting this code to UWP:
The Socket.Close() method is no longer there!?
The closest is ShutDown().

Is this ShutDown() supposed to be the replacement?

Kind regards.
Posted
Updated 2-Mar-16 1:16am
Comments
Necromesa 2-Mar-16 5:19am    
Have you found a solution? I'm in the same exact situation here:
The old client connects correctly whereas the ported one (UWP) doesn't.

Also I think the Close() replacement is Dispose().

Thanks.

I guess the normal things apply. Can you

- ping from the client to the server machine ?
- ping from the server to the client machine ?
(If either is 'no' you may have a routing issue - are your machines on the same LAN segment - ie 192.168.200.x and 192.168.200.y (you've already given one one these, use ipconfig on a commandline to check the client))
- Can you do a trace route (tracert) from the client to the server ? - if not you may need to add a route on your client and or get your network techs to check the network hardware (hubs/switches etc) inbetween the machines

If you can get ping working, you're most of the way there (assuming no network nazis/mgmt havnt blocked it for some reason)...

the next thing I'd be checking - do you need to enable software firewall access on either end ? port 2223 is 'non standard', so you may have a local software firewall issue or a 'policy' preventing you - your system admins/network admins would know this and (hopefully) be able to advise you
 
Share this answer
 
Comments
swDayDayUp 25-Nov-15 20:25pm    
Hi Garth,
Thanks for your reply.

Actually I think this is more of a software issue rather than an network related issue.

Because the same socket components works in .net framework 4.5.

I am trying to port an existing .net frmework 4.5 app to UMP app.
the existing app works fine.
I my case the solution was to add the Private Networks (Client and Server) Capability in my app's Package.appxmanifest.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900