在android中的数据报套接字上发送和接收UDP数据包

时间:2015-11-10 09:23:52

标签: android sockets udp thread-safety android-wifi

我有一个Android应用程序,通过WIFI发送和接收UDP数据包。 应用程序通过发送UDPpacket将数据发送到WIFI调制解调器,然后调制解调器响应应用程序。 我的应用程序完美地发送数据,但遗憾的是我无法从调制解调器接收数据并将其显示在屏幕上。

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class UdpConnectionWIFImodemActivity extends Activity {
    final String strNetworkIP = "192.168.0.0";
    final int intUDP_Port=8080;
    private int sourceport=0;
    class SocketListener implements Runnable
    {
        String str;
        public void run()
        {
            DatagramSocket socket;
            DatagramPacket packet;
        byte[] buf = new byte[256];
    System.out.println("Thread running");

        if (sourceport!=0) {

            try
            {

            socket = new DatagramSocket(sourceport);

                while (true)
                {
                    final TextView t = (TextView) findViewById(R.id.textView1);
                                                            packet = new DatagramPacket(buf, buf.length);
                    socket.receive(packet);
                    System.out.println("Received packet");
                    String s = new String(packet.getData());
                    CharSequence cs = t.getText();
                    str = cs + "\r\n" + s;
                    t.post(new Runnable()
                    {                   
                        public void run()
                        {
                                    t.setText(str);

                        }
                    }
                    );
                }
            }

            catch (IOException e)

            {
                Log.e(getClass().getName(), e.getMessage());
            }
                    }

        }

    }




    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = (TextView) findViewById(R.id.textView1);
        Button send1 = (Button) findViewById(R.id.button1);
        send1.setOnClickListener(new OnClickListener()
                {

            public void onClick(View v)
            {

                String s = "1RPMONgetinfo";

                try
                {
                final DatagramSocket socket = new DatagramSocket();

                byte[] buf = new byte[256];
                                buf = s.getBytes();
                    InetAddress address = InetAddress.getByName(strNetworkIP);
                    final DatagramPacket packet = new DatagramPacket(buf, buf.length, address, intUDP_Port);
                    new Thread()
                    {
                        public void run()
                        {
                            try
                            {
                                System.out.println("About to send message");
                                socket.send(packet);
                                sourceport  =   socket.getLocalPort();

                                System.out.println("Sent message");
                                                        }

                            catch (IOException e1)

                            {
                                // TODO Auto-generated catch block
                                                            e1.printStackTrace();
                            }
                    socket.close();
                        }
                    }.start();
                }

                catch (SocketException e1) {
                }

                catch (UnknownHostException e2) {
                }
            }

        });

        Thread t = new Thread(new SocketListener());

        t.start();
    }
    }

1 个答案:

答案 0 :(得分:0)

我猜你很久以前就已经解决了你的问题。但你的问题似乎和我一样。问题是我将Socket绑定为false。 这就是它对我有用的方式:

InetSocketAddress inetSocketAddress = new InetSocketAddress(InetAddress.getByName(*own IP address as String*), *own portnumber you want to listen to*);
Datagramsocket socket = new DatagramSocket(null);
socket.setReuseAddress(true);
socket.bind(inetSocketAddress);
byte[] message = new byte[4096];
DatagramPacket packet = new DatagramPacket(message, message.length);
socket.receive(packet);

顺便说一句:要测试我是否正确地在平板电脑上收到了应答包,我使用了应用程序:“UDP发送者/接收者”