2015-11-10 196 views
0

我有一個android應用程序,通過WIFI發送和接收UDP數據包。 應用程序通過發送UDPpacket將數據發送到WIFI調制解調器,然後調制解調器響應應用程序。 我的應用程序完美髮送數據,但不幸的是,我無法從調制解調器接收數據並將其顯示在我的屏幕上。發送和接收數據報套接字上的UDP數據包在android

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(); 
    } 
    } 

回答

0

我想你很早以前就已經解決了你的問題。但是你的問題似乎和我一樣。問題是我將套接字綁定爲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); 

BTW:爲了測試,如果我收到我的平板電腦應答分組正確我用的應用程序:「UDP發送器/接收器」