Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在不关闭服务器套接字的情况下在客户端获得确认?_Java_Multithreading_Sockets_Servlets_Network Programming - Fatal编程技术网

Java 如何在不关闭服务器套接字的情况下在客户端获得确认?

Java 如何在不关闭服务器套接字的情况下在客户端获得确认?,java,multithreading,sockets,servlets,network-programming,Java,Multithreading,Sockets,Servlets,Network Programming,我能够向服务器发送字符串,服务器也收到了相同的字符串。服务器能够发送确认,但在服务器结束连接之前,客户端不会得到确认。但我不想关闭连接。如何在不关闭连接的情况下显示确认 //This is Client public void Actuator1_Stop(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {

我能够向服务器发送字符串,服务器也收到了相同的字符串。服务器能够发送确认,但在服务器结束连接之前,客户端不会得到确认。但我不想关闭连接。如何在不关闭连接的情况下显示确认

//This is Client    
public void Actuator1_Stop(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
    try {
        Socket socket = new Socket("localhost", 1028);
        DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
        dout.writeUTF("Stop_Actuator");
        dout.flush();
        System.out.println("Command Sent = Stop_Actuator");

        //Get the return message from server
        InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String message = br.readLine();
        System.out.println("ACK received from the server : " +message);
        socket.close();
    } catch(Exception exception) {
        exception.printStackTrace();
    }
}

//This is Server
class Socket4 implements Runnable  {
    public void run() {
        try {
            ServerSocket ss = new ServerSocket(1028);
            while(true) {
                Socket s = ss.accept();
                DataInputStream dis = new DataInputStream(s.getInputStream());
                String cmd = dis.readUTF();
                System.out.println("Command= "+cmd);

                //Sending the response back to the client
                String ack = null;
                OutputStream os = s.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os);
                BufferedWriter bw = new BufferedWriter(osw);

                if(cmd.equals("Stop_Actuator")) {
                        ack= "Ok";
                        bw.write(ack);
                   } else {
                        ack = "Error";
                        bw.write(ack);
                   }

                   System.out.println("ACK sent to the client is "+ack);
                   bw.flush();
            }
        } catch(Exception e) {
             e.printStackTrace();
        }
    }
}

public class MyServer {
    public static void main(String[] args) {
        Socket1 s1 = new Socket1();
        Socket2 s2 = new Socket2();
        Socket3 s3 = new Socket3();
        Socket4 s4 = new Socket4();

        Thread t1 = new Thread(s1);
        Thread t2 = new Thread(s2);
        Thread t3 = new Thread(s3);
        Thread t4 = new Thread(s4);

        t1.start();
        t2.start();
        t3.start();
        t4.start();   
        }
    } 
}
如何在不关闭连接的情况下显示确认

//This is Client    
public void Actuator1_Stop(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
    try {
        Socket socket = new Socket("localhost", 1028);
        DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
        dout.writeUTF("Stop_Actuator");
        dout.flush();
        System.out.println("Command Sent = Stop_Actuator");

        //Get the return message from server
        InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String message = br.readLine();
        System.out.println("ACK received from the server : " +message);
        socket.close();
    } catch(Exception exception) {
        exception.printStackTrace();
    }
}

//This is Server
class Socket4 implements Runnable  {
    public void run() {
        try {
            ServerSocket ss = new ServerSocket(1028);
            while(true) {
                Socket s = ss.accept();
                DataInputStream dis = new DataInputStream(s.getInputStream());
                String cmd = dis.readUTF();
                System.out.println("Command= "+cmd);

                //Sending the response back to the client
                String ack = null;
                OutputStream os = s.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os);
                BufferedWriter bw = new BufferedWriter(osw);

                if(cmd.equals("Stop_Actuator")) {
                        ack= "Ok";
                        bw.write(ack);
                   } else {
                        ack = "Error";
                        bw.write(ack);
                   }

                   System.out.println("ACK sent to the client is "+ack);
                   bw.flush();
            }
        } catch(Exception e) {
             e.printStackTrace();
        }
    }
}

public class MyServer {
    public static void main(String[] args) {
        Socket1 s1 = new Socket1();
        Socket2 s2 = new Socket2();
        Socket3 s3 = new Socket3();
        Socket4 s4 = new Socket4();

        Thread t1 = new Thread(s1);
        Thread t2 = new Thread(s2);
        Thread t3 = new Thread(s3);
        Thread t4 = new Thread(s4);

        t1.start();
        t2.start();
        t3.start();
        t4.start();   
        }
    } 
}
在您的客户机中,您正在执行读取行:

String message = br.readLine();
但是,从服务器发送的不是完整的行。您需要在邮件末尾添加行终止字符:

ack = "Ok\n";
bw.write(ack);
然后,读取行完成,客户端获得ack。显然,错误确认还需要换行符(
“error\n”


确保正确关闭在
Socket4.run()
中创建的已接受套接字和服务器套接字。我假设您只是发布部分代码,但请确保在try/finally块中关闭这些套接字。

'ack=“Ok”'-这行看起来像readline()将返回的文本行吗?谢谢。成功了。现在我在客户端得到了ack。为什么要在一个方向上发送行,在另一个方向上使用
writeUTF()
?保持一致,谢谢。成功了。现在我在客户端得到确认。