I am trying to get a good grasp of UDP on Android, so I am writing a small test program to set up 2 sockets on the same machine, send a packet and receive it. I thought it would be straight-forward, but something is going wrong. My code is:
DatagramSocket server;
DatagramSocket client;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Initialize a client socket and a server socket using UDP
try {
server = new DatagramSocket();
client = new DatagramSocket();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Server will now send some data
String data = "This is a test";
DatagramPacket sendPacket = new DatagramPacket(data.getBytes(), data.length(),
client.getInetAddress(), client.getLocalPort());
try {
server.send(sendPacket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Client has to receive and read the data
byte[] buf = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(buf, buf.length);
try {
client.receive(receivePacket);
byte[] receivedData = receivePacket.getData();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
server.disconnect();
client.disconnect();
}
When I debug my code, it goes into the last try, in "client.receive(receivePacket);" but it never comes back from that function call. The packet seems to be never received, but I have a hard time finding why that is.
I hope someone could please help me fix this problem.
EDIT: New code using threads. Calling "recieve" before actually sending the packet. Trying to use AsyncTask.
public class udpexample extends Activity {
DatagramSocket server;
DatagramSocket client;
String data = "This is a test";
private static final String TAG = "UDPExampleActivity";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i(TAG, "Starting");
// Initialize a client socket and a server socket using UDP
try {
server = new DatagramSocket();
client = new DatagramSocket();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new ReceivePacket().execute();
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Server will now send some data
DatagramPacket sendPacket = new DatagramPacket(data.getBytes(), data.length(),
client.getInetAddress(), client.getLocalPort());
try {
Log.i(TAG, "About to send");
server.send(sendPacket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class ReceivePacket extends AsyncTask<Void, Void, Void> {
// Thread used to receive a packet
#Override
protected Void doInBackground(Void... params) {
// Client has to receive and read the data
byte[] buf = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(buf, buf.length);
try {
Log.i(TAG, "About to receive");
client.receive(receivePacket);
byte[] receivedData = receivePacket.getData();
// Verify that the packet is identical to the one being sent
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
server.disconnect();
client.disconnect();
}
}
}
Still having the same issue, it stops and waits on receive and nothing happens.
Thank you very much,
Jary
I think you are sending a packet while receiver is not yet listening.
Try first client.receive(..) and then server.send(..). That way client will be listening for incoming packets. Of course, you will have to run this in two different threads.
Related
I'm having a weird problem. I'm creating a socket and giving it the IP 192.168.43.255. When I use InetAddress.getByName(IP) it adds / to the ip as shown in the log below. Why this is happening ??
here is my code
public class ServerCom extends AsyncTask<String, Void , String>{
private int port=9999;
private String IP="192.168.43.255";
private BufferedReader input;
private Socket socket;
private DataOutputStream toSer;
private InetAddress serverAddr;
private String LocationID;
File file;FileWriter writer;
#Override
protected String doInBackground(String... RSS) {
// TODO Auto-generated method stub
Log.d("s","async");
SandboxView.Locate=false;
try {
serverAddr = InetAddress.getByName(IP);
socket = new Socket(serverAddr, port);
toSer = new DataOutputStream(socket.getOutputStream());
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// send result to server
try {
toSer.writeBytes(RSS[0]+"\n");
//get the response from server
LocationID=input.readLine();
toSer.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return LocationID;
}
protected void onPostExecute(String result)
{
super.onPostExecute(result);
//dismiss progressdialog.
//update ui
MainActivity.LocationID=LocationID;
SandboxView.Localization=MainActivity.CH2.getLocation(LocationID);
try {
writer = new FileWriter(file, true);
writer.append("room :"+LocationID+"\n");
writer.append(SandboxView.Localization.x+" "+SandboxView.Localization.y+"\n");
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MainActivity.view.invalidate();
SandboxView.Locate=true;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
}
05-16 17:53:16.331: W/System.err(3495): java.net.ConnectException: /192.168.43.255:9999 - Network is unreachable
This isn't really adding a / to the IP address, but the format of the address output by InetAddress.toString() uses a / to separate the hostname from the host address. See here. So, the / isn't really being added to the address, it is just shown in the logging.
I am trying to read data continuously using the following code:
public class MyClientTask extends AsyncTask<Void, Void, Void> {
String dstAddress;
int dstPort;
String response = "";
MyClientTask(String addr, int port){
dstAddress = addr;
dstPort = port;
}
#Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream inputStream = socket.getInputStream();
while ((bytesRead = inputStream.read(buffer)) != -1){
readInpt = inputStream.toString();
byteArrayOutputStream.write(buffer, 0, bytesRead);
response = byteArrayOutputStream.toString("UTF-8");
}
textResponse.setText(readInpt);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "IOException: " + e.toString();
}finally{
if(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
#Override
protected void onPostExecute(Void result) {
textResponse.setText(response);
super.onPostExecute(result);
}
}
But for some reason, it doesn't show me any output in the textbox. any help would be appreciated.
There are at least two issues in your code.
Frist, I'm not sure the method toString() on the inputStream is going to work, because the documentation says it returns a description of the object (which would be different than the string recieved). You might be confusing this with the contents of buffer which might be what you really want.
readInpt = inputStream.toString(); // Probably wrong
Second. You're updating the User Interface from a background thread, inside doInBackground() , which is always forbidden.
textResponse.setText(readInpt); // Forbidden, move somewhere else, e.g. onPostExecute()
try {
socket = new Socket(dstAddress, dstPort);
BufferedReader stdIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
response = stdIn.readLine();
publishProgress(response);
Log.i("response", response);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "IOException: " + e.toString();
}
catch (Exception e) {
e.printStackTrace();
}
You cannot print it on text field because the socket will listen to the server socket.If server socket does not send any response it will listen to the socket continuously until the response is received.
I am a complete noob at Android, only at the level of basic (1 or 2 line) Activities activated by buttons, but I would like to create a really simple app that, when I tap the app icon, it fires and forgets a message to a listening server on my Windows 8 PC. The phone is connected as a simple media device, without Kies, via a USB cable.
I can get as far as a message box lying and saying the message was sent. I need to know what kind of comms channel to use, e.g. a COM port or what, and how to send data over that from Android. On the Windows side, once I've established how to communicate, I can help myself.
starting with the desktop side of this application:
You can use the ADB (Android debug bridge) to establish a tcp/ip socket connection via port between the device and desktop. The command is :
adb forward tcp:<port-number> tcp:<port-number>
to run this command in your java program, you will have to create a process builder wherein this command ets executed on a child shell.
For windows you may need to use:
process=Runtime.getRuntime().exec("D:\\Android\\adt-bundle-windows-x86_64-20130729\\sdk\\platform-tools\\adb.exe forward tcp:38300 tcp:38300");
sc = new Scanner(process.getErrorStream());
if (sc.hasNext())
{
while (sc.hasNext())
System.out.print(sc.next()+" ");
System.out.println("\nCannot start the Android debug bridge");
}
sc.close();
}
The function required to execute adb commands :
String[] commands = new String[]{"/bin/sh","-c", command};
try {
Process proc = new ProcessBuilder(commands).start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String s = null;
while ((s = stdInput.readLine()) != null)
{
sb.append(s);
sb.append("\n");
}
while ((s = stdError.readLine()) != null)
{
sb.append(s);
sb.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
Above method will take the above command as a string and execute it on a child shell
//Extracting Device Id through ADB
device_list=CommandExecutor.execute("adb devices").split("\\r?\\n");
System.out.println(device_list);
if(device_list.length>1)
{
if(device_list[1].matches(".*\\d.*"))
{
device_id=device_list[1].split("\\s+");
device_name=""+CommandExecutor.execute("adb -s "+device_id[0]+" shell getprop ro.product.manufacturer")+CommandExecutor.execute("adb -s "+device_id[0]+" shell getprop ro.product.model");
device_name=device_name.replaceAll("\\s+"," ");
System.out.println("\n"+device_name+" : "+device_id[0]);
device=device_id[0];
System.out.println("\n"+device);
}
else
{
System.out.println("Please attach a device");
}
}
else
{
System.out.println("Please attach a device");
}
CommandExecutor is a class which has the execute method in it. The code of the execute method is the same as posted above.
This will check if any device is connected and if it connected, it will return it unique id number.
It is preferable to use id number while executing adb commands like :
adb -s "+device_id[0]+" shell getprop ro.product.manufacturer
OR
adb -s <put-id-here> shell getprop ro.product.manufacturer
Note that '-s' is necesarry after adb.
Then using adb forward commnd you need to establish a tcp/ip socket. Here the desktop will be the client and mobile/device will be the server.
//Create socket connection
try{
socket = new Socket("localhost", 38300);
System.out.println("Socket Created");
out = new PrintWriter(socket.getOutputStream(), true);
out.println("Hey Server!\n");
new Thread(readFromServer).start();
Thread closeSocketOnShutdown = new Thread() {
public void run() {
try {
socket.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
};
Runtime.getRuntime().addShutdownHook(closeSocketOnShutdown);
}
catch (UnknownHostException e) {
System.out.println("Socket connection problem (Unknown host)"+e.getStackTrace());
} catch (IOException e) {
System.out.println("Could not initialize I/O on socket "+e.getStackTrace());
}
Then you need read from the server i.e. the device :
private Runnable readFromServer = new Runnable() {
#Override
public void run() {
try {
System.out.println("Reading From Server");
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while ((buffer=in.readLine())!=null) {
System.out.println(buffer);
}catch (IOException e) {
try {
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
The 'buffer' will contain what device will send from its end of the app.
Now in your mobile application,you will need to open the same connection and siple write data to it out buffer
public class TcpConnection implements Runnable {
public static final int TIMEOUT=10;
private String connectionStatus=null;
private Handler mHandler;
private ServerSocket server=null;
private Context context;
private Socket client=null;
private String line="";
BufferedReader socketIn;
PrintWriter socketOut;
public TcpConnection(Context c) {
// TODO Auto-generated constructor stub
context=c;
mHandler=new Handler();
}
#Override
public void run() {
// TODO Auto-generated method stub
// initialize server socket
try {
server = new ServerSocket(38300);
server.setSoTimeout(TIMEOUT*1000);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//attempt to accept a connection
try{
client = server.accept();
socketOut = new PrintWriter(client.getOutputStream(), true);
socketOut.println("Hey Client!\n");
socketOut.flush();
syncContacts();
Thread readThread = new Thread(readFromClient);
readThread.setPriority(Thread.MAX_PRIORITY);
readThread.start();
Log.e(TAG, "Sent");
}
catch (SocketTimeoutException e) {
// print out TIMEOUT
connectionStatus="Connection has timed out! Please try again";
mHandler.post(showConnectionStatus);
try {
server.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
catch (IOException e) {
Log.e(TAG, ""+e);
}
if (client!=null) {
try{
// print out success
connectionStatus="Connection succesful!";
Log.e(TAG, connectionStatus);
mHandler.post(showConnectionStatus);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
private Runnable readFromClient = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
Log.e(TAG, "Reading from server");
socketIn=new BufferedReader(new InputStreamReader(client.getInputStream()));
while ((line = socketIn.readLine()) != null) {
Log.d("ServerActivity", line);
//Do something with line
}
socketIn.close();
closeAll();
Log.e(TAG, "OUT OF WHILE");
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
public void closeAll() {
// TODO Auto-generated method stub
try {
Log.e(TAG, "Closing All");
socketOut.close();
client.close();
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Runnable showConnectionStatus = new Runnable() {
public void run() {
try
{
Toast.makeText(context, connectionStatus, Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
};
}
}
Using Managed.adb in c#
AndroidDebugBridge bridge = AndroidDebugBridge.
CreateBridge(#"C:\Users\bla\AppData\Local\Android\Sdk\platform-tools\adb.exe", true);
List<Device> devices = AdbHelper.Instance.
GetDevices(AndroidDebugBridge.SocketAddress);
devices[0].CreateForward(38300, 38300);
Then create a server socket on android to that port.
server = new ServerSocket(38300);
server.setSoTimeout(TIMEOUT * 1000);
socket = server.accept();
and client socket on C#
TcpClient clientSocket = new System.Net.Sockets.TcpClient();
clientSocket.Connect("127.0.0.1", 38300);
I have a BufferedReader, when I try to read it, it just hangs and doesn't do anything, am I doing this right? I am using this in an AsyncTask.
Edit: I have a tablet connected to the Wi-Fi, this connects to my computer which is broadcasting on 172.20.104.203 on port 5334, I can see when the thread starts, but nothing after that.
Here my code:
try {
final BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
final String msg;
msg = (line);
Log.d("DeviceActivity", msg);
}
} catch (Exception e) {
e.printStackTrace();
Log.e("ClientAcivtity: Exception",
String.valueOf(e));
}
EDIT
I have all the right permissions or anything, I was doing this outside a AsyncTask and it worked perfectly, moved it because I didn't want it in the main thread.
-Edit , here is the full code.
public class NetworkTask extends AsyncTask<Void, byte[], Boolean> {
Socket nsocket; // Network Socket
InputStream nis; // Network Input Stream
OutputStream nos; // Network Output Stream
private Handler handler = new Handler();
Boolean connected = false;
public static final int PORT = 5334;
public String SERVERIP = "172.20.104.203";
Socket socket;
#Override
protected void onPreExecute() {
Log.i("AsyncTask", "onPreExecute");
InetAddress serverAddr;
try {
serverAddr = InetAddress.getByName(SERVERIP);
socket = new Socket(serverAddr, PORT);
connected = true;
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("ClientAcivtity: Exception", String.valueOf(e));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("ClientAcivtity: Exception", String.valueOf(e));
}
}
#Override
protected Boolean doInBackground(Void... params) { // This runs on a
// different thread
boolean result = false;
try {
Log.d("ClientActivity", "C: Connecting...");
if (socket != null) {
int cont = 1;
while (cont == 1) {
try {
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
// where you issue the commands
out.println("getPos");
Log.d("ClientActivity", "C: Sent " + "getPos");
} catch (Exception e) {
Log.e("ClientAcivtity: Exception",
String.valueOf(e));
}
try {
final BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
final String msg;
msg = (line);
Log.d("DeviceActivity", msg);
}
} catch (Exception e) {
e.printStackTrace();
Log.e("ClientAcivtity: Exception",
String.valueOf(e));
}
cont--;
}
Log.d("ClientActivity", "C: Closed.");
}
} catch (Exception e) {
Log.e("ClientAcivtity: Exception", String.valueOf(e));
}
return result;
}
#Override
protected void onProgressUpdate(byte[]... values) {
if (values.length > 0) {
Log.i("AsyncTask", "onProgressUpdate: " + values[0].length
+ " bytes received.");
}
}
#Override
protected void onCancelled() {
Log.i("AsyncTask", "Cancelled.");
}
#Override
protected void onPostExecute(Boolean result) {
if (socket != null) {
if (connected) {
if (result) {
Log.i("AsyncTask",
"onPostExecute: Completed with an Error.");
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Log.i("AsyncTask", "onPostExecute: Completed.");
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
My guess is that when you write out the command "getPos" the underlying BufferedWriter is not actually sending the data out on the line (you should verify this with tcpdump/wireshark). If this is the case, the server doesn't responsed to the readLine(), since it never got a command. To verify this claim, add out.flush(); after out.println("getPos");
Really, tcpdump will probably give you a better answer then anyone on the forums.
Also see http://developer.android.com/reference/java/io/BufferedWriter.html
Try doing it like this:
final BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
StringBuffer buf = new StringBuffer();
int i;
while((i = in.read()) != -1){
buf.append((char) i);
}
String data = buf.toString();
Reading from sockets is a quite difficult issue depending where the socket is actually connected to and how the other side responds.
If the other side is extremely fast than it can provide the socket with enough data so that the read routines actually work fine. However if there is a delay in the other side of any kind (just needs to be slower than your read routine incl the small default timeout) then your read fails even though there might be data on the other side - just arriving a little too slow at the socket.
Depending on your needs you may wrap your own minimum and maximum timer around the read routine.
Please provide more information and we can better understand the issue.
In many cases it is necessary to have a minimum timeout large enough for the other side to push data to the socket - but you might also need a maximum time for how long you actually want to wait for data to arrive.
UPDATE:
first the runnable to start the monitoring thread. You may use monitoringCanRun in your loop to interrupt the thread if required. And monitoringThreadIsAlive can be used to know if the thread is still running.
monitoringCanRun = true;
new Thread(new Runnable() {
public void run() {
monitoringThreadIsAlive = true;
performMonitoring();
monitoringThreadIsAlive = false;
}
}).start();
}
and performMonitoring looks like:
public void performMonitoring() {
while (monitoringCanRun) {
... do your read in the while loop
...you might like to insert some delay before trying again...
try { //we delay every partial read so we are not too fast for the other side
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I use the following code to receive the data from a particular port. It's not working in Android. But sending data to particular port is working fine.
public class UDPDemo extends Activity {
private TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.recv_message);
try {
DatagramSocket clientsocket=new DatagramSocket(9876);
byte[] receivedata = new byte[1024];
while(true)
{
DatagramPacket recv_packet = new DatagramPacket(receivedata, receivedata.length);
Log.d("UDP", "S: Receiving...");
clientsocket.receive(recv_packet);
String rec_str = new String(recv_packet.getData());
tv.setText(rec_str);
Log.d(" Received String ",rec_str);
InetAddress ipaddress = recv_packet.getAddress();
int port = recv_packet.getPort();
Log.d("IPAddress : ",ipaddress.toString());
Log.d(" Port : ",Integer.toString(port));
}
} catch (Exception e) {
Log.e("UDP", "S: Error", e);
}
}
}
If you are using the emulator you may need setup redirects, remember the emulator is behind a virtual router.
In other words, type these commands in;
telnet localhost 5554
redir add udp:9876:9876
and try again.
Used Port numbers
Create Datagram packet
try {
mDataGramSocket = new DatagramSocket(Config.PORT_NUMBER);
mDataGramSocket.setReuseAddress(true);
mDataGramSocket.setSoTimeout(1000);
} catch (SocketException e) {
e.printStackTrace();
}
Call below function through AsyncTask
Create Function to receive infinitely
public void receive() {
String text;
byte[] message = new byte[1500];
DatagramPacket p = new DatagramPacket(message, message.length);
try {
while (true) { // && counter < 100 TODO
// send to server omitted
try {
mDataGramSocket.receive(p);
text = new String(message, 0, p.getLength());
// If you're not using an infinite loop:
//mDataGramSocket.close();
} catch (SocketTimeoutException | NullPointerException e) {
// no response received after 1 second. continue sending
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
// return "error:" + e.getMessage();
mReceiveTask.publish("error:" + e.getMessage());
}
// return "out";
}