本机模拟运行 android与PC机通信;pc上服务是在jdk下用java server命令运行的。
android客户端点击发送按钮程序并无报错,服务器就是死活接受不到数据?
若是将Server.java和Client.java代码都自行编译后放在jdk中运行则通信成功.
服务端代码:
import java.net.DatagramPacket;
import java.net.DatagramSocket;public class Server implements Runnable{ private static final int PORT = 6000;
private byte[] buff = new byte[4096];
private boolean bool = true;
public Server(){}

public boolean isBool() {
return bool;
} public void setBool(boolean bool) {
this.bool = bool;
}
public static void main(String[] args) {
new Thread(new Server()).start();
}
public void run() {
// TODO Auto-generated method stub
DatagramSocket socket = null;
DatagramPacket inPacket = new DatagramPacket(buff,buff.length);
try{
socket = new DatagramSocket(PORT);
System.out.println("开启服务器.");
String rec = "";
while(!rec.equals("quit")){
                                     System.out.println("等待接受数据:");
socket.receive(inPacket);
rec = new String(inPacket.getData());
System.out.println("msg server received: "+rec);
}
System.out.println("关闭服务器.");
socket.close();
}catch(Exception e ){
e.printStackTrace();
}finally{
if(socket!=null)
socket.close();
}
}

}
客户机代码:package packages.udp;import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;public class Client {
private static final int SERVER_PORT = 6000;  
private DatagramSocket dSocket = null;  
  
    private String msg;  
    public Client(String msg) {  
        super();  
        this.msg = msg;  
    }  
    
    public String  send(){  
        StringBuilder sb=new StringBuilder();  
        InetAddress local = null;  
        try {  
            local = InetAddress.getByName("localhost");    
             sb.append("已找到服务器,连接中...").append("/r/n");  
        } catch (UnknownHostException e) {  
            sb.append("未找到服务器.").append("/r/n");  
            e.printStackTrace();  
        }  
        try {  
            dSocket = new DatagramSocket(); 
             sb.append("正在连接服务器...").append("\\n");  
        } catch (SocketException e) {  
            e.printStackTrace();  
            sb.append("服务器连接失败.").append("/n");  
        }  
        int msg_len = msg == null ? 0 : msg.length();  
        DatagramPacket dPacket = new DatagramPacket(msg.getBytes(), msg_len,  
                local, SERVER_PORT);  
        try {  
            dSocket.send(dPacket);  
            System.out.println("send");   //此行代码控制台有打印出
            sb.append("消息发送成功!").append("/r/n");  
        } catch (IOException e) {  
            e.printStackTrace();  
            sb.append("消息发送失败.").append("/r/n");  
        }  
        dSocket.close();  
        return sb.toString();  
    }  }Activity:
package packages.space;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;import packages.space.R;
import packages.udp.Client;
import packages.udp.Server;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;public class Activity_udp extends Activity{ private EditText msg_et=null;  
private Button send_bt=null;  
private TextView info_tv=null;   @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activityudp);

msg_et=(EditText)findViewById(R.id.txt_content);  
        send_bt=(Button)findViewById(R.id.btn_send1);  
        info_tv=(TextView)findViewById(R.id.view_udp);
        send_bt.setOnClickListener(new BtnSendOnClickListener()); }
 
class BtnSendOnClickListener implements OnClickListener{ public void onClick(View v) {
Client client=new Client(msg_et.getText().toString());  
            info_tv.setText(client.send());  
}
}}
AndroidManifest.xml配置:<uses-permission android:name="android.permission.INTERNET"/>