mehmetozer
mehmetozer

Reputation: 858

How to make a threadedsocket connection in java?

I am very sorry to ask such a basic question but I am new to threads and socket programming. I am trying to write a multithreaded socket program but it always gives erros. When I debugged it, I could not find why it gives that stupid error . Can you please help me where I am wrong ?

MY CLIENT CLASS :

public class Client {

    public void run() throws IOException{
        Socket s = new Socket("localhost",9999);
        PrintStream ps = new PrintStream(s.getOutputStream());
        ps.println(readFromConsole());
    }

    public String readFromConsole(){
        String result ;
        Scanner in = new Scanner(System.in);
        result = in.nextLine();
        return result;
    }
    public static void main(String[] args) throws Exception {
        Client c = new Client();
        c.run();
    }
}

MY SERVER CLASS :

public class Server {
    ServerSocket ss;
    public void run() throws IOException{
        ss = new ServerSocket(9999);
        while(true){
            TestForThread t;
            try {
                t = new TestForThread(ss.accept());
                Thread testThread = new Thread(t);
                testThread.start();
            } catch(IOException e){
                e.printStackTrace();
            }
        }
        //BufferedReader br =
        //new BufferedReader(new InputStreamReader(sForAccept.getInputStream()));
        //String temp = br.readLine();
        //System.out.println(runCommand(temp));
    }
    protected void finalize() {
        // Objects created in run method are finalized when
        // program terminates and thread exits
        try {
            ss.close();
        } catch (IOException e) {
            System.out.println("Could not close socket");
            System.exit(-1);
        }
    }

    public static void main(String[] args) throws Exception{
        Server s = new Server();
        s.run();
    }
}

MY THREAD CLASS::

public class TestForThread implements Runnable {

    private Socket client;
    public TestForThread(Socket c){
        client = c;
    }

    public void run() {
        String line;
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            in = new BufferedReader(new InputStreamReader(client
                .getInputStream()));
            out = new PrintWriter(client.getOutputStream(), true);
        } catch (IOException e) {
            System.out.println("in or out failed");
            System.exit(-1);
        }

        while (true) {
            try {
                line = in.readLine();
                String commandResult = runCommand(line);
                System.out.println(commandResult);
            } catch (IOException e) {
                System.out.println("Read failed");
                System.exit(-1);
            }
        }
    }

    public String runCommand(String command) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command); // you might need the full path
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        String answer = "";
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            answer = answer + line + "\n";
        }
        return answer;
    }
}

Thank you very much.

Upvotes: 1

Views: 166

Answers (1)

Riza
Riza

Reputation: 1184

In your code, you use BufferedReader to wrap the InputStream from Socket, and call readLine() to get string sent by the client. According to http://docs.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html#readLine(), readLine() will wait for '\n' or '\r'. The problem lies in Client.readFromConsole(), because it doesn't return a string that contains a line-termination character.

By adding '\n' or '\r' in the string returned by Client.readFromConsole() method, something like:

public String readFromConsole() {
    String result ;
    Scanner in = new Scanner(System.in);
    result = in.nextLine();
    return result+System.getProperty("line.separator");
}

the Server won't go down after the first request anymore.

Upvotes: 1

Related Questions