Specify a network interface on Android - android

I've read the discussion, and now I am working on my own simple Android firewall. Here is the algorithm they used.
The discussion
I was able to forward packets. If I remember correctly I just needed to read the TCP header and open another socket to that destination and send it headerless.
So now I need to write data to my Wi-Fi or 3G network interface if I understand them correctly. How to do that?

I solved it. I only needed to protect the socket which I'd like to use to send data directly to the server without using the TUN device.
Socket socket = socket = SocketChannel.open().socket();
if (!protect(socket)) {
Log.e(Constants.TAG, "Failed to protect the socket");
}
The documentation

Related

RXKOTLIN/RXJAVA: Communication between the socket using Observables

I am a newbie to RXKotlin/RXJava.
I am developing the background service in Android.
In my service, I have
Bluetooth socket
TCP Socket
Whenever the data is available on the Bluetooth socket, read and write to the TCP socket. And whenever data is received in the TCP socket, write to the Bluetooth socket.
Can someone help me:
how to achieve this using Observables?
how to exchange the socket id information?
how to exchange the data?
Thanks
Please try using RxSubjects (https://blog.mindorks.com/understanding-rxjava-subject-publish-replay-behavior-and-async-subject-224d663d452f)
Let me take PublishSubject as an example here.
//a publish subject which publishes int values
public PublishSubject<Integer> source = PublishSubject.create();
source.onNext(1);
source.onNext(2);
So above lines of code goes in Bluetooth socket class.
Now in TCP socket class, using the source, you can observe here.
source
.subscribe(
{
//result
},
{
//error
}
)
Thats it.
Please make sure, the subscription happens before Bluetooth socket starts publishing data.

How to make the Android Device listen to a particular port number

I am new to android and is trying to develop an application. I have a local server that has the address like http://abc:9070/
i.e: the server is running only on port number 9070 in my laptop.
Now i want to debug my program using a android device and i have to make sure that the android device listens to port number 9070, So that i can make the post http request call to the url and fetch some information.
Can someone tell me how can i make my device to listen to port number 9070?
Also can someone tell me whether changing default port number of adb solve this.
I have tried a lot to search for a solution. But i am not able to come up with any good answers.
Thanks in advance.
Nobody has expressed an opinion yet. May be the question is not clear, at least I found it very difficult to understand what you are trying to do.
You say you have a server (laptop) listening on port 9070 and you want a device to connect to this server thru this port? Is that right?
Have you try, from your device, launch the navigator and connect to that address? http://abc:9070
Anyway, the java code to make a socket connection is something similar to this:
try
{
Socket clientSocket = new Socket("YOUR_LAPTOP_IP", 9070);
// 1024 is an arbitrary number, could be 512, 65535, etc
byte[] buffer = new byte[1024];
int ret=0;
while ((ret=clientSocket.getInputStream().read(buffer)) > 0)
{
// from now on it's up to you what to do with the data you read
}
clientSocket.close();
}
catch (Exception e)
{
e.printStackTrace();
}

How do i send information via a setup bluetooth connection

So im working around with bluetooth and trying to figure out how to send two strings via a bluetooth connection. From one android device to another.
I found this guide http://developer.android.com/guide/topics/connectivity/bluetooth.html
but it talks alot about setting up the connection. So i went straight down to the chapter about Managing a Connection. The reason i do this is that in the apps i create i plan to setup the bluetooth connection before opening the apps (via the phones usual bluetooth setup) and then open the apps and send when it is necessary.
So my question is how do i find the bluetooth socket that should be setup? Since that should be what im searching for to create the sending and recieving threads?
Hope this is enough information, else tell what more you need and ill try and answer to the best of my ability.
Best Regards Drakthal
The usual bluetooth setup only pairs between devices, it doesn't create a data connection between them (And even if it would, you wouldn't be able to access this Socket object because it's not created in your process).
After Bluetooth is turned on, you can call BluetoothAdapter.getBondedDevices() to get a set of the paired devices. You can then iterate over them, and initiate a connection to the one you want. You can't avoid the connection creation :( If you want a simplified example, you can look here (An answer I posted a while ago, regarding the whole pairing/connecting/sending/receiving subject with bluetooth).
Once you acquired an open connection, sending the 2 string is easy.
String s1 = "A", s2 = "B";
byte[] buf1 = s1.getBytes(), buf2 = s2.getBytes();
OutputStream os = connection.getOutputStream();
os.write(buf1);
os.write(buf2);
os.flush();
connection.close();

How to transfer data from one device to another using wifi network in android 2.2

I need to trnasfer data from one android device to another device over wifi network?
If one device is willing to perform access-point like functionality as part of a portable hotspot, and you select that as a wirelss network on the other, you may achieve a network connection between the two that can be used for custom traffic. Though there are ways the hotspot could be implemented where that would not work. Also of concern, the "client" device will now be sending all of its network traffic through the hotspot device, including not just foreground apps but anything it decides to do in the background.
First you need to know to which device you want to transfer the data over network like to mobile device to pc. Suppose you are trying to send data you need will a continuous socket connection using a thread ... the back end device should accept the socket connection and accept the stream....
consider the frist device as client which will write the stream
Runnable action = new Runnable() {
public void run() {
//create your connection
while (true){
//write your stream here
}
}
};
new Thread(action).start();
and server side the device will open the stream and read it continuously
now in the write your stream code use link the and integrate like the example
Click here!

Problem when connecting to FTP server through android?

I've an UnknownHostException when i used this method for uploading files from ddms:
try {
SimpleFTP ftp = new SimpleFTP();
// Connect to an FTP server on port 21.
ftp.connect("ftp://*******", 21, "*****", "*****");
// Set binary mode.
ftp.bin();
// Change to a new working directory on the FTP server.
ftp.cwd("web");
// Upload some files.
ftp.stor(new File("data/data/com.android/file/contacts"));
// Quit from the FTP server.
ftp.disconnect();
}
catch (IOException e) {
// Jibble.
}
What is the problem for this method? Anyone clarify me.
There are two major problems with using FTP on the android emulator:
The emulator takes hold of a specific port on the host machine (between 5554 and 5584) to access the internet.
See http://developer.android.com/guide/developing/tools/emulator.html
FTP communicates on two ports. The initial port (the one the emulator is using) and a secondary data communication port (usually defined by the FTP client and server).
See http://www.troubleshootingnetworks.com/ftpinfo.html for information on how FTP works.
This means that the initial communication with the FTP server works the way it is intended, but once you are attempting to pass data to / from the server the emulator cannot communicate with the port the FTP server requests because your computer doesn't know what to do with the traffic on that port. See the link above to get a better grasp on FTP communications.
If you want to test FTP on Android you will need to have a device with its own internet connection.
Cursory look at SimpleFTP example suggests that you need to use host name without ftp:// prefix. Also make sure that you include INTERNET permission in manifest.

Categories

Resources