套接字 - SocketException:套接字已关闭 - Java

时间:2014-07-18 17:43:56

标签: java sockets

我最近一直在玩插座,但我遇到了一个问题...... 当我从服务器接收数据时,我收到“java.net.SocketException:socket closed”异常。我没有在任何地方关闭套接字,事实上,我使用close()的唯一地方就是在扫描程序上从System.in读取文本;

这是我的代码:

客户端:

package packets.sidedcomputer;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

import packets.MessagePacket;
import packets.sender.PacketSender;
import packets.side.Side;

public class Client extends SidedComputer
{
    volatile boolean finished = false;

    volatile String username;

    volatile Server server;

    public Socket clientSocket;

    public ClientReciever reciever;

    public Client(Server server, String username) throws UnknownHostException, IOException
    {
        this.username = username;
        this.server = server;
        this.reciever = new ClientReciever(this);
    }

    public void stopClient()
    {
        finished = true;
    }

    @Override
    public void run()
    {
        Scanner scanner = new Scanner(System.in);

        reciever.start();

        while(!finished)
        {
            try 
            {
                this.clientSocket = new Socket("192.168.1.25", 10501);

                String line;

                while((line = scanner.nextLine()) != null)
                {     
                    PacketSender sender = new PacketSender();

                    System.out.println("Client sending message \"" + line + "\" to server");
                    sender.sendPacket(new MessagePacket(line, username), clientSocket);
                }
            }
            catch (Exception e) 
            {
                e.printStackTrace();
            }

        }

        scanner.close();
    }

    @Override
    public Side getSide() 
    {
        return Side.CLIENT;
    }
}

服务器:

package packets.sidedcomputer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import packets.Packet;
import packets.data.PacketData;
import packets.info.ClientInfo;
import packets.reciever.PacketReciever;
import packets.sender.PacketSender;
import packets.side.Side;

public class Server extends SidedComputer
{
    volatile boolean finished = false;

    public ServerSocket serverSocket;

    public volatile List<ClientInfo> clients = new ArrayList<ClientInfo>();

    public void stopServer()
    {
        finished = true;
    }

    public Server()
    {
        try 
        {
            serverSocket = new ServerSocket(10501);
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void run()
    {
        try 
        {
            while (!finished)
            {
                Socket clientSocket = serverSocket.accept();  

                if(clientSocket != null)
                {
                    ClientInfo clientInfo = new ClientInfo(clientSocket);

                    this.clients.add(clientInfo);

                    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

                    String dataString;

                    while((dataString = in.readLine()) != null)
                    {
                        PacketReciever packetReciever = new PacketReciever();

                        PacketData packetData = new PacketData();

                        packetData.decodeInto(dataString);

                        Packet packet = packetReciever.recievePacket(packetData, packetData.packetID, getSide(), clientSocket.getLocalAddress().getHostAddress().toString(), clientSocket.getLocalPort() + "");

                        PacketSender packetSender = new PacketSender();

                        for (ClientInfo client : this.clients)
                        {
                            PrintWriter out = new PrintWriter(client.socket.getOutputStream(), true);
                            packetSender.sendPacketToClient(packet, out);
                        }
                    }
                }
            }
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            System.exit(1);
        }
    }

    @Override
    public Side getSide() 
    {
        return Side.SERVER;
    }
}

Packet Sender:

package packets.sender;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

import packets.Packet;
import packets.data.PacketData;

public class PacketSender implements IPacketSender
{
    @Override
    public void sendPacket(Packet packet, Socket socket)
    {
        if(packet.getDefualtID() == 0)
        {
            PacketData packetData = new PacketData(packet.getDefualtID());

            packet.writeData(packetData);

            String data = packetData.encodeIntoString();

            sendData(socket, data);
        }
    }

    protected void sendData(Socket socket, String data)
    {
        try 
        {

            try 
            (
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            ) 
            {
                out.println(data);
            } 
            catch (IOException e) 
            {
                System.err.println("Couldn't get I/O for the connection to " + socket);
                System.exit(1);
            } 
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    public void sendPacketToClient(Packet packet, PrintWriter out)
    {
        PacketData packetData = new PacketData(packet.getDefualtID());

        packet.writeData(packetData);

        String data = packetData.encodeIntoString();

        out.println(data);
    }
}

客户接收方:

package packets.sidedcomputer;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import packets.data.PacketData;
import packets.reciever.PacketReciever;
import packets.side.Side;

public class ClientReciever extends Thread
{
    public Client client;

    public ClientReciever(Client client)
    {
        this.client = client;
    }

    volatile boolean running = true;

    public void stopRunning()
    {
        running = false;
    }

    @Override
    public void run()
    {
        while(running)
        {
            if(client.clientSocket != null)
            {
                try 
                {
                    BufferedReader in = new BufferedReader(new InputStreamReader(client.clientSocket.getInputStream()));
                    String line;

                    while((line = in.readLine()) != null)
                    {
                        PacketReciever reciever = new PacketReciever();

                        PacketData packetData = new PacketData();
                        packetData.decodeInto(line);

                        reciever.recievePacket(packetData, packetData.packetID, Side.CLIENT, client.clientSocket.getLocalAddress().getHostAddress().toString(), client.clientSocket.getPort() + "");
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }

        }
    }
}

数据包接收器:

package packets.reciever;

import packets.Packet;
import packets.MessagePacket;
import packets.data.PacketData;
import packets.side.Side;

public class PacketReciever implements IPacketReciever
{
    @Override
    public Packet recievePacket(PacketData packetData, int id, Side side, String hostName, String port) 
    {
        Packet packet = null;

        if(id == 0)
        {
            packet = new MessagePacket();

            packet.readData(packetData);

            packet.execute(side, hostName + ":" + port);
        }

        return packet;
    }
}

1 个答案:

答案 0 :(得分:3)

我认为问题在于发件人中的try-with-resources调用,它会在try块的末尾调用close(),从而关闭Socket。尝试在PrintWriter的所有来电中使用一个sendData()