Reputation: 125
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
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:
I haven't worked with this special class, but the basics of TCP are always the same...
Upvotes: 4
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