Java客户端 - 服务器聊天应用程序获取java.net.SocketException:Socket关闭

时间:2017-10-01 23:17:05

标签: java multithreading sockets

我正在尝试为全局聊天创建一个简单的客户端 - 服务器应用程序从客户端退出连接时出现以下错误。

java.net.SocketException: Socket closed
at java.net.SocketInputStream.read(SocketInputStream.java:203)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.net.SocketInputStream.read(SocketInputStream.java:223)
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:337)
at java.io.DataInputStream.readUTF(DataInputStream.java:589)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at ReadFromServer.run(ChatClient.java:25)

当客户端崩溃时不使用Quit此错误

java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340)
at java.io.DataInputStream.readUTF(DataInputStream.java:589)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at Clients.run(ChatServer.java:34)

ChatServer.java

import java.util.*;
import java.io.*;
import java.net.*;
class Clients extends Thread 
{
private static ArrayList<DataOutputStream> clientOutputStreams;
private DataInputStream dataInputStream;
private DataOutputStream dataOutputStream;
private Socket socket;
static
{
    clientOutputStreams=new ArrayList<>();
}
Clients(Socket socket)
{
    try
    {
        this.socket=socket;
        this.dataInputStream=new DataInputStream(socket.getInputStream());
        this.dataOutputStream=new DataOutputStream(socket.getOutputStream());
        clientOutputStreams.add(dataOutputStream);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
public void run()
{
    try
    {
        try
        {
            String message=dataInputStream.readUTF();
            while(!message.equalsIgnoreCase("quit"))
            {
                for(DataOutputStream dis:clientOutputStreams)
                {
                    dis.writeUTF(message);
                }
                message=dataInputStream.readUTF();
            }
            Thread.currentThread().interrupt();
        }
        finally
        {
            dataInputStream.close();
            dataOutputStream.close();
            clientOutputStreams.remove(clientOutputStreams.indexOf(dataOutputStream));
            socket.close();
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
}
public class ChatServer
{
    public static void main(String[] args)throws Exception
    {
    try
    {
        ServerSocket serverSocket=new ServerSocket(9000);
        while(true)
        {
            Socket s=serverSocket.accept();
            Clients client=new Clients(s);
            client.start();
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
}

ChatClient.java

import java.util.*;
import java.io.*;
import java.net.*;
class ReadFromServer extends Thread
{
private DataInputStream readMessage;
ReadFromServer(Socket socket)
{
    try
    {
        this.readMessage=new DataInputStream(socket.getInputStream());
    }
    catch(Exception e)
    {
        e.printStackTrace();
        Thread.currentThread().interrupt();
    }
}
public void run()
{
    try
    {
        while(!Thread.currentThread().isInterrupted())
        {
            System.out.println(readMessage.readUTF());
        }
        readMessage.close();
        Thread.currentThread().interrupt();
        if(Thread.currentThread().isInterrupted())
            return;
    }
    catch(Exception e)
    {
        e.printStackTrace();
        Thread.currentThread().interrupt();
    }
}
}
class WriteToServer extends Thread
{
private DataOutputStream writeMessage;
private String clientName;
private Socket socket;
WriteToServer(Socket socket,String clientName)
{
    try
    {
        this.socket=socket;
        this.writeMessage=new DataOutputStream(socket.getOutputStream());
        this.clientName=clientName;
    }
    catch(Exception e)
    {
        e.printStackTrace();
        Thread.currentThread().interrupt();
    }
}
public void run()
{
    try
    {
        Scanner scanner=new Scanner(System.in);
        String message=scanner.nextLine();
        while(!message.equalsIgnoreCase("quit"))
        {
            writeMessage.writeUTF(clientName+":"+message);
            message=scanner.nextLine();
        }
        writeMessage.writeUTF(message);
        writeMessage.close();
        Thread.currentThread().interrupt();
        if(Thread.currentThread().isInterrupted())
            return;
    }
    catch(Exception e)
    {
        e.printStackTrace();
        Thread.currentThread().interrupt();
    }
}
}
public class ChatClient
{
public static void main(String[] args)
{
    try
    {
        Socket socket=new Socket("localhost",9000);
        try
        {
            System.out.print("Enter Your Name:");
            Scanner scanner=new Scanner(System.in);
            String clientName=scanner.nextLine();
            ReadFromServer rfs=new ReadFromServer(socket);
            WriteToServer wts=new WriteToServer(socket,clientName);
            wts.start();
            rfs.start();
            while(wts.isAlive());
            rfs.interrupt();
            System.out.println("End of Both Threads");
            //socket.close();
        }
        finally
        {
            socket.close();
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
}

当InputStreamReader

使用Socket时,如何处理这种情况

1 个答案:

答案 0 :(得分:0)

SocketClosedException表示关闭套接字,writeMessage.close(),然后继续使用readMessage.readUTF()。这是你代码中的一个错误。你将不得不理清哪些读者和作者线程应该关闭,它应该只是其中之一,而不是另一个仍然在运行。

当您在已经被对等方关闭的连接上调用EOFException时,readUTF()正是您应该期望的。抓住它并单独处理。