客户端 - 服务器模式下的InputStreamReader和OutputStreamWriter

时间:2021-12-24 23:55:36

I have to implement a client-server application without using NIO. client send a request to server (in JSON) and then server send response.

我必须在不使用NIO的情况下实现客户端 - 服务器应用程序。客户端向服务器发送请求(在JSON中),然后服务器发送响应。

the problem is with the response, because if the client send the request and te server don't send the response it work.

问题在于响应,因为如果客户端发送请求并且服务器不发送响应,则它起作用。

in both client and server side I use an InputStreamReader and OutputStreamWriter. If I read and write in the server is ok, but if I put the InputStreamReader in the client for read the response the server don't read the previous request from the client. I put some part of code

在客户端和服务器端,我使用InputStreamReader和OutputStreamWriter。如果我在服务器上读取和写入都没问题,但是如果我将InputStreamReader放在客户端中以读取响应,则服务器不会从客户端读取先前的请求。我把一部分代码

System.out.println("eleborazione richiesta");
    JSONParser parser = new JSONParser();
    Object obj;

    try{

        InputStreamReader in = new InputStreamReader(this.csocket.getInputStream(), StandardCharsets.UTF_8);
        OutputStreamWriter out = new OutputStreamWriter(this.csocket.getOutputStream(), StandardCharsets.UTF_8);
        obj =parser.parse(in);
        JSONObject jsonObj = (JSONObject) obj;
        String richiesta= (String) jsonObj.get("request");
        System.out.println("qui");
        if(richiesta==null)return;
        if(richiesta.equals("register")){//REGISTRAZIONE
            ArrayList<String> parametri = null;
            JSONArray  list = (JSONArray) jsonObj.get("parametri");
            if(list!=null) {
                Iterator<String> iterator = list.iterator();
                parametri =new ArrayList<String>(list.size());
                while(iterator.hasNext()){
                    parametri.add(iterator.next());

                }
            }
            System.out.println("richiesta "+ richiesta + " nickname " + parametri.get(0) + " lingua " + parametri.get(1));


            if(utenti.get(parametri.get(0))!=null){
                System.out.println("utente già presente");
            }else{

                utenti.put(parametri.get(0),(new Utenti( parametri.get(0),  parametri.get(1))));
                System.out.println("aggiunto");
            }

            JSONObject object= new JSONObject();
            object.put("response","ok");
            out.write(object.toJSONString());
            out.flush();
            System.out.println("mandata risposta");


        }
...

client

SocketAddress address = new InetSocketAddress("localhost", 1234);
    Socket client =null;
    try {
        client = new Socket("localhost", 1234);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    JSONObject obj = new JSONObject();
    obj.put("request","register");
    JSONArray list = new JSONArray();
    list.add("nick");
    list.add("lingua");
    obj.put("parametri", list);

    try (OutputStreamWriter out = new OutputStreamWriter(client.getOutputStream(), StandardCharsets.UTF_8))
    {
        InputStreamReader in = new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8);

        out.write(obj.toJSONString());
        //out.flush();
        //out.close();

        Thread.sleep(2000);

        JSONParser parser = new JSONParser();
        Object response =parser.parse(in);
        JSONObject jsonObj = (JSONObject) response;
        String resp= (String) jsonObj.get("response");
        System.out.println(resp);

if I execute this the server print only

如果我执行此服务器只打印

client connesso
eleborazione richiesta

if I remove the

如果我删除

 JSONParser parser = new JSONParser();
    Object response =parser.parse(in);
    JSONObject jsonObj = (JSONObject) response;
    String resp= (String) jsonObj.get("response");
    System.out.println(resp);

from the client, the server can send the response, but naturally no one read this

从客户端,服务器可以发送响应,但自然没有人读过这个

can someone tell me what the problem is?

谁能告诉我这是什么问题?

1 个解决方案

#1


1  

I think the reason is your Client and server are blocked reading since it is reading until EOF. you will not receive EOF until you close the socket or close the OutPutStream. You might want to close the OutPutStream immediately after writing if it is only a one-off write. out.write(obj.toJSONString()); out.flush(); socket.shutdownOutput();

我认为原因是你的客户端和服务器被阻止读取,因为它正在读取直到EOF。在关闭套接字或关闭OutPutStream之前,您不会收到EOF。如果只是一次性写入,您可能希望在写入后立即关闭OutPutStream。 out.write(obj.toJSONString());了out.flush(); socket.shutdownOutput();

#1


1  

I think the reason is your Client and server are blocked reading since it is reading until EOF. you will not receive EOF until you close the socket or close the OutPutStream. You might want to close the OutPutStream immediately after writing if it is only a one-off write. out.write(obj.toJSONString()); out.flush(); socket.shutdownOutput();

我认为原因是你的客户端和服务器被阻止读取,因为它正在读取直到EOF。在关闭套接字或关闭OutPutStream之前,您不会收到EOF。如果只是一次性写入,您可能希望在写入后立即关闭OutPutStream。 out.write(obj.toJSONString());了out.flush(); socket.shutdownOutput();

相关文章