Can't use StreamSocket to connect to a TcpList

2020-02-15 08:57发布

问题:

I have an app on mobile devices who connects to my server app in desktops I use TcpListener on my desktop and TcpClient on mobiles. I tried to add support for Windows 8 but it seems Microsoft removed it and now we should use StreamSocket as TcpClient and StreamSocketListener as TcpListener. I tried to change my code but it is not even connecting to my TcpListener. (Note that I used asynchronous on desktops as well).

TcpListener on my desktop:

server = new TcpListener(Constants.DEFAULT_PORT);

server.Start();

server.BeginAcceptSocket(new AsyncCallback(OnAccept), null);

StreamSocket on my Windows 8:

var client = new StreamSocket();
await client.ConnectAsync(new EndpointPair(new HostName("localhost"), "7800", new HostName("localhost"), "7800"));

It passes the ConnectAsync line without being connected because my OnAccept on the desktop is not called.

So I don't know whats wrong here.

回答1:

By default, Windows Runtime (store) apps cannot use networking to connect to localhost. This web page talks about it: http://msdn.microsoft.com/en-us/library/windows/apps/Hh780593.aspx

I never was able to get localhost communication working using the above instructions, and opted instead to set up a listener on my local network instead. Note that even local network access is turned off by default - you have to enable it via the application manifest for the Windows Runtime app.