Hello, i am learning from a tutorial in witch i can send messages by UDP protocol, the sample is working and i can send and receive messages. Now i want to make a listener for port 12345 to listen the messages received, maybe its not the best way but i do this with a handler...
LinearLayout main = new LinearLayout(IpClient.this);
main.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
text = new TextView(IpClient.this);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
text.setBackgroundResource(R.drawable.edit);
text.setText("INCOMING MESSAGES: \n ");
text.setTextColor(Color.BLACK);
Button b = new Button(IpClient.this);
b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
b.setText("receive");
main.addView(text);
main.addView(b);
setContentView(main);
final int server_port = 12345;
message = new byte[1500];
h = new Handler();
listen = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
p = new DatagramPacket(message, message.length);
try {
s = new DatagramSocket(server_port);
} catch (SocketException e1) {
}
try {
s.receive(p);
} catch (IOException e) {
}
String textIn = new String(message, 0, p.getLength());
//Toast.makeText(IpClient.this, "mesaj:" + textIn, Toast.LENGTH_SHORT)
// .show();
text.setText(text.getText() + "Received - " + textIn + "\n");
h.postDelayed(listen, 10);
}
};
h.postDelayed(listen, 100);
}
The problem is that when i run the app i need to receive a message to see the content view of the app -> the textView and the button. What is wrong and how can i fix this?
Thank you very much for the help!