android 94字节,android - 无法在Android应用上接收UDP字节 - 堆栈内存溢出

我正在编写一个android应用程序,以接收正在网络的广播地址上广播的数据包(这已经过测试,并且该数据包确实得到广播,并且也在“ UDP Sender / Receiver”应用程序中得到接收。)我无法让我的应用程序将其拾起并告诉我它存在。 这些设备位于同一网络上,并且发送设备的代码有效且专有。 这是该应用程序的基本DatagramSocket代码。

package com.ti.cc3x.android;

import java.io.IOException;

import java.net.*;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

import android.widget.Toast;

public class buttonListener extends Activity {

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.listener);

final TextView txt = (TextView)findViewById(R.id.txt1);

new Thread( new Runnable(){

public void run(){

try {

String text = null;

int server_port = 12356;

byte[] message = new byte[66];

DatagramPacket p = new DatagramPacket(message, message.length);

DatagramSocket s = new DatagramSocket(server_port);

while(text == null){

s.receive(p);

text = new String(message, 0, p.getLength());

txt.setText("Messed up.");

}

if(text != null){

Toast.makeText(buttonListener.this, text, Toast.LENGTH_LONG).show();

txt.setText("Received");

s.close();

}

}

catch (SocketException se) {

se.printStackTrace();

Toast.makeText(buttonListener.this, "Socket Error", Toast.LENGTH_LONG).show();

txt.setText("Socket Error");

}

catch (IOException ioe) {

ioe.printStackTrace();

Toast.makeText(buttonListener.this, "Network Error", Toast.LENGTH_LONG).show();

txt.setText("Network Error");

}

}

}).start();

}}

任何帮助表示赞赏,谢谢!

更新的代码:

package com.ti.cc3x.android;

import java.io.IOException;

import java.net.*;

import java.util.Arrays;

import android.app.Activity;

import android.content.Context;

import android.net.wifi.WifiManager;

import android.net.wifi.WifiManager.MulticastLock;

import android.os.Bundle;

import android.util.Log;

import android.widget.TextView;

import android.widget.Toast;

public class buttonListener extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.listener);

WifiManager wifi = (WifiManager)

getSystemService(Context.WIFI_SERVICE);

WifiManager.MulticastLock lock = wifi.createMulticastLock("Log_Tag");

final TextView txt = (TextView) findViewById(R.id.txt1);

lock.acquire();

new Thread( new Runnable(){

public void run(){

try {

String text = null;

int server_port = 12356;

byte[] message = new byte[66];

DatagramPacket p = new DatagramPacket(message, message.length);

DatagramSocket s = new DatagramSocket(server_port);

//while(text == null){

s.receive(p);

text = new String(message, 0, p.getLength());

txt.setText("Messed up.");

//}

//if(text != null){

Toast.makeText(buttonListener.this, text, Toast.LENGTH_LONG).show();

txt.setText("Received");

s.close();

//}

}

catch (SocketException se) {

se.printStackTrace();

Toast.makeText(buttonListener.this, "Socket Error", Toast.LENGTH_LONG).show();

txt.setText("Socket Error");

}

catch (IOException ioe) {

ioe.printStackTrace();

Toast.makeText(buttonListener.this, "Network Error", Toast.LENGTH_LONG).show();

txt.setText("Network Error");

}

}

}).start();

lock.release();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值