My application is scanning available ports where I'm interested in open/established session on a remote IP address.
My code below returns 22 (ssh) and 902 (Vmware) where a netstat -an returns:
tcp 0 0 192.168.1.160:60376 216.58.208.238:443 (google) ESTABLISHED
How can I retrieve the established session for a remote device?
My code for available ports (I do a loop on ports):
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), timeout);
socket.close();
Related
I want to connect two Android emulators (both running on the same host) with sockets.
I have a server running on one of them and listening to the 7000 port on the external IP address:
InetAddress addr = InetAddress.getByName("10.0.2.15");
ServerSocket serverSocket = new ServerSocket(7000, 50, addr);
try {
sock = serverSocket.accept();
} finally {
serverSocket.close();
}
and a client on a different emulator on the same host connecting to that port:
socket = new Socket();
socket.bind(new InetSocketAddress("10.0.2.15", 6666));
socket.connect(new InetSocketAddress("10.0.2.2", 7000));
I have bound the client to the 6666 port so I can do the adb port forwarding necessary:
(host) .\adb.exe forward --list
emulator-5554 tcp:7000 tcp:7000
emulator-5556 tcp:6666 tcp:6666
I can see with the debugger that the client socket is in "connected" state, but calling:
dataInputStream = new DataInputStream(socket.getInputStream());
dataInputStream.available(); // this throws IOException Broken Pipe
throws an IOException of Broken Pipe.
Any ideas where the problem is?
Using redir instead of adb forward seems to solve the problem.
On the server emulator
telnet locahost 5554
redir add tcp:7000:7000
On the client emulator
telnet localhost 5556
redir add tcp:6666:6666
I don't really understand why there is a difference, as the documentation states:
"The Android Debug Bridge (adb) tool provides port forwarding, an alternate way for you to set up network redirection."
I have created AndroidWebServer on my android phone.
When I try to access 192.168.1.150:8000 (phone address) I have good response from the server. But when I try to access the same url from the pc (connected via WiFi on the same network) nothing happens.
When the server is active if I run this
adb shell netstat -at
tcp 0 0 ::ffff:127.0.0.1:8000 :::* LISTEN
That is weird because other services got foreign address
tcp 0 0 ::ffff:192.168.1.150:54 ::ffff:173.194.76.188:5 ESTABLISHED
tcp 0 0 ::ffff:192.168.1.150:36 ::ffff:31.13.92.33:http ESTABLISHED
for my service the foring address is :::*
I am not sure what I do wrong
https://github.com/lopspower/AndroidWebServer
AndroidWebServer androidWebServer = new AndroidWebServer(8000);
androidWebServer.start();
What should I change in order my phone to be accessible from my pc connected to the same WiFi network?
Thanks
Looks like your server is listening on localhost. That means it will only accept connections that originate on the local machine. Try listening on 0.0.0.0 instead; that means you accept connections from all origins.
EDIT
Change this line:
AndroidWebServer androidWebServer = new AndroidWebServer("0.0.0.0", 8000);
I am trying to make a bluetooth connection with my Android client and a Python server. However, I am unable to do so since my Android client always fails to connect. Now I am wondering whether it is possible at all to use an Android Bluetooth socket and a python socket together. Is that possible?
And if so, do you have suggestions what else I might try? In a nutshell, this is what I do:
My Android client:
I get a Bluetooth device like this:
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().
getRemoteDevice(deviceAddress);
Where deviceAddress is equal to the bluetooth MAC address of my laptops bluetooth adapter. Which is E0:F8:47:3F:80:49
Then I use that device to create a Socket:
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(MY_UUID);
Where MY_UUID is this string: 00001101-0000-1000-8000-00805F9B34FB
After that I just call connect:
socket.connect();
And that is where it fails to connect. Maybe it is because of server wihich looks like this:
My Python server:
import bluetooth
hostMACAddress = 'E0:F8:47:3F:80:49' # The MAC address of a Bluetooth adapter
port = 3
backlog = 1
size = 1024
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.bind((hostMACAddress, port))
s.listen(backlog)
try:
client, clientInfo = s.accept()
while 1:
data = client.recv(size)
if data:
print(data)
client.send(data) # Echo back to client
except:
print("Closing socket")
client.close()
s.close()
So I have been working on a project in which a device running Android (API level = 14) must connect to a server running Linux (to be specific: a Raspberry Pi) via Bluetooth. When a connection is established, the app sends an encrypted XML string to the RPi. The RPi must decrypt this string, parse the XML and perform the corresponding action. The result of the action is send back to the Android device.
So far, I have managed to create a connection between the app and the RPi (which runs the latest version of the Bluez package). The RPi has a Bluetooth 4.0 dongle from Targus. The point where I'm stuck at, is when I try to send a string from the app to the RPi. The Bluetooth socket appears to be closed by then. Logcat gives the message Connection reset by peer.
The code used to create the socket is as follows:
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);
Logcat output is as follows:
06-20 14:29:42.224: DEBUG/RPiService(24273): ---------- [ CONNECTION ESTABLISHED ] ----------
06-20 14:29:42.224: DEBUG/RPiService(24273): connected, Socket Type:Secure
06-20 14:29:42.229: DEBUG/RPiService(24273): create ConnectedThread: Secure
06-20 14:29:43.734: DEBUG/RPiService(24273): setState() 2 -> 3
06-20 14:29:43.739: DEBUG/RPiService(24273): Connection reset by peer
06-20 14:29:43.744: WARN/System.err(24273): java.io.IOException: Connection reset by peer
06-20 14:29:43.754: WARN/System.err(24273): at android.bluetooth.BluetoothSocket.writeNative(Native Method)
06-20 14:29:43.759: WARN/System.err(24273): at android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:398)
06-20 14:29:43.764: WARN/System.err(24273): at android.bluetooth.BluetoothOutputStream.write(BluetoothOutputStream.java:85)
06-20 14:29:43.769: WARN/System.err(24273): at com.example.BluetoothTest.RPiService$ConnectedThread.run(RPiService.java:344)
On the side of the RPi, I am essentially running the following example server script from the PyBluez package:
from bluetooth import *
server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "00001101-0000-1000-8000-00805F9B34FB"
advertise_service( server_sock, "SampleServer",
service_id = uuid,
service_classes = [ uuid, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ]
)
print "Waiting for connection on RFCOMM channel %d" % port
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info
try:
while True:
data = client_sock.recv(1024)
if len(data) == 0: break
print "received [%s]" % data
except IOError:
pass
print "disconnected"
client_sock.close()
server_sock.close()
print "all done"
I've tried various UUIDs suggested by posts I read on SO including 00001101-0000-1000-8000-00805F9B34FB, 94f39d29-7d6d-437d-973b-fba39e49d4ee and 00000003-0000-1000-8000-00805F9B34FB (always the same on both ends of the connection). It appears to be that the first one is correct as I can't even make a connection when using an other UUID.
What may be the cause for the connection to be reset by the RPi? If anyone would be able to point me in the right direction, I'd be grateful.
It turned out that the default Bluez configuration on Debian was the cause of the connection issues (as described in this answer. Disabling the pnat plugin in /etc/bluetooth/main.conf allowed for communication between Android and the RPi.
DisablePlugins = pnat
For future reference, the UUID used by the applications is 00000003-0000-1000-8000-00805F9B34FB.
I'm trying to develop an Android application for a medical device using Bluetooth SPP. My Android application works as Bluetooth server. The problem is that the medical device (UA-767PBT) may not conform SDP process and uses a fixed port# for the connection. So the connection only works after reboot the Android device.
I'm seeking the way to create a Bluetooth server side socket with a specific rfcomm channel/port# using Java reflection.
I've found some code doing the similar things using Java reflection:
Create Bluetooth client side socket with a specific port#.
//Instead of using createRfcommSocketToServiceRecord
Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
// using port#1 / channel #1
clientSocket = (BluetoothSocket)m.invoke(mmDevice, Integer.valueOf(1));
Inspect the port# used in Bluetooth server side socket.
BluetoothServerSocket serverSocket;
BluetoothSocket socket;
int port;
serverSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(SERVICE_NAME, DEVICE_UUID);
Field mSocketField = BluetoothServerSocket.class.getDeclaredField("mSocket");
mSocketField.setAccessible(true);
socket = (BluetoothSocket) mSocketField.get(serverSocket);
mSocketField.setAccessible(false);
Field mPortField = BluetoothSocket.class.getDeclaredField("mPort");
mPortField.setAccessible(true);
port = (Integer) mPortField.get(socket);
mPortField.setAccessible(false);
Log.d("BT Server:", "The random port#: " + port);
socket = serverSocket.accept();
Create Bluetooth server side socket with a specific port number. How?
I also seek other solutions for this connection problem.