简体   繁体   English

服务器套接字一直读取数据,直到我从Java中的客户端关闭套接字为止

[英]Server Socket Keeps Reading Data Until I close Socket from Client In Java

Im unable to get data in server from client using java Following is my server code : 我无法使用java从客户端获取服务器中的数据,以下是我的服务器代码:

 public void UpdateClients() throws Exception{

    Socket socktmp=null;

    try
    {
        socktmp= mainSocket.accept();
        rdr= new BufferedReader( new InputStreamReader(socktmp.getInputStream()));
        String t="";
        String name="";
        while((t=rdr.readLine())!=null){     <<<<< **it stops here**
            name=t;
        }
        //rdr.close();
        LstClient.AddClient(name, socktmp);

        objwriter = new ObjectOutputStream(socktmp.getOutputStream());
        objwriter.writeObject(LstClient.clients);
        objwriter.flush();  
        objwriter.close();
   }
   catch(Exception ex){
        throw new Exception("Unable to Update Clients. Error :" + ex.getMessage());
   }
      finally{
        try{
            if(mainSocket!=null){
                mainSocket.close();
            }
            if(socktmp!=null){
                socktmp.close();
            }
            if(rdr!=null)
            {
                rdr.close();
            }
            if(objwriter!=null)
            {
                objwriter.close();
            }

        }
        catch(Exception ex1){
            throw new Exception("Unable to close streams after opeining. Error :"+ ex1.getMessage());
        }


   }


}

and following is my client code: 以下是我的客户代码:

public Client(String serverIp,int port,String name) throws Exception{

    try{
        if(mainSock!=null){
        mainSock.close();
        }
        mainSock= new Socket(serverIp, port);
        writer= new BufferedWriter(new OutputStreamWriter(mainSock.getOutputStream()));

        writer.write(name);
        writer.flush();
        **//writer.close();**
        Thread.sleep(1500);
        objin = new ObjectInputStream(mainSock.getInputStream());
        Hashtable<String,Socket> lst= new Hashtable<String , Socket>();
        lst = (Hashtable<String, Socket>) objin.readObject();
        cl = new LstClient();
        LstClient.clients = lst;
        System.out.println(LstClient.clients.size());
        objin.close();

    }
    catch(Exception ex){
        throw new Exception("Error : "+ ex.getMessage());
    }
    finally{
        if(mainSock!=null){
            mainSock.close();

        }
        if(writer!=null){
            writer.close();
        }
        if(objin!=null){
            objin.close();
        }


    }

}

I only get data in server or cursor goes on reading line on server side when i close writer in client end. 我只在服务器上获取数据,或者当我在客户端关闭写入器时,光标在服务器端的读取行上。 but when i try to read after closing the writer at client end it says socket is closed so without closing it pauses on readline on server end please help 但是当我在客户端关闭写入器后尝试读取消息时,它说套接字已关闭,因此如果不关闭它,则会在服务器端的readline上暂停,请帮忙

I very common mistake when using readLine, is to forget to send a new line. 使用readLine时,我经常犯的一个错误是忘记发送新行。

You are sending text without a new line ie \\n but waiting for it to be sent all the same. 您正在发送的文本没有换行,即\\n但是等待所有相同的文本发送。

Try adding write('\\n'); 尝试添加write('\\n');

Because you are reading with readLine() , which will block until it runs into a carriage return. 因为您正在使用readLine()进行读取,所以它将阻塞直到遇到回车符为止。 Add a '\\n' at the end of your name. 在名称末尾添加“ \\ n”。 Or better yet, use a PrintWriter and write using println 或者更好的是,使用PrintWriter并使用println写入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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