2014-09-24 86 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