tags:

views:

317

answers:

1

Hi, these are my client and server class but i don't know that why the text received doesn't work in a correct way (it will return something but not the one that I want) also when I close the run part of server i will have these exceptions,please help me.thanks

server side:

final static Vector handlers = new Vector(10);
private Socket socket;
private BufferedReader in;
private PrintWriter out;

public ChatHandler(Socket socket) throws IOException {
    this.socket = socket;
    in = new BufferedReader(
            new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(
            new PrintWriter(socket.getOutputStream(),true));
}

@Override
public void run() {
    String line;

    synchronized (handlers) {
        handlers.addElement(this);

    }
    try {

        while ((line = in.readLine()) != null && !line.equalsIgnoreCase("/quit")) {
            for (int i = 0; i < handlers.size(); i++) {
                synchronized (handlers) {
                    ChatHandler handler =
                            (ChatHandler) handlers.elementAt(i);
                    handler.out.println(line + "\r");
                    handler.out.flush();
                }
            }
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            in.close();
            out.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            synchronized (handlers) {
                handlers.removeElement(this);
            }
        }
    }
}

client side: ( apart of that)

public static synchronized void active() {

    String teXt = MainClient.getText();

    os.println(teXt);
    os.flush();
    try {
        String line = is.readLine();



            setFromServertext("Text recieved:"+line+"\n");

        is.close();
        is.close();
        c.close();
    } catch (IOException ex) {
        Logger.getLogger(MainClient.class.getName()).log(Level.SEVERE, null, ex);
    }

active method will be called when the user write something on the text area and click on the send button.

stacktrace:

    init:
deps-jar:
compile-single:
run-single:
Server is starting...
Server is listening...
Client Connected...
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:168)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
    at java.io.InputStreamReader.read(InputStreamReader.java:167)
    at java.io.BufferedReader.fill(BufferedReader.java:136)
    at java.io.BufferedReader.readLine(BufferedReader.java:299)
    at java.io.BufferedReader.readLine(BufferedReader.java:362)
    at ServerNetwork.ChatHandler.run(ChatHandler.java:44)
Client Connected...
Client Connected...
java.net.SocketException: Software caused connection abort: recv failed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:129)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
    at java.io.InputStreamReader.read(InputStreamReader.java:167)
    at java.io.BufferedReader.fill(BufferedReader.java:136)
    at java.io.BufferedReader.readLine(BufferedReader.java:299)
    at java.io.BufferedReader.readLine(BufferedReader.java:362)
    at ServerNetwork.ChatHandler.run(ChatHandler.java:44)
BUILD STOPPED (total time: 15 minutes 53 seconds)
A: 

This normally happens when the socket is closed, which causes the connection to be aborted
The documentation of close() states:

Any thread currently blocked in an I/O operation upon this socket will throw a SocketException.

Use shutdownOutput() if you want a TCP's normal connection termination.
If shutdownOutput is called at the server, the readLine() at the client will return null, indicating an EOF (end of file). Now the client should call shutdownOutput.

Carlos Heuberger