cari

Rumah  >  Soal Jawab  >  teks badan

Protokol Pengangkutan Rangkaian - Saya menulis perisian sembang ringkas dalam Java menggunakan UDP, tetapi saya sentiasa tidak boleh menerima mesej?

Menulis perisian sembang ringkas dalam Java menggunakan UDP, tetapi saya tidak dapat menerima sebarang mesej. Sila beritahu saya apa yang salah dengan kod tersebut.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

<code>package com.window;

 

import javax.swing.*;

import java.awt.*;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.io.IOException;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.logging.Logger;

 

/**

 * Created by young on 2017/4/19.

 */

public class MainWindow {

 

    private JLabel lb_addIp;

    private JButton btn_add;

    private JPanel jp_qq;

    private JLabel lb_selectIP;

    private JComboBox<String> cb_selectIP;

    private JTextField tf_addip;

    private JTextArea ta_charHistory;

    private JTextField tf_input;

    private JButton btn_submit;

    private JLabel lb_charHistory;

    private JLabel lb_message;

    private JTextPane tp;

 

    private String ip;

    private String sendMessage;

 

    private int sendport = 10023;

 

    private int receviceport = 10024;

 

    private DefaultListModel<String> listModel = new DefaultListModel<String>();

    public String name;

    private List<String> messageList = new ArrayList<>();

 

    public MainWindow() {

 

        btn_add.addMouseListener(new MouseAdapter() {

            @Override

            public void mouseClicked(MouseEvent e) {

                ip = tf_addip.getText();

                Object[] com = new Object[]{ip};

                cb_selectIP.addItem(ip);

            }

        });

        btn_submit.addMouseListener(new MouseAdapter() {

            Logger logger = Logger.getLogger("submit");

 

            @Override

            public void mouseClicked(MouseEvent e) {

                if (ip == null) {

                    lb_message.setForeground(Color.red);

                    lb_message.setText("请输入IP。");

                    return;

                }

                try {

                    InetAddress inetAddress = InetAddress.getByName(ip);

                    if (!inetAddress.isReachable(2000)) {

                        lb_message.setForeground(Color.red);

                        lb_message.setText("无法链接该地址!");

                        return;

                    }

                } catch (IOException e1) {

                    e1.printStackTrace();

                }

 

                sendMessage = tf_input.getText();

 

                logger.info("sendMessage" + sendMessage);

 

 

                new Thread(() -> {

                    send(sendMessage);

                    lb_message.setForeground(Color.green);

                    lb_message.setText("发送成功。");

                }).start();

                new Thread(() -> {

                    receive();

                }).start();

            }

        });

 

    }

 

    public void receive() {

        DatagramSocket datagramSocket = null;

        try {

            datagramSocket = new DatagramSocket(sendport, InetAddress.getByName(ip));

            byte[] buf = new byte[1024];

            DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);

            datagramSocket.receive(datagramPacket);

            String id = datagramPacket.getAddress().getHostAddress();

            int port = datagramPacket.getPort();

            byte[] date = datagramPacket.getData();

            System.out.println(Arrays.toString(date));

            String mes = ip + ":" + new String(date) + "\n";

            //System.out.println("ip=" + id + "port=" + port);

            tp.setText(tp.getText() + mes);

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            assert datagramSocket != null;

            datagramSocket.close();

        }

    }

 

    public void initWindow() {

        JFrame frame = new JFrame("QQ");

        frame.setContentPane(new MainWindow().jp_qq);

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        frame.pack();

        frame.setVisible(true);

        tp.setEditable(false);

    }

 

    public void send(String message) {

        DatagramSocket datagramSocket = null;

        try {

            datagramSocket = new DatagramSocket(receviceport);

            byte[] buf = message.getBytes();

            DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getByName(ip), sendport);

            datagramSocket.send(packet);

            tp.setText(tp.getText() + name + message + "\n");

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            datagramSocket.close();

        }

    }

 

}

</code>

Terdapat banyak kod, jadi saya meletakkannya di github
alamat github

迷茫迷茫2789 hari yang lalu813

membalas semua(1)saya akan balas

  • 高洛峰

    高洛峰2017-05-17 10:09:59

    Anda melaksanakan penghantaran dan penerimaan dalam dua urutan masing-masing. Ini menimbulkan masalah Anda boleh melakukan penerimaan dahulu dan kemudian menghantar, atau mungkin untuk melakukan penerimaan semasa proses penghantaran. Fenomena yang berlaku ialah kadangkala data boleh diterima, tetapi kadangkala tidak.

    balas
    0
  • Batalbalas