Android connect to server ip and port - android

what is the best approach of connecting android to server and ip and corresponding port? THis connection doesn't need to be all the time, but I am assuming I will send and recive files (in byte arrays or streams).
Thanks

Since the Android Development Tools are native to Java, you can use simple Java Socket APIs to accomplish this goal (see ServerSocket and Socket).
Server Code
You must start by opening a ServerSocket on your host computer by defining a port to listen on:
ServerSocket ss = new ServerSocket([some_port]);
Then you must begin listening for a client by calling ss.accept(). This method will block until a client connects:
Socket my_socket = ss.accept();
You now have a socket on your server that you can manipulate as you wish (probably through the use of ObjectInputStream and ObjectOutputStream):
ObjectOutputStream out = new ObjectOutputStream(my_socket.getOutputStream());
ObjectInputStream in= new ObjectInputStream(my_socket.getInputStream());
Client Code
You must establish a connection with the server that you have just created. You will do this by initializing a socket and passing in the IP address of your server (usually localhost for most testing purposes) and the port number on which your server is currently listening:
Socket socket = new Socket("localhost", [some_port]);
Again, establish some streams for communication:
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
And there you have it! You can now easily communicate with a server from an Android device. It is much simpler than you would think.
Please note, however, that this architecture implements TCP, which is much slower than UDP and will not work for any type of fast-paced data intensive games, but should accomplish your goals given your description above.

Related

Specify a network interface on 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

Try to open the socket connection which is alreay opened hangs the android system.How to solve it?

I am very new to android. I am developing an application in android 2.2 that sends the some bytes to server via socket connection.
In server side they used simple java coding for server socket implementation. The sending data in socket connection is success.
But my problem is when send the data in socket connection then I close the connection in client side (that is in mobile side). But in server side it does not closed.
Then again the client try to send data to server via socket. So the client side (in android mobile) application is hanged.
socket=new Socket(this.ipAddress,this.port_number);
The above coding snippet hangs the client system. How to show the error screen in the client side without hanging client application.
All are welcome to give your ideas
I got the solution,The following are the steps,codings,etc.
while(//condition)
{
socket=new Socket(this.ipAddress,this.port_number);
//Opening input and output streams
//Transfer the data
//After data transfer connectin is closed.
//While loop runs untill some condition
}

How to transfer sqlite3 database between two Android devices using Socket?

I have read this Bluetooth Chat post, and this Transfer file post.And I have two real android devices ,not AVDs, my
intent is to set IP address and port in one device which acts as a Client while the other acts as a Server.
They are using WIFI,and I have connected both of them to PC respectively.Get into adb shell ,and ping each other.It works.
I have written client code like this:
Socket socket = new Socket("192.168.1.142",8888);
InputStream in = socket.getInputStream();
byte[] buffer = new byte[in.available()];
Toast.makeText(this, String.valueOf(in.available()), Toast.LENGTH_LONG).show();
in.read(buffer);
String msg = new String(buffer);
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
and the Server:
ServerSocket server = new ServerSocket(8888);
while(true) {
Socket client = server.accept();
OutputStream out = client.getOutputStream();
String msg = "Hello Android!";
out.write(msg.getBytes());
client.close();
}
I have add this
<uses-permission android:name="android.permission.INTERNET" /> to manifest.
But no response. I have two questions:
1.Why there is no response in my client?
2.How to handle with sqlite3 database ,there must be something different with ordinary text files,but what is the difference?
Any suggestions will be very appreciated.
It would be unwise to transfer a sqlite3 .db between devices that may be differing make/model/manufacturer/etc. Rather, you should dump the schema and content of database to csv or sql and transfer that. You may want to compress the file too before transfer.
As for networking with Android. If you're using an AVD (emulator) then you're going to find it impossible or near impossible. Your proxy also plays a role in networking so you need to be warey of what it allows, how it's currently configured, and how it behaves (bugs, quirks, features). You should use a tool such as wireshark to inspect network comms and make sure that your App is even sending something out before worrying whether that something gets recieved.

Sockect for udp connection in android

Can anyone tell me which port is used for udp connection in android?
And how to open sockect for it?
DatagramSocket socket = new DatagramSocket();
socket.setSoTimeout (0);
socket.setSendBufferSize (16384);
socket.setReceiveBufferSize(16384);
socket.connect(address);
is one way of doing it. The socket.setXXX stuff is (obviously) optional.
As far as I remember ports below 1024 are reserved for privileged processes (could be wrong about that though).

address family not supproted by protocol error while connecting socket communication in android

in my app i want to receive some message form the server and based on that i want to display pop up message and for this i want to do socket communication in android.
When i am try to read response form the server using socket.getInputstream i will get error
"request time out :Address family not supported by the protocol"
Here is my code.
Socket socket = new Socket("localhost",62000));
boolean isconnect = socket.isConnected();
Log.e("Socket Connection ", String.valueOf(isconnect));
// Read and display the response message sent by server application
//
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
String message = (String) ois.readObject();
System.out.println("Message: " + message);
ois.close();
socket.close();
Unfortunately, this exception is caught and reported by Android, and your app doesn't get to see the stacktrace as far as I know (and it's reported at the debug level).
I'm pretty sure the cause of this exception is that an outside machine is trying to access Android and the port is closed (so the connection is refused).
Make sure:
You have a server running on the right port in Android
You turn on port forwarding for that port (e.g. you can have the service running on port 10000 in the Android emulator, and have your computer's port 20000 forward to that port)
Your client is accessing Android using 0.0.0.0 via the forwarded port (20000, not 10000)
You correctly specify TCP or UDP (might break things if it's the wrong one)
Hope this helps!

Categories

Resources