Socket connection android to iphone - android

i'm developing a small example about socket connection between android and ios (via wifi), after trying, the connection hasn't been established. Here is what I have done so far, I created a server on ios (used Bonjour to publish the service). I also created a client on android. However, after starting the server on ios, I also got the log:
ServerSocketConnection[3487:c07] Bonjour Service Published: domain(local.) type(_serversocket._tcp.) name(Macmini) port(54065)
Which means the server starting ok.
To the client part(android), I created the client through Socket class, some few lines of code:
Socket s = new Socket("local.", 54065);
OutputStream out = s.getOutputStream();
PrintWriter output = new PrintWriter(out);
output.println("Hello Android!");
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
String st = input.readLine();
Putting it into AsyncTask to execute. However, I got the UnknowHostException:
08-06 12:45:44.460: W/System.err(873): java.net.UnknownHostException: Unable to resolve host "local.": No address associated with hostname
I'm a newbie to this kind of problem so any ideas what the problem is? I know it's related to the "host" thing but need the way to fix it.
*Note: I run 2 apps on 2 simulators (ios and android) as the same wifi network and same MAC, maybe this is the problem? any help would be appreciated and sorry for my English, it's not my native one.

Use the actual IP address of the iPhone instead of 'local.'.
On Android you can find a phone's IP via Settings -> WiFi -> Advanced. Not sure if iPhone offers the same option.
ps. Also be sure to have the internet permission in your manifest;
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Related

How to establish simple communication between android app and C# desktop app

I've created C# application which is running (almost non-stop) on my windows 7 desktop computer. Now I'm looking for a simple way to tell my desktop application to stop running from my android phone. My initial plan was to have .txt file on my ftp server, so desktop app would check i.e. every hour if .txt file contains command to shut down (pressing a button on android app would change .txt file on ftp server). However even after few hours of tutorials on java I was still unable to figure out working with ftp connection.
What would be easiest way given my lack of java knowledge (I understand I'll have to learn a bit more, but I really don't want to get too deep into java for now)?
The simplest way would be to send a simple udp or tcp message to your windows application.
http://developer.android.com/reference/java/net/DatagramSocket.html
String messageStr="Shutdown!";
int server_port = 8855;
DatagramSocket s = new DatagramSocket();
InetAddress local = InetAddress.getByName("192.168.1.55");
int msg_length=messageStr.length();
byte[] message = messageStr.getBytes();
DatagramPacket p = new DatagramPacket(message, msg_length,local,server_port);
s.send(p);
In your C# Application you simply open a Socket and wait for your packet.
How do I make a UDP Server in C#?

Android FTP with 4G

I am trying to connect to an FTP server in an Android application and I believe that I fall in an infinite loop, but can't understand why. The library I am using is Apache Comomns net.
I have the internet permission in manifest :
<uses-permission android:name="android.permission.INTERNET"/>
I am also connecting to the FTP in an AsyncTask.
Here is a sample code which was supposed to work :
// I have other values when I test it of course
FTPClient ftpClient = new FTPClient();
ftpClient.connect("111.111.111.111"); // It stops working here when in 4G
ftpClient.login("user", "mdp");
ftpClient.changeWorkingDirectory("directory");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.logout();
ftpClient.disconnect();
It works on Wifi (and my real application works too in wifi) but when I use my 4G (or 3G) it seems to loop at the line where I try to connect() forever.
I have tryed to understand what there is behind this but it's not clear for me.
How can I connect to my FTP using 4G?
Any help would be appreciated.
It sounds like your network operator may have the FTP port blocked.
To test this you can try to ftp to the same site on a browser in your mobile device - if it works then it point to an issue in your app.
If it does not work then try with a known working FTP site - Tele2 host a test site for example (there are lot of other open FTP sites if you do a quick search):
ftp://speedtest.tele2.net
If you can't access this from your mobile device over 4G then it looks very likely that your operator has FTP, and you should contact them to find out why and what you can do to remove the block.

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.

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();

TCP problem - hangs when reading server's response

I am trying to write client for Android which is supposed to communicate with PC server application on local network. Server app is written by my friend in C#. Currently there is an iPhone app that is using this server application with no problems.
I have very simple code for TCP client:
1. Socket s = new Socket(server, port);
2. OutputStream out = s.getOutputStream();
3. PrintWriter output = new PrintWriter(out);
4. output.println("ACTION=Next&VALUE=0&");
5. BufferedReader input = new BufferedReader(new nputStreamReader(s.getInputStream()));
6. String st = input.readLine();
I went through many TCP implementation examples, and they are all similar. Pretty much like my code above. My app freezes on line 6 when I try to read response from the server.
It doesn't cause any errors (no exceptions), nothing shows in debugger, just timeout error after awhile. Server is supposed to return string after executing my action in line 4.
I don't understand why this code hangs. Input is not NULL (I've checked it). I would expect some exception to be thrown or simply empty string to be returned.
So? What am I missing? Could it be problem with some special characters that server app is sending and android can't handle that? Do I need any special permission in my manifest?
I am positive that I have correct IP address and correct port number. I can see that on server application running on my PC.
Thanks.
String st = input.readLine(); Does this command make the program wait until something is being read or it just takes straight whatever is in the buffer. I guess you will need to check continuosly if something came from the server. I mean, you need one infinite loop, something like
While (True){
st = input.readLine();
This will check if anything came the whole time. If u dont use it and if String st = input.readLine(); command doesnt make the program wait, then your code will end without taking anything.
You of course need internet permission in the manifest.
If it were me I'd add instrumentation to the server so that it can tell you when there's been a connection, when that connection has received data, and when a reply is being sent. Or else run tcpdump on the server.
You could also temporarily try grabbing the input character by character rather than a whole line at a time.
Try to figure out how far the "conversation" is progressing so you can figure out where it is really getting stuck

Categories

Resources