2015-05-11 85 views
4

我想從我的android手機4.2(客戶端)使用WIFI連接發送一個UDP消息到PC(服務器)。我的手機和PC通過無線路由器連接。但是從手機到手機沒有收到任何消息。我也測試了這個代碼,用於PC到PC連接的成功。我已將互聯網權限添加到manifest.xml。如果你能幫助我,我會很樂意的。謝謝。我已經添加了此權限。從android手機發送udp消息到PC(窗口)不起作用

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

客戶:

@Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 




     Button button1 = (Button) findViewById(R.id.button1); 
     final TextView tv = (TextView) findViewById(R.id.textView1); 
     final TextView tv2= (TextView) findViewById(R.id.textView2); 







     button1.setOnClickListener(new OnClickListener() 
     { 

      @Override 
      public void onClick(View v) 
      { 

       boolean morgan= isOnline(); 
       String s = String.valueOf(morgan); 
       tv.setText(s); 


       try{ 

        //InetAddress ipaddress = InetAddress.getByName("localhost"); 
        InetAddress ipaddress = InetAddress.getByName("192.168.10.11"); 
        int port = 6500; 
        //byte[] buffer = new byte[1024]; // empty byte array 
        String msg ="hello goooooooogle"; // send this message to the server 
        byte [] b_array = msg.getBytes(); 

        //on SERVER side DatagramSocket able to receive packets on 8080 port 
        DatagramPacket packet = new DatagramPacket(b_array, b_array.length, ipaddress, port);// DatagramPacket(byte[], byte_length, InetAddress, port_number) 
        DatagramSocket socket = new DatagramSocket(); 
        socket.send(packet); 
        socket.close(); 

       } 
       catch(Exception e) 
       { 
        System.out.println(e); 
       } 
       } 




     }); 
    } 

    public boolean isOnline() { 

     Runtime runtime = Runtime.getRuntime(); 
     try { 

      Process ipProcess = runtime.exec("/system/bin/ping -c 1 192.168.10.11"); 
      //Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8"); 
      int  exitValue = ipProcess.waitFor(); 
      return (exitValue == 0); 

     } 
     catch (IOException e)   
     { e.printStackTrace(); } 
      catch (InterruptedException e) { e.printStackTrace(); } 

     return false; 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) 
     { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

服務器

public class server 
{ 
    public static void main(String args[]) 
    { 

     try{ 
      System.out.println("aaa"); 
      byte[] inbuf = new byte[1000]; // default size 
      DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length); 

      DatagramSocket socket = new DatagramSocket(6500); 
      socket.receive(packet); 

      int numBytesReceived = packet.getLength(); 
      System.out.println(numBytesReceived); 
      String s = new String(inbuf); 
      System.out.println(s); 
      //System.out.println(inbuf[2]); 

      socket.close(); 
     } 
     catch(Exception e) 
     { 
      System.out.println(e); 
     } 
    } 
} 
+0

'/在SERVER端能夠在8080端口上接收數據包的DatagramSocket'。你的意思是說,如果你的Android客戶端連接到8080的所有作品? – greenapps

+0

你的應用崩潰了不是嗎?你爲什麼不告訴?你會在logcat中看到一個異常。請告訴我們。 – greenapps

+0

沒有崩潰。它工作正常。但在服務器(PC)端沒有收到數據包。我已經嘗試過8080,但沒有工作。 – user1850484

回答

1

當與Android設備上的網絡操作處理,建議使用一個單獨的線程來執行這樣的操作。因此,請嘗試將onClick()方法中的代碼隔離爲AsyncTask,以便在後臺運行它。

private class SendMessageTask extends AsyncTask<String, Void, Void> { 
protected Void doInBackground(String... ip) { 
    // run network socket code here 
    return null; 
} 
} 

隨後的onClick()將包含這樣的事情:

new SendMessageTask().execute("IP_HERE"); 

很明顯,你可以修改它以滿足您的需求。但是,如果您需要在應用的生命週期內發送更多數據,則可能需要使用自己的後臺線程。這裏有一個更詳細的解釋爲什麼網絡操作不能/不應該在UI線程中完成 - http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html

相關問題