先贴代码: 请耐心看看,有点多,但是简单,谢谢
package xiaobenben.muticast;import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.Scanner;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;public class Muticast extends Activity {
    /** Called when the activity is first created. */
   private Button button_ok;
   private TextView textview;
   private Button button_begin;
   private EditText edittext;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button_ok = (Button)findViewById(R.id.button_ok);
        button_begin = (Button)findViewById(R.id.button_begin);
        textview = (TextView)findViewById(R.id.textview_text);
        edittext = (EditText)findViewById(R.id.editview);
        textview.setText("hi,xiaobenben!");
        button_begin.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
try {
System.out.println("init() start");
new Multicast().init();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
        
        });
    }

class Multicast implements Runnable{
private static final String BROADCAST_IP = "230.0.0.1";
public static final int BROADCAST_PORT = 30000;
private static final int DATA_LEN = 4096;
private MulticastSocket socket = null;
private InetAddress broadcastAddress = null;
private Scanner scan = null;
byte[] inBuff = new byte[DATA_LEN];
private DatagramPacket inPacket = new DatagramPacket(inBuff , inBuff.length);
private DatagramPacket outPacket = null;
private String str; public void init()throws IOException
{
try
{
socket = new MulticastSocket(BROADCAST_PORT);
broadcastAddress = InetAddress.getByName(BROADCAST_IP);
socket.joinGroup(broadcastAddress);
socket.setLoopbackMode(false);
outPacket = new DatagramPacket(new byte[0] , 0 ,broadcastAddress , BROADCAST_PORT);
new Thread(this).start();
scan = new Scanner(edittext.getText().toString());
while(scan.hasNextLine())
{
byte[] buff = scan.nextLine().getBytes();
outPacket.setData(buff);
socket.send(outPacket);
}
}
finally
{
socket.close();
}
}
public void run(){
try
{
while(true)
{

socket.receive(inPacket);

System.out.println("聊天信息:" + new String(inBuff , 0 , inPacket.getLength()));
textview.setText("聊天信息:" + new String(inBuff , 0 , inPacket.getLength()));
System.out.println(inPacket.getAddress().getHostAddress());
}
}

catch (IOException ex)
{
ex.printStackTrace();
try
{
if (socket != null)
{

socket.leaveGroup(broadcastAddress);

socket.close();
}
System.exit(1); 
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}
每次程序点击开始按钮后,也就是发送信息后,就强制我退出了。
看了Android API的,是支持MulticastSocket的,看看那儿有问题,或者是需要在manifest里面加申明么?谢谢了

解决方案 »

  1.   

    类似的权限:
    <uses-permission android:name="android.permission.READ_SMS"></uses-permission>
    有在manifest.xml中加吗?
      

  2.   

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_SMS"></uses-permission>
    加了两条。。还是不行。。不知道为什么,
      

  3.   

    你的程序有问题,你看看,程序的逻辑是不是存在问题,一般force close 都是程序存在小错
      

  4.   

    我当时用Eclipse调试了下,程序在运行了 socket = new MulticastSocket(BROADCAST_PORT);之后,直接跳到了
               finally
                {
                socket.close();
                }
    然后就完了。按道理说那句话应该没错的吧
      

  5.   

    请问下KISS能不能把QQ给我,我吧工程给你,你帮我看看,谢谢了
      

  6.   

    你在DDMS中可以看到logcat的 没用过吗?
      

  7.   

    看到了,它说 thread exitting with uncaught exception(group = 0x4001aa28)
    Only the original thread that create a view hierarch can touch its views..莫非是我的线程写错了????
      

  8.   

    经过测试,就是这句话错了:
    textview.setText("聊天信息:" + new String(inBuff , 0 , inPacket.getLength()));
    这句话是内部类run()方法里面的,我原本想的是 在线程里面接收数据报,然后只在外部类定义的一个TextView里面显示,这样是不行的,那我该怎么来写这一句话呢?
      

  9.   

    //读取Socket中的数据,读到的数据放在inPacket所封装的字节数组里。textview.setText("聊天信息:" +msg);
    String msg = new String(inBuff , 0 , inPacket.getLength());
      

  10.   

    kisshujinwen1 问题解决了?
    我在Android手机上测试,收不到广播消息是怎么回事
      

  11.   

    你的线程好像都没启动
    new Thread(new Multicat()).start();
      

  12.   

     public void run(){
                try
                {
                while(true)
                    {
                    
                    socket.receive(inPacket);
                    
                    System.out.println("聊天信息:" + new String(inBuff , 0 , inPacket.getLength()));
                    textview.setText("聊天信息:" + new String(inBuff , 0 , inPacket.getLength()));
                    System.out.println(inPacket.getAddress().getHostAddress());
                    }
                }
            
            catch (IOException ex)
                {这里面出错了。textView.setText(...);应该放到主线程里面,你可以在子线程使用Handler和主线程通信
      

  13.   

    这么久了。。
    android里面,不允许通过子线程修改界面吧
      

  14.   

    第一,子线程中要用Handler或post方法传值给textView.setText(...)
    第二,别老只catchIOException...要catchException