Kategorien
Java

Java Client/Server Kommunikation Socketverbindung

Das Parkhaus Besipiel unter Verwendung einer Socketverbindung über einen Port.

Client:

public class Client {
    public static void main(String[] args) throws IOException {

        Socket kkSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            kkSocket = new Socket("127.0.0.1", 6700);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("host unknown.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: 127.0.0.1.");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer;
        String fromUser;

        while ((fromServer = in.readLine()) != null) {            
            if (fromServer.equals("server quit"))
                break;
            System.out.println("Server: " + fromServer);
            fromUser = stdIn.readLine();
            out.println(fromUser);
        }
        System.out.println("Client down.");
        out.close();
        in.close();
        stdIn.close();
        kkSocket.close();
    }
}

Server:

public class Server {
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(6700);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 6700.");
            System.exit(1);
        }
        System.out.println("Server Running.");
        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                clientSocket.getInputStream()));
        String inputLine, outputLine;
        Protocol kkp = new Protocol();

        outputLine = kkp.processInput(null);
        out.println(outputLine);

        while ((inputLine = in.readLine()) != null) {
            if(inputLine.equals("quit"))
            {
                out.println("server quit");
                break;
            }
             outputLine = kkp.processInput(inputLine);
             out.println(outputLine);
//             if (outputLine.equals("quit server"))
//                break;
        }
        System.out.println("Server Down.");
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}

Protocoll für den Server:

public class Protocol {
    private static final int WAITING = 0;
    private static final int RUNNING = 1;
    private int state = WAITING;
    private Parkhaus parkhaus = null;

    public Protocol()
    {
        parkhaus = new Parkhaus(5);
    }
    public String processInput(String theInput) {
        String theOutput = null;

        if (state == WAITING) {
            theOutput = "Hello Client!";
            state = RUNNING;
        } else if (state == RUNNING) {
            if (theInput.equalsIgnoreCase("in")) {
                Auto auto = new Auto(parkhaus);
                if(auto.einfahrenParkhaus())
                {
                    theOutput = "ok";
                }
                 else
                {
                    theOutput = "sorry, voll";
                 }
            }
            else if(theInput.equalsIgnoreCase("out")) {
                Auto auto = new Auto(parkhaus);
                if(auto.rausfahrenParkhaus())
                {
                    theOutput = "ok";
                }
                 else
                {
                    theOutput = "sorry, leer";
                 }
            }
            else if(theInput.equalsIgnoreCase("free")) {
                theOutput = "" + parkhaus.freieParkplaetze();
            }
            else {
                theOutput = "gültige Eingaben: in, out, free, quit";
            }
        }
        return theOutput;
    }
}