Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send and receive using Datagramsocket simultaneously in Android - Just send?

I am using DatagramSocket in one Thread to receive and in another Thread to send data to PC (Java). but it just send data every 1 second but does not receive. But when I put receive code in the same Thread after sending code ... it works ... but I want to send data each one second and simultaneously wait for any data ...

Edit: I have found out that the problem is that I can not open a port in two different Thread. Now My question is that how can I cancel DatagramSocket.Receive() every 1 second and send my data and again go back to receive?

public class MainActivity extends ActionBarActivity {

TextView status;
GPSTracker gps;
boolean started = false;
boolean waitting = false;
String mess = "Waiting ...";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    status = (TextView) findViewById(R.id.status);

    gps = new GPSTracker(MainActivity.this);

    Timer time = new Timer(); // Instantiate Timer Object
    ScheduledTask st = new ScheduledTask(); 
    time.schedule(st, 0, 1000); // Create Repetitively task for every 1 secs
}

public class SendThread implements Runnable {
    private DatagramSocket mySocket;

    public void run() {
        try {
            InetAddress receiverHost = InetAddress.getByName("192.168.0.1");
            int receiverPort = Integer.parseInt("8080");
            String message = "Lat:0-Long:0";
            if (gps.canGetLocation()) {
                double latitude = gps.getLatitude();
                double longitude = gps.getLongitude();
                message = "Lat:" + latitude + "-Long:" + longitude;
            } else {
                gps.showSettingsAlert();
            }
            mySocket = new DatagramSocket();
            byte[] sendBuffer = message.getBytes();
            DatagramPacket packet = new DatagramPacket(sendBuffer,
                    sendBuffer.length, receiverHost, receiverPort);
            mySocket.send(packet);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public class RecieveThread implements Runnable {
    public void run() {
        try {
            Log.i("1", "1");
            DatagramSocket mySocket = new DatagramSocket(8080);
            waitting = true;
            // to receive a message
            int MESSAGE_LEN = 60;
            byte[] recvBuffer = new byte[MESSAGE_LEN];
            DatagramPacket datagram = new DatagramPacket(recvBuffer,
                    MESSAGE_LEN);
            Log.i("2", "2");
            mySocket.receive(datagram);
            Log.i("Recieved", "Recieved");
            String recvdString = new String(recvBuffer);
            mess = recvdString;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    status.setText(mess);
                }
            });
            mySocket.close();
            waitting = false;
        } catch (Exception e) {
            waitting = false;
            e.printStackTrace();
        }
    }
}

public class ScheduledTask extends TimerTask {
    public void run() {
        if (!waitting) {
            Thread rThread = new Thread(new RecieveThread());
            rThread.start();
        }
        Thread sThread = new Thread(new SendThread());
        sThread.start();
    }
}
}
like image 388
user3157047 Avatar asked Jul 16 '14 16:07

user3157047


1 Answers

I have found the answer: I have use two different socket, One for sending data through the port and another one to receive data from the same port. (two different Thread) but the first one should connect to that port and second one should bind to that port. and everything works well.

like image 78
user3157047 Avatar answered Sep 28 '22 13:09

user3157047