-1

I'm trying to make an Android app that's able to send a message to a computer and receive one from it. It's pretty basic. The thing is, I have accomplished this through multicasting, although not exactly. My app is able to receive messages from the computer (which uses a java application I made to receive and send the messages). But, when I try to send a message from the device to the computer, the message doesn't arrive to the computer. I mean, to the application.

Both the desktop app and the Android app use the same Client - Server classes. This is what gets me so confused. Because, as I am using the same classes, why does it work one way but not the other? I just don't no.

The desktop app runs on windows.

Also, when the Android app receives a message, it receives it the following way: "Message 1���������������������������..." when the message should be received: "Message 1". I don't know if this could be relevant.

The code is the following:

Server Class:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class MulticastSocketServer implements Runnable{

    final static String INET_ADDR = "224.0.0.3";
    final static int PORT = 8888;
    static String msg;

    public MulticastSocketServer(String message) throws UnknownHostException, InterruptedException {
        msg = message;
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        // Get the address that we are going to connect to.
        InetAddress addr = null;
        try {
            addr = InetAddress.getByName(INET_ADDR);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Open a new DatagramSocket, which will be used to send the data.
        try (DatagramSocket serverSocket = new DatagramSocket()) {

            msg += "\\0";

            for (int i = 0; i < 10; i++) {
                // Create a packet that will contain the data
                // (in the form of bytes) and send it.
                DatagramPacket msgPacket = new DatagramPacket(msg.getBytes(),
                        msg.getBytes().length, addr, PORT);
                serverSocket.send(msgPacket);

                System.out.println("Server sent packet with msg: " + msg);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            serverSocket.disconnect();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Client Class:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
import java.util.Timer;
import java.util.TimerTask;

public class MulticastSocketClient implements Runnable {

    final static String INET_ADDR = "224.0.0.3";
    final static int PORT = 8888;
    Connection360 conn;

    public MulticastSocketClient (Connection360 connection) throws UnknownHostException {
        conn = connection;
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        try{
            // Get the address that we are going to connect to.
            InetAddress address = InetAddress.getByName(INET_ADDR);

            // Create a buffer of bytes, which will be used to store
            // the incoming bytes containing the information from the server.
            // Since the message is small here, 256 bytes should be enough.
            byte[] buf = new byte[256];

            // Create a new Multicast socket (that will allow other sockets/programs
            // to join it as well.
            try (final MulticastSocket clientSocket = new MulticastSocket(PORT)){
                //Joint the Multicast group.
                clientSocket.joinGroup(address);

                System.out.println("Connected");

                //while (true) {
                    // Receive the information and print it.
                    DatagramPacket msgPacket = new DatagramPacket(buf, buf.length);
                    Timer timer = new Timer("tmr");
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            clientSocket.disconnect();
                        }
                    },10000);
                    clientSocket.receive(msgPacket);

                    String msg = new String(buf, 0, buf.length);
                    System.out.println("Socket 1 received msg: " + msg.substring(0, msg.indexOf("\\0")));
                    conn.MessageReceived(msg.substring(0, msg.indexOf("\\0")));
                    clientSocket.disconnect();
                //}
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            }catch (UnknownHostException ex){

            }
    }
}

This classes are the ones I made for the desktop app. The classes I made for the Android app are the same, but I had to change the System.out.println() to Log.v(). As for the rest, it's exactly the same.

So, if you happen to know what could be happening, I would really appreciate your assistance with the topic.

Thank you!

blastervla
  • 539
  • 6
  • 19
  • Could we at least see some code? – Roberto Anić Banić Jul 24 '15 at 00:45
  • Yes. Show some code. It seems to be an encoding incompatibility. Try setting characters sets while sending and receiving to UTF-8 – Shreyas Chavan Jul 24 '15 at 00:48
  • I have added the code. Sorry for that. I forgot the most important part. I'll try setting the encoding, but I don't know if it will work. Because the messages don't even arrive at the desktop application. It doesn't display anything at all when I send a message. I don't know if it still may be a trouble caused by the encoding. I'll check that as soon as I can. Thank you for the quick response – blastervla Jul 24 '15 at 00:57

1 Answers1

0

When you read the incoming packet, you don't use its size but the size of the buffer instead:

String msg = new String(buf, 0, buf.length);
// should be:
String msg = new String(buf, 0, msgPacket.getLength());
// or even better:
String msg = new String(msgPacket.getData());

If the incoming packet is shorter, the rest of the buffer contains random data which is what you got. Java strings are not NUL-terminated so msg.indexOf("\\0") does not work.

StenSoft
  • 9,369
  • 25
  • 30
  • I'll change that now. Thank you! I used the ´msg.indexOf("\\0")´ so as to trim the message myself, as I couldn't figure out what was happening. Glad you helped. As of the problem with the desktop app, do you have an idea of what could be happening? – blastervla Jul 24 '15 at 01:45
  • @blastervla Maybe firewall? – StenSoft Jul 24 '15 at 01:54
  • Maybe. I'll try that and see what I accomplish. Thanks! – blastervla Jul 25 '15 at 00:52
  • Okay. It was the firewall which was blocking the connection. I have set up my windows firewall manually to allow the communication. But, if my program were to be installed, do you have any idea how could I set this up programatically? Or, ask for permission upon installation, for example? Thanks for everything! – blastervla Jul 25 '15 at 02:53