3

I have two classes,one sender class and the other is the receiver class.Both of the sending and receiving apps stops after few seconds and close down. My sender class is :

    public class MainActivity extends Activity {
InetAddress receiverAddress;
DatagramSocket datagramSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    try {
        datagramSocket = new DatagramSocket(4444);
    } catch (SocketException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    byte[] buffer = "0123456789".getBytes();
    byte[] address="192.168.1.101".getBytes();

    try {
        receiverAddress = InetAddress.getByAddress(address);
    } catch (UnknownHostException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    DatagramPacket packet = new DatagramPacket(
            buffer, buffer.length, receiverAddress, 4444);

    try {
        datagramSocket.send(packet);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }




}

My receiving or listening class is:

public class MainActivity extends Activity {
DatagramSocket datagramSocket;
DatagramPacket packet;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv1=(TextView)findViewById(R.id.textView1);
     try {
        datagramSocket = new DatagramSocket(80);
    } catch (SocketException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    byte[] buffer = new byte[10];
     packet = new DatagramPacket(buffer, buffer.length);

    try {
        datagramSocket.receive(packet);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    byte[] buff = packet.getData();

tv1.setText(buff.toString());

}

Thanks in advance for the help.

Ameer Humza
  • 83
  • 1
  • 3
  • 13

3 Answers3

5

In Android you're not allowed to execute Network operations on the UIThread (Main-Thread)

To Fix this: Copy your network-code to a new Thread and let it run.

Sven
  • 51
  • 1
  • 2
1

The port numbers in the "new DatagramSocket(...)" calls look weird. The client should create an "unbound" socket - simply use "new DatagramSocket();". The sender should bind to the port that the client sends to, i.e. "new DatagramSocket(4444);".

sleinen
  • 511
  • 3
  • 10
0

Source and destination port number should be same. Give same numbers in "DatagramSocket(xxx)". xxx must be same in both programs.

  • Almost but not quite. The port bind of the sender is irrelevant. In the code above, the sender is sending to port 4444 and the receiver is listening on port 80. None of this explains why the applications stop after a few seconds. The receiver should block indefinitely waiting for a packet that never arrives. Unless someone is pointing a web browser at it... – AlastairG May 03 '14 at 08:37