2

我有服务器和客户端。我的服务器接受所有连接并返回到客户端字符串。但是当我尝试发送更多行时,它崩溃了

java.net.SocketException: Socket is closed at java.net.Socket.getOutputStream(Unknown Source) at server.ServerCore.hiMsg(ServerCore.java:67) at server.ServerCore.run(ServerCore.java:49)

这是我的服务器代码:

package server;

import java.io.File;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pisio
 */
public class ServerCore extends Thread {

    private LoadSettings loadSettings = LoadSettings.Init();
    private int port = loadSettings.getConfigInt("port");
    private int max_connections = loadSettings.getConfigInt("max_connections");
    private String ipServer = loadSettings.getConfig("ipServer");
    private ServerSocket socket;
    private Socket connection;
    private boolean serverRuning = false;
    private int connectedUsers = 0;
    private Vector connVector = new Vector();

    @Override
    public void run() {

        try {
            socket = new ServerSocket(port, max_connections);
            System.out.println("+++\t Server was started at  address:" + socket.getLocalSocketAddress() + " with posible max users " + max_connections);

            serverRuning = true;

            while (serverRuning) {
                if (connectedUsers <= max_connections) {
                    connection = socket.accept();
                    connection.setKeepAlive(serverRuning);
                    connVector.add(connection);
                    connectedUsers = connVector.size();
                    hiMsg();
                    hiMsg();
                    hiMsg();

                }

                System.out.println("+++\t Last User:" + connVector.get(connVector.size() - 1).toString());
            }


        } catch (IOException ex) {
            //   Logger.getLogger(ServerCore.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void hiMsg() {

        try {
            Socket c = (Socket) connVector.get(connVector.size() - 1);
            PrintWriter out = new PrintWriter(new OutputStreamWriter(c.getOutputStream()));
            out.write("Hi user !  Im server !  Your master !  Blow me down\nping: ?  PONG ?");
            out.close();

        } catch (IOException ex) {
            Logger.getLogger(ServerCore.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void stopServer() {
        statusServer();
        serverRuning = false;
        try {
            socket.close();
        } catch (IOException ex) {
            //Logger.getLogger(ServerCore.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("+++++\t  SERVER WAS STOPED !");
        // System.exit(port);

    }

    public void statusServer() {
        if (serverRuning) {
            System.out.println("Server running at port:" + port + "  with connected users :" + connectedUsers + "/" + max_connections);
        } else {
            System.out.println("Server IS NOT RUNNING!");
        }
    }
}

这是我的客户代码:

package meetingsclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.String;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ServerConnect {

    private static ServerConnect sc = null;
    private Socket socket = null;

    private ServerConnect() {

        try {
            socket = new Socket("localhost", 8080);

            sendHiMsg();

        } catch (Exception ex) {
            Logger.getLogger(ServerConnect.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void sendHiMsg() throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));


        System.out.println("Input:" + in.readLine());


    }

    public static ServerConnect Init() {
        if (sc == null) {
            sc = new ServerConnect();
        }
        return ServerConnect.sc;
    }
}

我读了这个主题:Socket Exception: socket is closed but this dotn帮助了我。//对不起链接,但我无法理解如何正确格式化我的代码。

4

1 回答 1

3

来自in的javadocgetOutputStream()Socket

关闭返回的 OutputStream 将关闭关联的套接字。

此外,关闭PrintWriter(和所有其他打印机/写入器)也会关闭它们的底层流。因此,您OutputStream通过关闭PrintWriter(in hiMsg()) 然后尝试写入已关闭的套接字来关闭您的。

要解决此问题,请不要关闭PrintWriter. 垃圾收集将为您处理!但是,当你完成它时,请关闭套接字。

于 2013-04-06T23:19:25.840 回答