android真机使用UDP协议向PC机发送数据报,PC端的服务器接受不到

如题,代码如下:
android上的客户端,忽略了Activity部分
public class LocationClient {
private final String remoteIP;//外网IP
private final int PORT = 28888;
private final int PACKET_SIZE = 4096;

private DatagramSocket dSocket;
private DatagramPacket inPacket;
private DatagramPacket outPacket;

public LocationClient(String remoteIP) {
    super();
    this.remoteIP = remoteIP;

    try {
        dSocket = new DatagramSocket();
    } catch (SocketException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    inPacket = new DatagramPacket(new byte[PACKET_SIZE], PACKET_SIZE);

    try {
        //处理remoteIP地址字符串
        String[] remoteIPSplit = remoteIP.split("\\.");
        byte[] remoteIPBuf = new byte[4];
        for(int i = 0; i < 4; i++){
            remoteIPBuf[i] = (byte)(Integer.parseInt(remoteIPSplit[i])&0xff);
        }

        outPacket = new DatagramPacket(new byte[PACKET_SIZE], 0,
                PACKET_SIZE, InetAddress.getByAddress(remoteIPBuf), PORT);

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

}

/**
 * 发送数据 MAX长度 = 4k
 */
public void sendData(String content) {

        outPacket.setData(content.getBytes());

        try {
            dSocket.send(outPacket);

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

}

public void close() {

    dSocket.close();

}

}
PC机上的服务器:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.swing.filechooser.FileNameExtensionFilter;

public class LocationServer {
private final int PORT = 28888;
private final int PACKET_SIZE = 4096;

private DatagramSocket dSocket;
private DatagramPacket inPacket;
private DatagramPacket outPacket;

public LocationServer() {
    super();

    try {

        dSocket = new DatagramSocket(PORT);

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

    inPacket = new DatagramPacket(new byte[PACKET_SIZE], PACKET_SIZE);

}

public void run(){

    while(true){

        try {

            System.out.println("~~~~~服务器启动~~~~~");

            dSocket.receive(inPacket);

            byte[] content = inPacket.getData();

            System.out.println(String.valueOf(content));

            new RecordManager(inPacket.getAddress().getHostAddress(), content).run();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            dSocket.close();
        }

    }

}

private class RecordManager implements Runnable{

    private String fileName;
    private byte[] addContent;

    public RecordManager(String fileName, byte[] addContent) {
        super();
        this.fileName = fileName;
        this.addContent = addContent;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

        File file = new File(fileName);

        try(RandomAccessFile rAccessFile = new RandomAccessFile(file, "w")){
            //指针跳到末尾
            rAccessFile.seek(file.length());

            //追加内容
            rAccessFile.write(addContent);


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

    }
}

public static void main(String[] args){
    new LocationServer().run();
}

}

pc在 内网吧 也许。

建议你先在 PC端 使用TCP/UDP调试工具,看看能不能接收到数据?
如果不能,则说明是客户端发送的问题;如果能,说明是服务器端接收的问题。

1。确定二者在同一网络,或者他们即使不在同一网络但可以间接路由
2。关闭PC端防火墙
3。如果手机访问公网IP,需在路由器设置端口映射,同时需确保网络运营商没有封禁端口

看下是不是防火墙拦截了,还是pc端的程序有问题,这种程序,不要一上来就写上一大段,先从基本的通讯写起,先保证连上,能传个字符也行,然后逐步完善。

首先抓包看有没有发出来