简体   繁体   中英

Java Why does the server not respond to a second client?

I wrote a Simple Server Client Program and when I start the Server and the Client, the Client gets the message which it sent to the Server back and terminates itself. But when i start another Client it does not get the message back. Why? Thanks alot!

First class: the client

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class StringClient {

    public static void main(String[] args) {

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

            OutputStream raus = socket.getOutputStream();
            PrintStream ps = new PrintStream(raus, true);
            System.out.println("1");
            ps.println("Hello World!");
            ps.println("Hello Universe!");

            InputStream rein = socket.getInputStream();
            System.out.println("verf\u00FCgbare Bytes: " + rein.available());
            BufferedReader br  =new BufferedReader(new InputStreamReader(rein));
            System.out.println(2);

            while(br.ready()){
                System.out.println(br.readLine());
            }


        }catch(UnknownHostException e ){
            System.out.println("Unknown Host..."); 
            e.printStackTrace();

        } catch (IOException e) {
            System.out.println("IOProbleme..."); 
            e.printStackTrace();
        }finally{
            if(socket != null){
                try{
                    socket.close();
                    System.out.println("Socket geschlossen...");
                }catch(IOException e){
                    System.out.println("Fehler beim Schließen des Sockets!");
                    e.printStackTrace();
                }
            }
        }
    }

}

Second class: the Server

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class StringServer {
    private final ServerSocket server;

    public StringServer(int port) throws IOException {
        server = new ServerSocket(port);
    }


    private void connect(){

        while(true){
            Socket socket = null;
            try{
                socket = server.accept();
                reinRaus(socket);
            }catch(IOException e){
                e.printStackTrace();
            }finally{
                if(socket != null){
                    try{
                        socket.close();
                    }catch(IOException e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    private void reinRaus(Socket socket) throws IOException{
        BufferedReader rein = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintStream raus = new PrintStream(socket.getOutputStream());
        String s;

        while(rein.ready()){
            s = rein.readLine();
            raus.println(s);
        }
    }

    public static void main(String[] args) throws IOException {
        StringServer server = new StringServer(3141);
        server.connect();
    }


}

The main thread should allocates individual/different threads to the incoming clients, so different clients get allocated for different threads (they get different sockets). In your current code there's only a server serving a client.

StringServer.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class StringServer {
    private final ServerSocket server;

    public StringServer(int port) throws IOException {
        server = new ServerSocket(port);
    }


    private void connect(){

        while(true){
            Socket socket = null;
            try{
                socket = server.accept();


                 clientHandler ch = new clientHandler(socket);
                   ch.start();



            }catch(IOException e){
                e.printStackTrace();
            }finally{
                if(socket != null){
                    try{
                        socket.close();
                    }catch(IOException e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }



    public static void main(String[] args) throws IOException {
        StringServer server = new StringServer(3541);
        server.connect();
    }


}
class clientHandler extends Thread {

    Socket client;


    public clientHandler(Socket client) {
        this.client = client;
    }

    @Override
    public void run() {

        try {

          BufferedReader rein = new BufferedReader(new InputStreamReader(client.getInputStream()));
        PrintStream raus = new PrintStream(client.getOutputStream(),true);
        String s;

        while(rein.ready()){
            s = rein.readLine();
            raus.println(s);
        }
            client.close(); // IOException

        } catch (IOException e) {
        }

    }

}

StringClient.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class StringClient {

    public static void main(String[] args) {

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

            OutputStream raus = socket.getOutputStream();
            PrintStream ps = new PrintStream(raus, true);
            System.out.println("1");
            ps.println("Hello World!");
            ps.println("Hello Universe!");

            InputStream rein = socket.getInputStream();
            System.out.println("verf\u00FCgbare Bytes: " + rein.available());
            BufferedReader br  =new BufferedReader(new InputStreamReader(rein));
            System.out.println(2);

            while(br.ready()){
                System.out.println(br.readLine());
            }


        }catch(UnknownHostException e ){
            System.out.println("Unknown Host..."); 
            e.printStackTrace();

        } catch (IOException e) {
            System.out.println("IOProbleme..."); 
            e.printStackTrace();
        }finally{
            if(socket != null){
                try{
                    socket.close();
                    System.out.println("Socket geschlossen...");
                }catch(IOException e){
                    System.out.println("Fehler beim Schließen des Sockets!");
                    e.printStackTrace();
                }
            }
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM