2

i am using the below code to send text message between two android devices but i could not receive the packet at the receiver side
the sender:

   String messageStr="Hello Android!";
          Log.d("note","message prepaered");
          int server_port = 12345;
          try{
              Log.d("note","socket prepaered");
          DatagramSocket s = new DatagramSocket();
              Log.d("note","socket defined");
          InetAddress local = InetAddress.getByName(ip.getText().toString());
          int msg_length=messageStr.length();
          byte[] message = messageStr.getBytes();
              Log.d("note","converting message to bytes");
          DatagramPacket p = new DatagramPacket(message, msg_length,local,server_port);
          s.send(p);
              Log.d("note","sending msg");}
          catch (SocketException e){
              Log.d("error",e.getMessage());
          }
          catch(IOException v1){
              Log.d("error", v1.getMessage());
          } 

the receiver :

 String text;
          int server_port = 12345;
          byte[] message = new byte[1500];
          try{
          DatagramPacket p = new DatagramPacket(message, message.length);
              Log.d("note","putting msg in packet");
          DatagramSocket s = new DatagramSocket(server_port);
              Log.d("note","defining socket");
          s.receive(p);
              Log.d("note","recieving packet");
          text = new String(message, 0, p.getLength());
          msg.setText(text);
          s.close();
          }
          catch (SocketException e){
              Log.d("error",e.getMessage());
          }
          catch(IOException v1){
              Log.d("error", v1.getMessage());
          }

thanks in advance for help

lena
  • 89
  • 1
  • 1
  • 12
  • Not an answer but a caveat: Are planning to use that loopback only? If you want to sent UDP over mobile networks you might have a lot of trouble with NAT and proxies. – eckes Feb 16 '15 at 20:38

1 Answers1

0

Ok. There are a lot of undefined variables in your story. First of all, you should try this at a local machine, where you can be sure that this is not a network problem. Second, try to wrap the receiving code in a endless loop, it will make the debugging easier. If by now, you found out that your code is actually working on a localhost, you should check your network configuration on your devices, make sure you are using the right addresses, and both devices are in the same network.

If you have trouble making your code work on a localhost, try to test your server and client independently on a working software, like http://sourceforge.net/projects/sockettest/ for example, it will allow you to make sure that your client and server code is working

Also don't forget about the timing, start your receiver first.

P.S. Please format your code properly.

Rofgar
  • 318
  • 3
  • 12
  • first of all thanks for replay , second how should i test the code on local machine if you have a way i will be thankful??? – lena Feb 17 '15 at 06:01
  • Sure, it is really easy. You can send a package from your local machine to your local machine. Just run your receiver as a Java application from a command line. Simply create an app and put your code into the "main" method. While it is running, do the same with your sender. Don't forget to use 127.0.0.1 as the IP address, it is the address of your local machine. – Rofgar Feb 17 '15 at 08:39
  • thanks when i run the receiver first it solve the problem but i want to ask you if i want to send voice not text should i change any thing in the code – lena Feb 18 '15 at 05:04
  • No, voice is just a array of bytes. All you have to do, is convert the voice to bytes on the sender. – Rofgar Feb 18 '15 at 08:26
  • but the voice that i record is a array of short[] not bytes and i think it is not acceptable to transfer array of a short using DatagramPacket – lena Feb 20 '15 at 16:12
  • Everything is an array of bytes (BTW, strange that your result is an array of shorts, what do you use to record the voice data?) You can easily convert that short[] array into byte array to transfer it, and on the receiver convert it back to short[] or whatever you need from those bytes. Check out the examples in http://stackoverflow.com/questions/19584885/converting-short-array-to-byte-array-in-java – Rofgar Feb 21 '15 at 08:54