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();
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With