- 1 //客户端,接收方
- 2 3 import java.awt. * ;
- 4 import java.io. * ;
- 5 import java.net. * ;
- 6 import java.util. * ;
- 7 import javax.swing. * ;
- 8 9 public class IClient extends JFrame {
- 10 JPanel jp = new JPanel();
- 11 JTextArea jta = new JTextArea();
- 12 JScrollPane jsp = null;
- 13 14 public IClient() 15 {
- 16 Toolkit t = Toolkit.getDefaultToolkit();
- 17 Dimension Size = t.getScreenSize();
- 18 int width = Size.width;
- 19 int height = Size.height;
- 20 setLocationByPlatform(true);
- 21 this.setAlwaysOnTop(true);
- 22 jsp = new JScrollPane(jta);
- 23 this.setTitle("接收方");
- 24 this.add(jp);
- 25 this.setSize(500, 400);
- 26 this.setBounds((width - 500) / 2, 27(height - 400) / 2, 600, 500);
- 28 this.add(jsp, BorderLayout.CENTER);
- 29 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- 30 this.setVisible(true);
- 31
- }
- 32 public static void main(String[] args) throws UnknownHostException,
- IOException,
- InterruptedException {
- 33 new IClient().Receive();
- 34
- }
- 35 36 public void Receive() throws UnknownHostException,
- IOException,
- InterruptedException 37 {
- 38 Socket client = new Socket("localhost", 9000);
- 39 DataOutputStream dos = new DataOutputStream(client.getOutputStream());
- 40 DataInputStream dis = new DataInputStream(client.getInputStream());
- 41 int length = dis.read(); //读取接收数据帧的长度
- 42 //System.out.println(length); //输出要接受的数据的长度
- 43
- for (int i = 0; i) 44 {
- 45 char c = dis.readChar(); //接收数据帧,判断是否正确
- 46
- if (c != 'η') { //CRC数据帧校验正确
- 47 Random ran = new java.util.Random();
- 48 int re = ran.nextInt(13);
- 49
- if (re >= 4) { //数据帧帧数正确
- 50
- if (i) 51 {
- 52
- if (jta.getText() == null || "".equals(jta.getText())) {
- 53 jta.append("Receive:" + "成功接收" + (i + 1) + "号帧,内容为:" + c);
- 54
- } else {
- 55 jta.append("\r\nReceive:" + "成功接收" + (i + 1) + "号帧,内容为:" + c);
- 56
- }
- 57 //System.out.println("成功接收"+(i+1)+"号帧,内容为:"+c); //输出接受到的数据帧
- 58 dos.writeInt(i + 2);
- 59
- }
- 60
- else 61 {
- 62
- if (jta.getText() == null || "".equals(jta.getText())) {
- 63 jta.append("Receive:" + "成功接收" + (i + 1) + "号帧,内容为:" + c);
- 64
- } else {
- 65 jta.append("\r\nReceive:" + "成功接收" + (i + 1) + "号帧,内容为:" + c);
- 66
- }
- 67 68 //System.out.println("成功接收"+(i+1)+"号帧,内容为:"+c);
- 69 dos.writeInt(i + 2); //向发送方返回确认帧
- 70 Thread.sleep(100);
- 71
- }
- 72
- }
- 73
- else //数据帧内容错误
- 74 {
- 75
- if (jta.getText() == null || "".equals(jta.getText())) {
- 76 jta.append("Receive:" + "不是所需要的帧,丢弃等待!");
- 77
- } else {
- 78 jta.append("\r\nReceive:" + "不是所需要的帧,丢弃等待!");
- 79
- }
- 80 Thread.sleep(600);
- 81 //System.out.println("不是所需要的帧,丢弃等待!");
- 82 dos.writeInt( - 1);
- 83 i = i - 1;
- 84
- }
- 85
- }
- 86
- else if (c == 'η') //CRC数据帧错误丢弃等待
- 87 {
- 88
- if (jta.getText() == null || "".equals(jta.getText())) {
- 89 jta.append("Receive:" + "数据帧错误!");
- 90
- } else {
- 91 jta.append("\r\nReceive:" + "数据帧错误!");
- 92
- }
- 93 //System.out.println("数据帧错误!");
- 94 i = i - 1;
- 95 dos.writeInt( - 2);
- 96
- }
- 97
- }
- 98
- if (jta.getText() == null || "".equals(jta.getText())) {
- 99 jta.append("接收信息成功!");
- 100
- } else {
- 101 jta.append("\r\n" + "接收信息成功!");
- 102
- }
- 103 jta.setEnabled(false);
- 104 //System.out.println("接收信息成功!");
- 105 client.close();
- 106
- }
- 107
- }
来源: http://www.bubuko.com/infodetail-2004949.html