Heloo,this wil be my first post here, the reason for that is I can't figure this one out by myself

public class Client extends Thread{
Socket socket = null;
ServerResponse sr = null;
    private class serverResponse extends Thread{
     InputStream in = null;
     public serverResponse(Socket socket) throws IOException{
         if (socket == null) { System.out.println("socket:null");}
          this.in = socket.getInputStream();
     }
     public void run() {
        try {
                while (true) {
                    if (!socket.isClosed() || socket.isConnected()) {
                    byte[] input = new byte[1024];
                    // read the message
                    in.read(input, 0, input.length);
                    // unserialize and dispatch
                    }
                }
                 } catch (IOException ex) {
                 System.out.println("IOException in waitForReplyFromServer");
                 ex.printStackTrace();
                 } finally {
                 if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
              }
           }
public client() throw Exception{
    this.socket =new Socket(InetAddress.getBytName("localhost", 4444); 
    this.sendMessage(new String(john").getBytes());
}
sendMessage(byte[] tosend){
    OutputStream out = null;
        try {
            out = this.socket.getOutputStream();
            out.write(bytesToSend, 0, bytesToSend.length);
            out.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Exception in sendBytes()");
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
public void run(){
    this.serverResponse.start();
}
}

Excepting for the lack of some client class members, this what my class looks like, the problem that I was referring about is I get an IOException :
IOException in waitForReplyFromServer
java.net.SocketException: socket closed
from this line line: in.read(input, 0, input.length);, the thing that confuses me the most is that is after the if-condition

Edited 4 Years Ago by CPT: NormR1's observation

Is that right?

you are right, it was the other way around now it's the right way

Edited 4 Years Ago by CPT

Did that fix the problem?

no
this program should send information to a server via sendBytes function, and wait for responses in the serverResponse thread/class, I don't understand why the socket is closed at that point because sendbytes sends information succesfuly to the server(which means that the socket shouldn't be closed)

Edited 4 Years Ago by CPT

so this a basic version of the client:

package temp_client;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class Client extends Thread{
    Socket socket = null;
    serverResponse sr = null;
    private class serverResponse extends Thread{
        InputStream in = null;

        public serverResponse(Socket socket) throws IOException{
            if (socket == null) { System.out.println("socket:null");}
            this.in = socket.getInputStream();
        }

        public void run() {
            try {
                while (true) {
                    if (!socket.isClosed() || socket.isConnected()) {
                        byte[] input = new byte[1024];
                        // read the message
                        in.read(input, 0, input.length);
                        // unserialize and dispatch
                        for(int i=0;i<input.length;i++){
                            System.out.print("byte["+i+"]="+input[i]);
                        }
                    }
                }
            } catch (IOException ex) {
                System.out.println("IOException in waitForReplyFromServer");
                ex.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }

    }
    public Client() throws Exception{
        this.socket =new Socket(InetAddress.getByName("localhost"), 4444);
        this.sendMessage(new String("john").getBytes());
        this.sr = new serverResponse(socket);

        this.sendMessage(new String("Hello world").getBytes());
    }
    void sendMessage(byte[] tosend) throws IOException{
        OutputStream out = null;
        try {
            out = this.socket.getOutputStream();
            out.write(tosend, 0, tosend.length);
            out.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Exception in sendBytes()");
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
    public void run(){
        this.sr.start();
    }
    public static void main(String args[]){
        Client c1;
        try {
            c1 = new Client();
            c1.start();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

and this is the server:

package temp_server;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class Server extends Thread{

    void StartServer(){
        ServerSocket ss = null;
        try{
            ss = new ServerSocket(4444);
            while(true){
                Socket sock = ss.accept();
                new handleClient(sock).start();
            }

        }catch(IOException e){
            e.printStackTrace();
            System.out.println("IOEception in startServer()!");
            System.exit(-1);
        }catch(Throwable th){
            th.printStackTrace();
        }
    }
    public void run(){
        this.StartServer();
    }
}
class handleClient extends Thread{
    private Socket socket = null;
    private BufferedInputStream in = null;

    public handleClient(Socket sock)throws IOException{
        this.socket = sock;
        this.in =  new BufferedInputStream(socket.getInputStream());
    }
    public void run(){
        byte[] input = new byte[1024];
        try{
            in.read(input,0,input.length);
        }
        catch(IOException e){
            System.out.println("IOEXCEPTION!!!!!");
        }
    }
    void sendMessage(byte[] tosend) throws IOException{
        OutputStream out = null;
        try {
            out = this.socket.getOutputStream();
            out.write(tosend, 0, tosend.length);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Exception in sendBytes()");
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
        }
    }

    public static void main(String[] args){
        Server server = new Server();
        server.start();
    }
}

as an IDE I'm using eclipse: for some reason the only way I can get the server to run is by right click on the server's package and select run as java aplication.

I don't have your IDE. How do I execute it?

What is the code supposed to do when it does execute?

I moved the main functino from the handle client in the server class, now it should work normally, I can't edit my previous post so here is the server :

package temp_server;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class Server extends Thread{

    void StartServer(){
        ServerSocket ss = null;
        try{
            ss = new ServerSocket(4444);
            while(true){
                Socket sock = ss.accept();
                new handleClient(sock).start();
            }

        }catch(IOException e){
            e.printStackTrace();
            System.out.println("IOEception in startServer()!");
            System.exit(-1);
        }catch(Throwable th){
            th.printStackTrace();
        }
    }
    public void run(){
        this.StartServer();
    }

    public static void main(String[] args){
        Server server = new Server();
        server.start();
    }
}
class handleClient extends Thread{
    private Socket socket = null;
    private BufferedInputStream in = null;

    public handleClient(Socket sock)throws IOException{
        this.socket = sock;
        this.in =  new BufferedInputStream(socket.getInputStream());
    }
    public void run(){
        byte[] input = new byte[1024];
        try{
            in.read(input,0,input.length);
            //echoing back the message
            sendMessage(input);
        }
        catch(IOException e){
            System.out.println("IOEXCEPTION!!!!!");
        }
    }
    void sendMessage(byte[] tosend) throws IOException{
        OutputStream out = null;
        try {
            out = this.socket.getOutputStream();
            out.write(tosend, 0, tosend.length);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Exception in sendBytes()");
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
        }
    }
}

What is the code supposed to do when it does execute?

Well the client application sends a message to the server in the constructor, after that it should wait for a response from the server in serverResponse thread and print-out what it received. The server should handle the client conection in the handleClient thread, in which it should echo the message back to the client.

Edited 4 Years Ago by CPT

Try commenting out all the calls to a close() method.
Add lots of printlns to the code to show what is execute and what the values of the Strings that are send and received.

line 36: this.sendMessage(new String(john").getBytes());
shouldn't it be in full quotations: "john"?

Edited 4 Years Ago by corby

line 36: this.sendMessage(new String(john").getBytes());
shouldn't it be in full quotations: "john"?

well in the program it is in full quotations, thanks for pointing that out

latest update: I've put the keyword volatille to the Socket member in the client(since it's shared resource between the main thread and the serverResponse) thread with no results: the socket still appears to be empty in the serverResponse thread.

Can you post the println output that traces what the code is doing, where it is executing and what values it is writing and reading?

Where is the problem now with the code? What does it do and what does it not do?

This article has been dead for over six months. Start a new discussion instead.