为什么我的端口源随着我发送的每条 UDP 消息而变化?

问题描述 投票:0回答:1

我正在 Wireshark 上跟踪从 Android 手机发送的 UDP 消息,但每次发送消息时源端口号都会发生变化。

所以我有两个问题:

  1. 如果我想收到回复消息,这会很糟糕吗?或者会发现,只是每条收到的消息都来自不同的端口?

  2. 如果 1)的答案是肯定的,这很糟糕,那么我应该做什么来改变它? 这是我的代码:

编辑:完整代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public static final int SERVERPORT = 1111;
    public static final String SERVER_IP = "255.255.255.255";

    private LinearLayout msgList;
    private EditText edMessage;
    private int clientTextColor;

    private ClientThread clientThread;
    private Thread thread;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        msgList = findViewById(R.id.msgList);
        edMessage = findViewById(R.id.edMessage);
        clientTextColor = ContextCompat.getColor(this, R.color.colorAccent);
        handler = new Handler();
    }

    
    // Just for displaying messages on device 
    public TextView textView(String message, int color) {...}

    public void showMessage(final String message, final int color) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                msgList.addView(textView(message, color));
            }
        });
    }

    // Implementation

    @Override
    public void onClick(View view) {

        if(view.getId() == R.id.clear) {
            msgList.removeAllViews();
        }

        if (view.getId() == R.id.send_data) {
            // Starting thread
            clientThread = new ClientThread();
            thread = new Thread(clientThread);
            thread.start();

            String clientMessage = edMessage.getText().toString().trim(); // Client's Message
            showMessage(clientMessage, Color.BLUE); // Just display
            if (null != clientThread) {
                clientThread.sendMessage(clientMessage + "\r\n");
            }
        }
    }

    class ClientThread implements Runnable {
            byte[] buffer = new byte[1024];

            @Override
            public void run() {
                try {
                    while (true) {
                        DatagramSocket ds = new DatagramSocket(SERVERPORT);
                        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
                        ds.receive(dp);
                        String serverMsg = new String(dp.getData(), 0, dp.getLength());

                        showMessage("Server: " + serverMsg, clientTextColor);
                        ds.close();
                    }
                } catch (UnknownHostException e1) {
                    e1.printStackTrace();
                }
            }

        void sendMessage(final String message) { // Called by "Send Data" button
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        byte[] msg = message.getBytes();
                        InetAddress ip = InetAddress.getByName(SERVER_IP);
                        DatagramSocket socket = new DatagramSocket();
                        DatagramPacket packet = new DatagramPacket(msg, msg.length, ip, SERVERPORT);
                        socket.setBroadcast(true);
                        socket.send(packet);
                    } catch(Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    String getTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(new Date());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != clientThread) {
            clientThread.sendMessage("Disconnect");
            clientThread = null;
        }
    }
}
java android networking udp
1个回答
0
投票

在代码中,每次迭代都会创建一个新套接字,这将打开一个新端口。这就是多个客户端与服务器并行通信的方式。

  1. 大多数服务器会捕获您的IP地址和端口,因此它将正确响应您的套接字,但您必须确保您的套接字对象持续足够长的时间才能观察到。
  2. 坏取决于目标。只要您需要在该进程中使用该套接字与服务器通信,就创建一个打开的套接字。您不需要打开另一个套接字,因为您正在发起一条新消息。
© www.soinside.com 2019 - 2024. All rights reserved.