问题描述
我能够通过正确的UDP套接字发送我的数据,但是当我收到的数据是不断在接收命令等待我不知道是什么原因造成这
请看看下面
我能够从Android设备服务器端正确recive数据,但是当我从服务器端发送数据给Android设备没有收到。但是当我从服务器发送数据到任何其他客户端例如PC应用它接收并显示rpoperly数据
类任务实现Runnable {
@覆盖
公共无效的run(){
尝试{
字符串messageStr =饲料;
INT SERVER_PORT = 8888;
InetAddress类本地= InetAddress.getByName(10.0.2.2);
INT msg_length = messageStr.length();
字节[]消息= messageStr.getBytes();
DatagramSocket的S =新的DatagramSocket();
// DatagramPacket类P =新的DatagramPacket(消息,msg_length,地方,SERVER_PORT);
s.send(对); //正确能够发送数据。我接收数据到服务器 的for(int i = 0; I< = 20;我++){
最终int值=我;
消息=新的字节[30000]
P =新的DatagramPacket(消息,message.length);
s.receive(P); //不断等候在这里,但我发送的数据从服务器返回,但是不会接收
最后一个字节[]数据= p.getData();;
尝试{ 视频下载(1000);
}赶上(InterruptedException的E){
e.printStackTrace();
}
handler.post(新的Runnable(){
@覆盖
公共无效的run(){
progressBar.setProgress(值);
imageView.setImageBitmap(BitmapFactory.de codeByteArray的(数据,0,data.length));
}
});
}
}
赶上(异常前)
{ }
}
}
在Eclipse文档:
The "s.receive(p);
" command blocks the thread until it receices data or the timeout set with setSoTimeout(timeout) is over.
I have made 2 classes to make my communication happen.First UDP-Server:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
public class UDP_Server
{
private AsyncTask<Void, Void, Void> async;
private boolean Server_aktiv = true;
@SuppressLint("NewApi")
public void runUdpServer()
{
async = new AsyncTask<Void, Void, Void>()
{
@Override
protected Void doInBackground(Void... params)
{
byte[] lMsg = new byte[4096];
DatagramPacket dp = new DatagramPacket(lMsg, lMsg.length);
DatagramSocket ds = null;
try
{
ds = new DatagramSocket(Main.SERVER_PORT);
while(Server_aktiv)
{
ds.receive(dp);
Intent i = new Intent();
i.setAction(Main.MESSAGE_RECEIVED);
i.putExtra(Main.MESSAGE_STRING, new String(lMsg, 0, dp.getLength()));
Main.MainContext.getApplicationContext().sendBroadcast(i);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (ds != null)
{
ds.close();
}
}
return null;
}
};
if (Build.VERSION.SDK_INT >= 11) async.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else async.execute();
}
public void stop_UDP_Server()
{
Server_aktiv = false;
}
}
I send the received data to an BroadcastReceiver and there you can do what ever you want to with the data.
And now my client to send the data. In this code i send a broadcast, but i think it will be no problem to change the code for sending to a direct IP or something.
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Build;
public class UDP_Client
{
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
{
ds = new DatagramSocket();
DatagramPacket dp;
dp = new DatagramPacket(Message.getBytes(), Message.length(), Main.BroadcastAddress, Main.SERVER_PORT);
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();
}
And here is how you instantiate the classes from your main class.
//start UDP server
Server = new UDP_Server();
Server.runUdpServer();
//UDP Client erstellen
Client = new UDP_Client();
And here how to send a message with the client.
//Set message
Client.Message = "Your message";
//Send message
Client.NachrichtSenden();
To stop the UDP_Server, just set Server.Server_aktiv to false.
To set the message above u can also write a "setMessage(String message)" methode or something like that.
I hope this will help you =).And at last sorry for my bad english. :D
这篇关于发送和接收数据的UDP套接字的java的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!