Java套接字服务器通信容易

时间:2018-09-18 13:59:41

标签: java sockets server communication

嗨,我想编写一个简单的代码: 服务器和套接字

服务器-启动 套接字-要连接 服务器-接受

服务器-等待来自套接字的命令 套接字-向流写入命令 服务器-希望在流上进行响应

ajsiodjasodasodsa 不起作用?!

这是连接到服务器的套接字

try (Socket socket = new Socket("localhost", 1234);) {
        // get the outputstream to write a command to the server as soon as it is
        // connected:
        OutputStream outputStream = socket.getOutputStream();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
        // this is the command
        bufferedWriter.write("LISTPARTS");
        bufferedWriter.flush();

        // Now it should open a input stream to read the resonse of the server!
        InputStream inputStream = socket.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        System.out.println("\nafter creating the reader");

        String readLine;
        while ((readLine = bufferedReader.readLine()) == null) {
            System.out.println("what:" + readLine);
        }

此服务器:

   while (true) {
            try (ServerSocket server = new ServerSocket(1234);) {

                System.out.println("wait for Client");
                Socket socket = server.accept(); // wartet bis akzeptiert
                System.out.println("Client was accepted");
                // Open input stream because server waits for a command:
                InputStream inputStream = socket.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                // wait for command
                System.out.println("wait for command");
                String readLine;
                while ((readLine = bufferedReader.readLine()) == null) {
                    System.out.println("wait for client");
                }
                System.out.println("read line:" + readLine);

                // here it should look if the command works ( this works fine  )
                if (readLine.equals("LISTPARTS")) {

                    System.out.println("Commando: listparts");
                    // for responding the server opens a writer to write the response!
                    OutputStream outputStream = socket.getOutputStream();
                    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
                    BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
                    // this method write onto the stream ( it should work normaly !)
                    circuit.dumpParts(bufferedWriter);
                    bufferedWriter.flush();

                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

这是控制台上服务器主输出的结果:

Server- Main
wait for Client
Client was accepted
wait for command

和套接字控制台的控制台:

after creating the reader

1 个答案:

答案 0 :(得分:0)

这是解决方案:

while ((readLine = bufferedReader.readLine()) != null) {
                if (readLine.isEmpty()) {
                    break;
                }
                System.out.println("what:" + readLine);

            }