gpt4 book ai didi

android - Android上的UDP客户端

转载 作者:行者123 更新时间:2023-12-03 11:53:13 25 4
gpt4 key购买 nike

我已经在Wi-Fi演示板上制作了UDP服务器,并使用android App(UDP发送器)对其进行了测试。
我在Android上创建了自己的UDP客户端应用程序,但是它不起作用。

我已经很好地创建并配置了套接字端口和IP地址,但该应用程序无法正常工作,我也不知道为什么。

PS:在 list 中,我添加了使用权限以访问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

最佳答案

我找到了解决问题的方法

问题出在 list 上

这是我的新代码和 list

最主要的我叫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();
}
}

list :

<?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>

我在 list 中添加了部分代码

希望将来能对某人有所帮助;)

关于android - Android上的UDP客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26016954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com