繁体   English   中英

Android模拟器上的UDP

[英]UDP on Android emulator

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

客户端(在Android模拟器上):

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");
} 
  }  
}  

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

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

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

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                                               

           }
        }   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM