【问题标题】:UDP on Android emulatorAndroid 模拟器上的 UDP
【发布时间】:2013-01-03 05:16:55
【问题描述】:

我正在尝试在两个不同系统上运行在 android 模拟器上的客户端程序和服务器之间建立一个简单的 UDP 连接。服务器端很好,但客户端总是崩溃。是模拟器的问题吗?我应该重定向端口以使其工作吗?

客户端(在安卓模拟器上):

package com.example.clientrecv;   
import java.io.IOException;  
import java.net.DatagramPacket;  
import java.net.DatagramSocket;  
import java.net.SocketException;  
import android.os.Bundle;  
import android.app.Activity;  
import android.util.Log;  
import android.widget.Button;  
import android.widget.Toast;  


public class MainActivity extends Activity  
{  
public String text;  
public int serverport=1234;  
public byte[] message=new byte[1000];  
public Button b;  
public DatagramPacket p;  
public DatagramSocket s;  
public Toast t;  

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b=(Button) findViewById(R.id.button1);

                try {
                    p = new DatagramPacket(message,message.length);
                    s = new DatagramSocket(serverport); 
                    try {
                        s.receive(p);
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    text= new String(message,0,p.getLength());
                    Log.d("hello","the message:"+text);
                    s.close();
                // TODO Auto-generated method stub



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

}   
}
                public void showmsg()
                {
                    t=Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG);
                    t.show();
                }  
}  


SERVER SIDE: (on pc)  


import java.io.*;  
import java.net.*;  
class serversend  
{  
public static void main(String args[]) throws Exception  
  {
String strmsg="Server says hello";
int serverport=1234;
int len=strmsg.length();
System.out.println("starting");
byte[] message=strmsg.getBytes();
try{
InetAddress local=InetAddress.getByName("localhost");
DatagramSocket s=new DatagramSocket();
DatagramPacket p=new DatagramPacket(message,len,local,serverport); 
System.out.println("Running");
s.send(p);
System.out.println("Sent");
}catch(Exception e)
{
  System.out.println("caught");
} 
  }  
}  

【问题讨论】:

  • 你定义了客户端和服务器通信的端口吗?
  • 是的。但我不知道如何让它沟通。有可能吗?
  • 我什至尝试使用以下命令重定向它: telnet localhost 5554 redir add udp::

标签: android proxy udp client-server


【解决方案1】:

有许多示例可以帮助您使用 UDP 在服务器和客户端之间进行通信 这两者之间的通信应该在服务器端添加客户端端口

InetAddress local = InetAddress.getByName("192.168.1.102");

这里是使用 UDP 进行客户端服务器通信的以下链接 LINK UDP1 Link UDP2

【讨论】:

  • 这些应该在客户端,对吧?并且该代码适用于带有 java 的普通 UDP。但我无法在模拟器上运行它。
  • 您浏览给定的链接了吗?它没有显示适当的结果吗?任何崩溃警告?如果是这样,请在您的问题中添加日志猫
  • 是的,我通过了。模拟器说“应用程序已停止”
  • 引起:java.lang.ClassNotFoundException:你在清单文件中定义了你的类吗?
  • 我试图在这里粘贴我的代码。但它想要满足太多的格式规范。
【解决方案2】:

Android 3.0 之后的版本不允许您在主 UI 线程中实现网络操作。你必须为此定义一个新的线程......这是你可以做到的:

public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

          //here you Set up you parameters and launch the thread (e.g):

          this.newThread = new Thread(new mThread());
      this.newThread.start();


      /* Next you define your newThread's run method in wich all networking 
         operations must take place*/


   class mThread implements Runnable {
        public void run() {

      // Do all networking tasks you need                                               

           }
        }   

【讨论】:

    猜你喜欢
    • 2017-12-19
    • 2011-12-30
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    相关资源
    最近更新 更多