Socket closed错误

几乎相同的代码,运行后出现了不同的效果:

代码1:

接收消息:

package com.wzz.test;

import java.io.*;
import java.net.*;

public class Service {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(8800);

        while (true){
            //准备接收包裹
            byte[] container = new byte[1024];
            DatagramPacket packet = new DatagramPacket(container, 0, container.length);
            socket.receive(packet);//阻塞式接收包裹

            //断开连接 bye
            String receiveData = new String(packet.getData(), 0, packet.getLength());
            System.out.println(receiveData);
            if (receiveData.equals("bye")){
                break;
            }
        }
        socket.close();
    }
}

发送消息:

package com.wzz.test;

import com.sun.xml.internal.bind.v2.util.ByteArrayOutputStreamEx;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.net.*;

public class TestThread {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(9900);

        //准备数据,控制台读取 System.in
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        while (true){
            String data = reader.readLine();
            byte[] datas = data.getBytes();
            DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, InetAddress.getByName("127.0.0.1"), 8800);

            socket.send(packet);
            if (data.equals("bye")){
                break;
            }
        }
        socket.close();
    }
}

结果:

代码2:

接收消息:

package com.wzz.A04网络编程.B05UDPChart;

import java.io.*;
import java.net.*;

public class C02UdpReceive01 {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(8800);

        while (true){
            //准备接收包裹
            byte[] container = new byte[1024];
            DatagramPacket packet = new DatagramPacket(container, 0, container.length);
            socket.receive(packet);//阻塞式接收包裹

            //断开连接 bye
            String receiveData = new String(packet.getData(), 0, packet.getLength());
            System.out.println(receiveData);

            if (receiveData.equals("bye")){
                break;
            }
            socket.close();
        }
    }
}

发送消息:

package com.wzz.A04网络编程.B05UDPChart;

import java.io.*;
import java.net.*;

public class C01UdpSender01 {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(9900);

        //准备数据,控制台读取 System.in
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        while (true){
            String data = reader.readLine();
            byte[] datas = data.getBytes();
            DatagramPacket packet = new DatagramPacket(datas,0,datas.length, InetAddress.getByName("127.0.0.1"),8800);

            socket.send(packet);
            if (data.equals("bye")){
                break;
            }
        }
        socket.close();
    }
}

结果:

原因分析:

socket 只要在 io流close的情况下 自动关闭,意思就是你想边发送边接受最正确的方式就是发送和 接受的操作都做完之后 再一起关闭IO流 完美解决。在代码2的接收消息中提前将socket关闭,导致Socket closed。

解决方法:

将代码socked.close();移出while(ture)循环。

posted @ 2021-10-22 16:32  隔岸稻花香  阅读(5060)  评论(0编辑  收藏  举报