在Android中同时使用Datagramsocket发送和接收 - 只需发送?

时间:2014-07-16 16:41:08

标签: java android send datagram

我在一个Thread中使用DatagramSocket接收并在另一个Thread中将数据发送到PC(Java)。但它只是每1秒发送一次数据但是没有收到。 但是当我在发送代码后将接收代码放在同一个线程中时......它的工作原理...... 但我想每秒发送一次数据并同时等待任何数据...

编辑:我发现问题是我无法在两个不同的线程中打开一个端口。现在我的问题是,如何每1秒取消DatagramSocket.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();
    }
}
}

1 个答案:

答案 0 :(得分:4)

我找到了答案: 我使用了两个不同的套接字,一个用于通过端口发送数据,另一个用于从同一端口接收数据。 (两个不同的线程) 但第一个应该连接到该端口,第二个应该绑定到该端口。 一切都运作良好。