简体   繁体   English

在服务器处理之前如何保持套接字连接?

[英]How to keep Socket Connection until Server Process it?

I am writing Socket program , Here Client Sends a String through Stream , Server Process it and writes back to Client. 我正在编写Socket程序,在这里客户端通过Stream发送一个字符串,服务器对其进行处理并将其写回到客户端。 My problem is, after Server process the String , it Writes back to Stream but in client It can't able to read the Stream its showing exception as Exception in while: java.net.SocketException: socket closed Here is my code, 我的问题是,在Server处理完String ,它会写回到Stream中,但是在客户端中,它无法在以下时间内读取Stream作为Exception in while: java.net.SocketException: socket closed显示异常Exception in while: java.net.SocketException: socket closed这是我的代码,

Client , 客户

  public void run() {
    while (true) {
        try {
            // Open your connection to a server, at port 1231
            s1 = new Socket("localhost", 1231);

            OutputStream s1out = s1.getOutputStream();
            DataOutputStream dos = new DataOutputStream(s1out);
            InputStream in=s1.getInputStream();
            DataInputStream dis=new DataInputStream(in);

            String s = br.readLine();
         dos.writeUTF(s);
         dos.flush();
         dos.close();

            System.out.println(dis.readUTF());//it is the String from Server after processing
            dis.close();

        } catch (IOException ex) {
            //  Logger.getLogger(SimpleClient.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Exception in while: " + ex);
        }
    }

In Server 在服务器中

  public void run()
    {


        while(true){
            try {

                 System.out.println("Waiting for connect to client");
                 s1=serverSocket.accept();


                 s1In = s1.getInputStream();
                 dis = new DataInputStream(s1In);

                 out=s1.getOutputStream();
                 dos=new DataOutputStream(out);

                 String clientData=dis.readUTF();

                 //processing task String

              dos.writeUTF("Bus Registered Successfully");
              dos.flush();

            }
          }

Here I am not able to read Bus Registered Successfully at client side . 在这里,我无法在客户端阅读Bus Registered Successfully How to Solve this.? 该如何解决?

Well there are many things not right in your program. 嗯,您的程序中有很多不对的地方。 But first let me answer your question ... you are closing the socket just after writing the stream ... so server throws exception, just remove dos.close(); 但是首先让我回答您的问题...您正在写流后立即关闭套接字...因此服务器抛出异常,只需删除dos.close(); just after the dos.flush(); 就在dos.flush(); . It will run fine. 它将运行正常。

Now back to the programming practices ... 现在回到编程实践...

1) Server should accept the connection in a while(true) loop and then make a new thread. 1)服务器应在while(true)循环中接受连接,然后创建一个新线程。 So following statement should not be the part of run method. 所以下面的语句应该的一部分run方法。

             System.out.println("Waiting for connect to client");
             s1=serverSocket.accept();


             s1In = s1.getInputStream();
             dis = new DataInputStream(s1In);

             out=s1.getOutputStream();
             dos=new DataOutputStream(out);

2) There is no need of run method in client . 2)client 不需要 run方法。 Because Every new client will be a new program that has its own variables and socket . 因为每个新客户都是一个具有自己的variablessocket的新程序。

A quick look shows me that the reason the socket is closed is because you used dos.close() . 快速浏览一下即可了解到,关闭套接字的原因是因为您使用了dos.close()

Closing a DataInputStream (or PrintStream , or any similiar stream) will close the underlying socket. 关闭DataInputStream (或PrintStream或任何类似的流)将关闭基础套接字。

Just take out dos.close() . 只需取出dos.close()

You can also move the dos.close() to the very end of the try block. 您也可以将dos.close()移至try块的末尾。 As a general rule, don't close anything related to the socket until you're done with the socket. 通常,在完成套接字之前,请勿关闭与套接字相关的任何内容。

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

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