Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use UDP sockets in android?

Tags:

java

android

udp

I am trying to use UDP sockets in a android, here I send my string from android emulator and receive that by my Java program on PC, but my Java program does not receive anything, although when I used Java program as both client and server (I made two different Java programs) it worked.

This is my android main activity :

public class First extends Activity {
Button b;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Thread t = new Thread(new Second());
            t.start();
        }
    });

Here is my second class in android :

    public class Second implements Runnable {
Second()
{
    run();
}
public void run() {
    // TODO Auto-generated method stub
    try {

        String messageStr = "Hello Android!";
        int server_port = 9876;
        DatagramSocket s = new DatagramSocket();
        InetAddress local = InetAddress.getByName("127.0.0.1");
        int msg_length = messageStr.length();
        byte[] message = messageStr.getBytes();
        DatagramPacket p = new DatagramPacket(message, msg_length, local,
                server_port);
        s.send(p);
    } catch (Exception e) {

    }
}
}

This is my Java code on PC:

    public static void main(String args[]) throws Exception
    {
            DatagramSocket serverSocket = new DatagramSocket(9876);
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
            while(true)
            {
                    DatagramPacket receivePacket = new     DatagramPacket(receiveData,receiveData.length);
                    serverSocket.receive(receivePacket);
                    String sentence = new String(receivePacket.getData(),0,receivePacket.getLength());
                    InetAddress IPAddress = receivePacket.getAddress();
                    int port = receivePacket.getPort();
                    System.out.println("MESSAGE RECEIVED  "+sentence+"  "+IPAddress+"         "+port);
            }
     }
like image 955
Mr_Hmp Avatar asked Sep 29 '12 11:09

Mr_Hmp


People also ask

How do UDP sockets work?

UDP is a very simple protocol. Messages, so called datagrams, are sent to other hosts on an IP network without the need to set up special transmission channels or data paths beforehand. The UDP socket only needs to be opened for communication. It listens for incoming messages and sends outgoing messages on request.

What is UDP socket connect?

Datagram (UDP) sockets. The CONNECT command enables an application to associate a socket with the socket name of a peer. The socket then is considered to be a connected UDP socket. You can call the CONNECT command multiple times with different peer names to change the socket association.

What is UDP socket API?

User Datagram Protocol (UDP) UDP is a simple transport-layer protocol. The application writes a message to a UDP socket, which is then encapsulated in a UDP datagram, which is further encapsulated in an IP datagram, which is sent to the destination.

Are UDP sockets bidirectional?

Datagram sockets allow processes to use UDP to communicate. A datagram socket supports bidirectional flow of messages. A process on a datagram socket can receive messages in a different order from the sending sequence and can receive duplicate messages. Record boundaries in the data are preserved.


1 Answers

On your Android emulation (and Android device), 127.0.0.1 means the Android emulation machine, not the host PC. You can access your host at 10.0.2.2

like image 196
zmbq Avatar answered Sep 29 '22 17:09

zmbq