Android UDP接收pc机发送消息,接收不到?

雪凝花 发布于 2013/05/13 13:39
阅读 1K+
收藏 0

@叶儿 你好,想跟你请教个问题: pc机广播消息,android udp接收不到,是什么原因?

加载中
0
尐帥鱼
尐帥鱼
把360和金山都卸载后试一试
0
雪凝花

不好用,希望哪位大侠能给段源码

0
灰花走湿
灰花走湿

建议贴出你的错误代码


0
雪凝花
就是接收不到pc机发送来的消息
手机接收端源码:
TextView resive;
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  
        resive=(TextView)findViewById(R.id.server_textView);
        for(int i=0;i<10;i++)
        { 
           startUDPClient();
        try 
        {
Thread.sleep(1000, 0);//延时

        catch (InterruptedException e) 

   e.printStackTrace();
}
        } 
    } 
    private void startUDPClient()
    {
    try { 
MulticastSocket socket = new MulticastSocket(1235); 
InetAddress group = InetAddress.getByName("192.168.18.26"); 
socket.joinGroup(group); 
DatagramPacket packet; 
    byte[] buf = new byte[4456]; 
    packet = new DatagramPacket(buf, buf.length); 
    socket.receive(packet); 
    String received = new String(packet.getData()); 
    System.out.println("Quote of the Moment: " + received); 
    System.out.println("Ip:" + packet.getAddress().getHostName());
    System.out.println("Port:" + packet.getPort()); 
    resive.setText(received);
socket.leaveGroup(group); 
socket.close(); 

    catch (IOException e) 
    {
// TODO Auto-generated catch block
e.printStackTrace();


0
s
swping
IP地址和端口对吗?
返回顶部
顶部