CRL88
CRL88

Reputation: 125

WIFI Direct IP Address issue

I have checked and found that the problem is the ip address being assigned to the connectionEndpointPair is carrying the IP of the wi-fi direct network adapter and i don't know how to open the port on that specific ip, the ip is different from when i ping it from my pc the windows is listening on port 5009 and connection established when i use the wi-fi ip but when i use the wi-fi direct ip addresses i'm having an issue

The wi-fi direct connection between the device and the windows 8.1 application is ok, I then am awaiting for my sockets to connect but it does not happen what could be the issue ?

I get error on Visual Studio:

No connection could be made because the target machine actively refused it. (Exception from HRESULT: 0x8007274D)

On the Windows side i am using this code:

String deviceSelector = WiFiDirectDevice.GetDeviceSelector();

        DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(deviceSelector);

        if(deviceCollection.Count > 0)
        {
            try
            {
                wfdDevice = await WiFiDirectDevice.FromIdAsync(deviceCollection[0].Id);

                wfdDevice.ConnectionStatusChanged +=ConnectionStatusChangedNotficationHandler;



                var endpointPairs = wfdDevice.GetConnectionEndpointPairs();
                EndpointPair connectionEndpointPair = endpointPairs[0];

                try
                {
                    connectionEndpointPair.RemoteServiceName = "5009";


                    StreamSocket socket = new StreamSocket();
                    await socket.ConnectAsync(connectionEndpointPair);

                    string s = "hello";
                }catch (Exception err)
                {
                    string s = err.Message;
                    s = err.StackTrace;
                }
            }
            catch (Exception err)
            {
                string error = err.Message;

            }

On the android side i am using this code:

               private void initiateClientSocket(String hostAddress) {


        int timeout = 10000;
        int port = 5009;

        InetSocketAddress socketAddress 
          = new InetSocketAddress(hostAddress, port);

        try {
          Socket socket = new Socket();
          socket.bind(null);
          socket.connect(socketAddress, timeout);
        } catch (IOException e) {
          Log.e(TAG, "IO Exception.", e);
        }

        // TODO Start Receiving Messages
      }

From the android side I am getting:

java.net.UnknownHostException: Host is unresolved: my ip

Can any please help

Thanks :)

Upvotes: 1

Views: 3716

Answers (2)

Roland Bär
Roland Bär

Reputation: 1730

Even if you have a direct WIFI Connection between the Android phone and the windows computer, you need a server and a client for a tcp connection.

I don't know the purpose of your application but I assume that the windows computer is the better choice for the server. So instead connecting to the socket, on the computer you should open a listener for the port.

The server class that is related to the chosen StreamSocket class is the StreamSocketListener. You can find the Documentation here: http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.sockets.streamsocketlistener.aspx

There is also a paragraph in this documentation about the typical order of operations:

  • Create the StreamSocketListener.
  • Use the Control property to retrieve a StreamSocketListenerControl object and set the socket quality of service required.
  • Assign the ConnectionReceived event to an event handler.
  • Call the BindServiceNameAsync or BindEndpointAsync method to bind to a local TCP port or service name.
  • When a connection is received, use the StreamSocketListenerConnectionReceivedEventArgs object to retrieve the Socket property with the StreamSocket object created.
  • Use the StreamSocket object to send and receive data.
  • Call the Close method to stop listening for and accepting incoming network connections and release all unmanaged resources associated with the StreamSocketListener object. Any StreamSocket objects created when a connection is received are unaffected and can continue to be used as needed.

I haven't worked with this special class, but the basics of TCP are always the same...

Upvotes: 4

Daniel
Daniel

Reputation: 619

I don't quite understand your code on Window, but guess that there is something wrong with the following code

connectionEndpointPair.RemoteServiceName = "5009";

It seems that "RemoteServiceName" has nothing to do with the port number 5009. Please check other fields in the struct/object "connectionEndpointPair" and make proper settings.

Upvotes: 2

Related Questions