简体   繁体   中英

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. 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,

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 . 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(); just after the 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. So following statement should not be the part of run method.

             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 . Because Every new client will be a new program that has its own variables and socket .

A quick look shows me that the reason the socket is closed is because you used dos.close() .

Closing a DataInputStream (or PrintStream , or any similiar stream) will close the underlying socket.

Just take out dos.close() .

You can also move the dos.close() to the very end of the try block. As a general rule, don't close anything related to the socket until you're done with the socket.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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