WZ22HAO 2015年12月10日
UDP通信,消息传输的一个bug
a发给b str1 b接受到str1
b发给a str2 a接收到的是str1
b再发给a str3 a接受到str2
b发给a str4 a接受到str3

目前代码已经改好,但是不理解之前的问什么会出错
修改的方法:把UDP传送的报文分开定义,分别是msgPin和msgPout
注释掉的是原本有问题的代码

谢谢大家


//Client.java
import java.awt.BorderLayout;
import java.awt.Container;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;

public class Client extends JFrame implements ActionListener, Runnable {
private static final long serialVersionUID = 1L;
final static int PC = 6000;
final static int DESTINATION = 6001;
final static String HOST = "127.0.0.1";
final static int BUFFER_SIZE = 10000;
String str;
JPanel p;
JButton btnSend;
JTextField msgSend;
JTextArea msgShow;
//DatagramPacket msgP;
DatagramPacket msgPin;
DatagramPacket msgPout;
DatagramSocket skt;
InetAddress address;
byte msg[] = new byte[BUFFER_SIZE];
Thread thread;

Client() {
super("I am B");
p = new JPanel();
btnSend = new JButton("发送数据");
msgSend = new JTextField(20);
msgShow = new JTextArea(10, 10);
p.add(msgSend);
p.add(btnSend);
Container con = getContentPane();
con.setLayout(new BorderLayout());
con.add(p, BorderLayout.NORTH);
con.add(new JScrollPane(msgShow), BorderLayout.CENTER);
btnSend.addActionListener(this);
thread = new Thread(this);
thread.start();
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {
str = msgSend.getText().trim();
if (str.length() != 0) {
try {
str = msgSend.getText();
msg = str.getBytes();
address = InetAddress.getByName(HOST);
//msgP = new DatagramPacket(msg, msg.length, address, DESTINATION);
//skt.send(msgP);
msgPout = new DatagramPacket(msg, msg.length, address, DESTINATION);
skt.send(msgPout);
msgSend.setText("");
} catch (IOException e1) {
System.out.println("获取服务器失败");
}
}
msgSend.setText("");
}

public void run() {
try {
//msgP = new DatagramPacket(msg, msg.length, address, PC);
msgPin = new DatagramPacket(msg, msg.length, address, PC);
skt = new DatagramSocket(PC);
} catch (Exception e) {
e.printStackTrace();
}
while (true) {
if (skt == null) {
break;
} else {
try {
//skt.receive(msgP);
//str = new String(msgP.getData(), 0, msgPin.getLength());
skt.receive(msgPin);
str = new String(msgPin.getData(), 0, msgPin.getLength());
msgShow.append("收到数据" + str + "\n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) throws IOException {
new Client();
}
}


//Server.java
import java.awt.BorderLayout;
import java.awt.Container;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;

public class Server extends JFrame implements ActionListener, Runnable {
private static final long serialVersionUID = 1L;
final static int PC = 6001;
final static int DESTINATION = 6000;
final static String HOST = "127.0.0.1";
final static int BUFFER_SIZE = 10000;
String str;
JPanel p;
JButton btnSend;
JTextField msgSend;
JTextArea msgShow;
DatagramPacket msgPin;
DatagramPacket msgPout;
DatagramSocket skt;
InetAddress address;
byte msg[] = new byte[BUFFER_SIZE];
Thread thread;

Server() {
super("I am A");
p = new JPanel();
btnSend = new JButton("发送数据");
msgSend = new JTextField(20);
msgShow = new JTextArea(10, 10);
p.add(msgSend);
p.add(btnSend);
Container con = getContentPane();
con.setLayout(new BorderLayout());
con.add(p, BorderLayout.NORTH);
con.add(new JScrollPane(msgShow), BorderLayout.CENTER);
btnSend.addActionListener(this);
thread = new Thread(this);
thread.start();
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {
str = msgSend.getText().trim();
if (str.length() != 0) {
try {
str = msgSend.getText();
msg = str.getBytes();
address = InetAddress.getByName(HOST);
//msgP = new DatagramPacket(msg, msg.length, address, DESTINATION);
//skt.send(msgP);
msgPout = new DatagramPacket(msg, msg.length, address, DESTINATION);
skt.send(msgPout);
msgSend.setText("");
} catch (IOException e1) {
System.out.println("获取服务器失败");
}
}
msgSend.setText("");
}

public void run() {
try {
//msgP = new DatagramPacket(msg, msg.length, address, PC);
msgPin = new DatagramPacket(msg, msg.length, address, PC);
skt = new DatagramSocket(PC);
} catch (Exception e) {
e.printStackTrace();
}
while (true) {
if (skt == null) {
break;
} else {
try {
//skt.receive(msgP);
//str = new String(msgP.getData(), 0, msgPin.getLength());
skt.receive(msgPin);
str = new String(msgPin.getData(), 0, msgPin.getLength());
msgShow.append("收到数据" + str + "\n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) throws IOException {
new Server();
}
}
...全文
65 点赞 收藏 1
写回复
1 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Java SE
创建于2007-09-28

3.4w+

社区成员

30.7w+

社区内容

Java 2 Standard Edition
社区公告
暂无公告