Android Open Source - MessengerApp Udp Client






From Project

Back to project page MessengerApp.

License

The source code is released under:

MIT License

If you think the Android project MessengerApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.myfirstapp;
//from   w w  w.  jav  a  2  s. co  m
import java.net.*;
import java.io.*;

public class UdpClient implements Runnable {
    private TcpSender ts;
    
    @Override
    public void run() {
      DatagramSocket getSocket = null;
        while (!Thread.interrupted()) {
            try {
              getSocket = new DatagramSocket(UdpBroadcaster.PORT);
                getSocket.setSoTimeout(100);
              getSocket.setBroadcast(true);

                byte[] buf = new byte[1024];

                DatagramPacket getPacket = new DatagramPacket(buf, buf.length);

                getSocket.receive(getPacket);
                
                System.out.println("Received broadcast from " + getPacket.getAddress());
                ts.addReceiver(getPacket.getAddress());

            } catch (UnknownHostException e) {
              System.out.println("Host unknown");
        e.printStackTrace();
      } catch (SocketException e) {
        e.printStackTrace();
        System.out.println("Failed to open socket");
      } catch(SocketTimeoutException e) {
        // Ignore
      } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Failed to received packet");
      } finally {
        if(getSocket != null) {
          getSocket.close();
          getSocket = null;
        }
      }
        }
    }

    public UdpClient(TcpSender ts) {
        this.ts = ts;
    }
}




Java Source Code List

com.example.myfirstapp.MainActivity.java
com.example.myfirstapp.TcpReceiverWorker.java
com.example.myfirstapp.TcpReceiver.java
com.example.myfirstapp.TcpSender.java
com.example.myfirstapp.UdpBroadcaster.java
com.example.myfirstapp.UdpClient.java