Problem with socket connection by using computer name? - android

I want to connect my cell-phone to PC by using socket connection.
But it's only successful when I use IP Address. I try to use Computer Name but it throws UnKnowHostException.
mySocket = new Socket("192.168.1.100", 10000); //it's ok!
mySocket = new Socket("My_PC_NAME", 10000); //it throw UnKnowHostException !
My cell phone is using WIFI and its IP is : 192.168.1.99
Please help me,
Thanks !

This is quite normal as your android device hasn't access to the DNS that assigns names to IP addresses. Since it isn't part of your PC network, you can't use computer names for the socket connection.

Related

Wifi Direct Group Owner Address is always 192.168.49.1, UnknownHostExeption

Was following this link to connect devices using Wifi direct to send message between two devices (device A with android 4.3 and Device B 7). When I try to connect client (device A) to server (device B) to transfer data using
socket.connect(new InetAddress(host, port)), 500);
It always gives UnknownHostException.
P.S - Device B is groupOwner. (checked using ConnectionInfo). IP address of groupOwner is always 192.168.49.1.
Any help will be appreciated.
You must make sure a Wi-Fi Direct connection is established, then the GO automatically runs the DHCP to assign IP addresses to itself (192.168. 49.1/24)

QTcpServer - Unsupported Socket Operation on Android

I am trying to start a server on Android using QTcpServer with Qt 5.3.1 but the server does not start and I get "Unsupported Socket Operation". It works fine on Windows.
Code below:
void StartListening()
{
QHostAddress hostAddress;
hostAddress.setAddress(QString("localhost"));
hostAddress.toIPv4Address();
quint16 portNumber = 9878;
server->setMaxPendingConnections(1);
server->setProxy(QNetworkProxy::NoProxy);
if (server->listen(hostAddress, portNumber))
{
// Ok
}
else
{
Debug("Server did not start. " + server->errorString());
}
}
server->errorString() returns "Unsupported Socket Operation when it runs on Android
Isn't this supported by Qt Android or am I doing something wrong?
Thx
OK! I worked it out.
The problem is with this line:
hostAddress.setAddress(QString("localhost"));
If I replace "localhost" with "127.0.0.1", the server starts fine but no one outside the "device the server is running on" can connect to it. This means, let's say your network is using 192.168.1.xx and your Android device has the following IP address: 192.168.1.2. If you start the server with "127.0.0.1" on your Android device which has an IP address: "192.168.1.2" and then using your PC with an IP address 192.168.1.3 you do telnet 192.168.1.2 9878 it will fail to connect!
So then I decided to start the server by specifying the IP address of the Android device: 192.168.1.2
hostAddress.setAddress(QString("192.168.1.2"));
Voila! That works too! The server starts and I can connect from outside the device! For example if I do telnet 192.168.1.2 9878 from my PC while the server is started on Android, it connects! So all I need to do now is replace the hard coded IP address with the actual IP address of the device! I think QNetworkInterface::allInterfaces() or something like that will give me the ability to get the default IP address.
So just use the actual IP address of the network card rather than localhost or 127.0.0.1 and all should be Ok. All working now.

Connecting Android with PC and displaying a message

I am new to android. I am just trying to connect my Android device to PC and pass a string to PC using Bluetooth. I have no idea on how to do it. Android side I read about the Bluetooth API. Please suggest me some ways to do it. Thanks in advance.
For Android, my code is slightly different from yours:
BluetoothSocket socket = Device.createRfcommSocketToServiceRecord(device_UUID);
socket.connect();
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeChar('x'); // for example
socket.close();
I used DataOutputStream to send data to PC. But surely this doesn't matter, just for your reference.
For PC,
LocalDevice localDevice = LocalDevice.getLocalDevice();
localDevice.setDiscoverable(DiscoveryAgent.GIAC); // Advertising the service
String url = "btspp://localhost:" + device_UUID + ";name=BlueToothServer";
StreamConnectionNotifier server = (StreamConnectionNotifier) Connector.open(url);
StreamConnection connection = server.acceptAndOpen(); // Wait until client connects
//=== At this point, two devices should be connected ===//
DataInputStream dis = connection.openDataInputStream();
char c;
while (true) {
c = dis.readChar();
if (c == 'x')
break;
}
connection.close();
I am not sure if the above codes still work today, as this was done 2 years ago. The BlueCove API may have changed a lot. But anyway, these codes work for me. Hope this may help you.
One more note is that, I had to uninstall the Toshiba Bluetooth Driver in my PC and reinstall the Microsoft one in order to make use of BlueCove. Otherwise, it won't work. (However, latest version of BlueCove may have already supported different drivers, please correct me if I said anything wrong.)
(Author: Victor Wong)
For clarification: on the PC side, you usually have a bluetooth device that comes with a virtual COM port. For testing purposes, you can use any terminal program (e.g. http://realterm.sourceforge.net/). When you start it on your virtual bluetooth serial port and connect your Android device, it will show the received data.

Android: Socket connection not working in a specific phone

Socket connection = new Socket("192.168.1.200", 6000); // 6000 is the standard port for the device to which i am connecting
InputStream is = connection.getInputStream();
Socket connection as implemented above does not work on LGP500(Android) but works fine on HTC Desire(Android). Could someone tell me what could be wrong...And the weird part is that it used to work fine in LG P500 before, stopped working all of a sudden...tried doing a factory reset..but it was no use still...
I get a SocketException: Network unreachable....
Would be really happy if someone could help me out on this..
That IP address is for a local subnet (probably WiFi) so make 100% sure the phone is using WiFi and not 3G for networking.

Can't use ServerSocket on Android

I'm trying to listen on a port using ServerSocket on an Android device. I want to be able to connect to this port over WiFi using a computer on the same network.
I get no exception when binding it to a port, however when I check netstat it says:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 (null):4040 (null):* LISTEN
I've tried countless ways of binding it to localhost, 0.0.0.0, the WiFi LAN IP address of the device with SocketInetAddress and InetAddress.getByName. Nothing seems to work.
When I try to connect to the port from a computer in the same WiFi (I've tried both netcat and Java's Socket.connect()), all I can see in Wireshark is an ARP request:
Who has [phone's LAN address]? Tell [computer LAN address].
This request repeat itself until timed out.
I've tried the reverse way, by setting the ServerSocket on the computer and connecting to that port from the phone, that works very well.
My testing phone is an Samsung Spica i5700 with a custom ROM.
Any ideas?
Edit:
The code is simple as this:
ServerSocket server = new ServerSocket();
server.setReuseAddr(true);
server.setTimeout(0);
server.bind(new InetSocketAddress(4040));
Socket client = null;
while((client = server.accept()) == null);
// Connected
enter code here
enter code here
Instead of using server.bind, try initializing the server socket like this:
server = new ServerSocket(4040);
Also, server.accept() will actually block until a connection is made, so you don't need that while loop (see: http://download.oracle.com/javase/1.5.0/docs/api/java/net/ServerSocket.html#accept() )
I struggled with this too and was only able to connect to my Android server by using:
ServerSocket myServerSocket = new ServerSocket();
String hostname = getLocalIpAddress();
myServerSocket.bind(new InetSocketAddress(hostname, myPort));
Where hostname was the local IP, which I got using the getLocalIpAddress() function from this page:
https://github.com/Teaonly/android-eye/blob/master/src/teaonly/droideye/MainActivity.java
I was able to get this working by using
ServerSocket server = new ServerSocket( myTcpPort, 0, addr );
where addr = InetAddress of your phone. Otherwise, it only seems to bind to localhost (127.0.0.1). Also, I'm using port 8080.

Categories

Resources