0

嗨,我目前正在使用 DatagramSockets 在 Android 中制作一个项目,但我的编程并不是那么好。我正在制作一个注册页面。我的 android 设备使用用户名和收据代码等信息连接到电脑。我的电脑可以接收数据。当我的电脑收到数据时,我需要检查收据代码是否有效且未使用。然后,如果它是无效的、未使用的、使用的或有效的,我会发送给客户。问题是我的安卓设备无法接收到电脑发送的数据。我真的需要你们的帮助。目前我正在使用模拟器。我需要专家的帮助。这是我的代码:

安卓设备:

public class RegisterAsyncTask extends AsyncTask<String, Integer, String>{
    private DatagramSocket socket;
    private DatagramPacket p;
    private byte[] receiveData = new byte[1024];

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        try {
            InetAddress local = InetAddress.getByName("10.0.2.2");
            socket = new DatagramSocket();
            p = new DatagramPacket(params[0].getBytes(), params[0].length(),local,12345);
            socket.send(p);
            Log.d ("asd", "Packet sent");
            p = new DatagramPacket (receiveData, receiveData.length);
            Log.d ("asd", "Receiving packet");
            socket.receive(p);
            Log.d ("asd", "Packet received");
            String message = new String (p.getData(), 0 , p.getLength());
            Log.d ("asd", message);
            return message;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

个人电脑:

public class HandleAClient {

    private DatagramSocket socket;
    private DatagramPacket packet;
    private int server_port = 12345;
    private byte[] receiveData = new byte[1024];
    private String receiptCode;
    private InetAddress clientAddress;
    private String errorMessage;

    public HandleAClient (DatagramSocket socket, DatagramPacket packet) {
        //Initializes instance variables
        this.socket = socket;
        this.packet = packet;

    }
    public void registerClient () {
        try {
            String message = new String (packet.getData(),0,packet.getLength());
            Parser parser = new Parser();
            receiptCode = parser.getReceiptCode(message);
            System.out.println (receiptCode);
            System.out.println ("Message: " + message + " on " + packet.getAddress() + " on port "  + server_port);
            String cadd = packet.getAddress().toString();
            String newcadd = cadd.substring(1, cadd.length());
            System.out.println (newcadd);
            clientAddress = InetAddress.getByName(newcadd);

            if (!ControlVariables.db.checkReceiptCode(receiptCode)) {
                errorMessage = "Invalid Code";
                System.out.println (errorMessage);
                packet = new DatagramPacket(errorMessage.getBytes(), errorMessage.length(),clientAddress,12345);
                socket.send(packet);
                System.out.println ("error message send" + " to " +newcadd);
            }
            else {
                ReceiptCode rc = new ReceiptCode();
                rc = ControlVariables.db.getReceiptCode(receiptCode);
                if (rc.getStatus().equals("used")) {
                    errorMessage = "Used Code";
                    packet = new DatagramPacket(errorMessage.getBytes(), errorMessage.length(),clientAddress,12345);
                    socket.send(packet);
                }
                else {
                    System.out.println (receiptCode);
                    System.out.println ("Message: " + message + " on " + packet.getAddress() + " on port "  + server_port);
                    errorMessage = "Successful";
                    packet = new DatagramPacket(errorMessage.getBytes(), errorMessage.length(),clientAddress,12345);
                    socket.send(packet);
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
4

1 回答 1

0

由于我无法在您的帖子中添加评论,因此我会尝试这种方式。

读过那个模拟器不能使用真正的互联网连接。所以我不知道你如何将数据发送到PC。

请提供有关错误的更多信息或有关未收到数据包的信息。

于 2013-08-09T13:13:30.613 回答