2

I have server and client. My server acepts all connections and returns to client string. But when I try to send more lines its crashed with

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)

Here is it my Server Code :

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!");
        }
    }
}

And Here is it my client code :

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;
    }
}

I readed this topic : Socket Exception: socket is closed but this dotn helped me. // Sorry for the links , but I cant understand how properly to format my code here.

Community
  • 1
  • 1
  • 5
    Don't use pastebin. Post only the relevant part of your code directly in your question. If you feel the need to use pastebin, it is a clear indication that you are trying to post way more code that you probably need and should. – Bruno Reis Apr 06 '13 at 22:50
  • I cant format my code here. Doesnt work. I think this is not a big problem. Next time I will try to paste it here. – Preslav Panayotov Apr 06 '13 at 22:57
  • 2
    You sure can format your code here. Actually, your code is automatically formatted for you. You can navigate a little bit around here to see that most questions (and answers) contain formatter code samples. Check this: http://stackoverflow.com/editing-help – Bruno Reis Apr 06 '13 at 22:58
  • Okey , 10x for the links. I fixed it. So Can we now slove my problem with closed socket ? – Preslav Panayotov Apr 06 '13 at 23:19
  • you are very rude for a newcomer, aren't you? There are rules, there is etiquette, and if you want help from the community, you have to follow all of that. – Bruno Reis Apr 06 '13 at 23:21
  • I apologize. I made a mistake. You gave me guidance, I read, I tried and I was able to format the text. Right? Sorry if I look from countries like jackanapes. The problem that previously described harassing me than a few days. Again, I'm sorry. As you can see my take lessons and try to do everything the community wants it. – Preslav Panayotov Apr 06 '13 at 23:25

1 Answers1

3

From the javadoc of getOutputStream() in Socket:

Closing the returned OutputStream will close the associated socket.

Also, closing a PrintWriter (and all other Printers/Writers) close their underlying streams as well. So, you are closing your OutputStream by closing the PrintWriter (in hiMsg()) and then trying to write to the socket, which was closed.

To fix the problem, don't close the PrintWriter. Garbage collection will take care of it for you! But do close the socket when you are done with it.

ddmps
  • 4,350
  • 1
  • 19
  • 34
  • If I remove the `out.close();` it dont sends the information to the client. – Preslav Panayotov Apr 06 '13 at 23:27
  • Isn't that the point? If you mean it doesn't send it - then try with `out.flush();` instead, which ensures it flushes (sends)! – ddmps Apr 06 '13 at 23:29
  • Thanks a lot ! I will reconstructed my Vector to escape from this to close PrintWriter – Preslav Panayotov Apr 06 '13 at 23:38
  • This is the way I reconstructed my Server, and for now its work. `private class ConnectionU { public Socket socket; public BufferedReader br; public PrintWriter pw; public ConnectionU(Socket s) { br = new BufferedReader(new InputStreamReader(s.getInputStream())); pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream())); } public PrintWriter getPrintWriter() { return pw; } public BufferedReader getBufferedReader() { return br; } }` – Preslav Panayotov Apr 06 '13 at 23:46