2014-09-24 123 views
0

我在Wi-Fi演示板上製作了一個UDP服務器,並使用和Android應用程序(UDP發送器)進行了測試。 我在Android上創建了自己的UDP客戶端應用程序,但它不起作用。機器人上的UDP客戶端

我創建並配置好套接字端口和IP地址,但該應用程序不起作用,我不知道爲什麼。

PS:在清單我增加了用途,權限訪問到的Wi-Fi

她是我的簡單的代碼

import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.InetAddress; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class Udp_Client extends Activity { 


    /** Called when the activity is first created. */ 
    TextView txt5,txt1; 
    byte[] send_data = new byte[1024]; 
    Button hello; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     txt1 = (TextView)findViewById(R.id.textView1); 
     txt5 = (TextView)findViewById(R.id.textView5); 

     hello = (Button) findViewById(R.id.button1); 






     hello.setOnClickListener(new View.OnClickListener(){    
      public void onClick(View v) {     


       try { 
        client(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } 

     }); 


    } 


    public void client() throws IOException{ 

      String str="Hello"; 
      int port = 50000; 
     DatagramSocket client_socket = new DatagramSocket(port); 
     InetAddress IPAddress = InetAddress.getByName("192.168.0.1"); 

         send_data = str.getBytes(); 


      DatagramPacket send_packet = new DatagramPacket(send_data,str.length(), IPAddress, port); 
      //client_socket.setBroadcast(true); 
      client_socket.send(send_packet);      


      client_socket.close(); 

     }  

} 

[1]:http://i.stack.imgur.com/aXPhf.png

+0

首先我看到的是,str.leng th和send_data字節數組的長度根據字符串的編碼而不同長度) – 2014-09-24 12:33:40

+0

即使當我添加連接(IP地址,端口) 感謝您的幫助 – 2014-09-24 13:44:39

回答

0

我發現這個問題

的問題是在清單

這是我的新代碼和解決方案我的清單

中至極主我稱之爲UDP_Client:

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 


public class MainActivity extends ActionBarActivity implements View.OnClickListener { 
    private TextView sceen = null; 
    private Button launch = null; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     sceen = (TextView)findViewById(R.id.textView1); 
     launch = (Button)findViewById(R.id.udpbutton); 
     launch.setOnClickListener(this); 

     launch.setOnClickListener((OnClickListener) this); 

    } 
      public void onClick(View v) { 
       //UDP Client erstellen 
       UDP_Client Client = new UDP_Client(); 
       Client.Message = "Your message"; 
        Client.NachrichtSenden(); 






      } 


    } 

我UDP_Client的代碼

import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.InetAddress; 

import android.annotation.SuppressLint; 
import android.os.AsyncTask; 
import android.os.Build; 

public class UDP_Client 
{ 

    private InetAddress IPAddress = null; 
    private String message = "Hello Android!" ; 
    private AsyncTask<Void, Void, Void> async_cient; 
    public String Message; 


    @SuppressLint("NewApi") 
    public void NachrichtSenden() 
    { 
     async_cient = new AsyncTask<Void, Void, Void>() 
     { 
      @Override 
      protected Void doInBackground(Void... params) 
      { 
       DatagramSocket ds = null; 

       try 
       { 
        byte[] ipAddr = new byte[]{ (byte) 192, (byte) 168,43, (byte) 157}; 
        InetAddress addr = InetAddress.getByAddress(ipAddr); 
        ds = new DatagramSocket(5000); 
        DatagramPacket dp;       
        dp = new DatagramPacket(Message.getBytes(), Message.getBytes().length, addr, 50000); 
        ds.setBroadcast(true); 
        ds.send(dp); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
       finally 
       { 
        if (ds != null) 
        { 
         ds.close(); 
        } 
       } 
       return null; 
      } 

      protected void onPostExecute(Void result) 
      { 
       super.onPostExecute(result); 
      } 
     }; 

     if (Build.VERSION.SDK_INT >= 11) async_cient.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 
     else async_cient.execute(); 
    } 
} 

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.udp_server" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="21" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

我添加的代碼部分在我的清單

希望這將幫助別人的未來;)

+0

2014-09-26 11:53:06

0

我想你錯過這個:

public void connect (InetAddress address, int port) 

DataGramSocket對象。

嘗試:

client_socket.connect(IPAddress, port); 
client_socket.send(send_packet); 
+0

我沒有測試它,我' m不是UDP Socket專家;) – 2014-09-24 12:39:26

+0

即使當我添加連接(IP地址,端口) – 2014-09-24 13:43:41

+0

時,它仍然無法正常工作。對不起,我只對API Docs有一個小小的評價,並認爲它。因爲我看不到您的客戶端和服務器之間的初始連接。 – 2014-09-24 13:44:49