java

时间:2017-03-19 18:57:17

标签: java sockets tcp

在此代码片段中,客户端将String发送到服务器。 服务器将其反转并重新发送给客户端。 但是,客户端没有收到任何字符串。 或者,服务器可能没有收到要反转的字符串。

服务器代码:

public class Serveur {
public static void main(String[] args) {
    ServerSocket sock = null;
    Socket link = null;
    BufferedReader input = null;
    PrintWriter output = null;

    try {
        sock = new ServerSocket(1234);
        link = sock.accept();
        input = new BufferedReader(new InputStreamReader(link.getInputStream()));
        output = new PrintWriter(link.getOutputStream());
        String str, rstr;
        while(true){
            System.out.println("entered while loop");
            str = input.readLine();
            System.out.println("we received : " + str);
            rstr="";
            for (int i = 0; i < str.length(); i++)
                rstr = str.charAt(i) + rstr;
            System.out.println("we will send to the client : " + rstr);
            output.print(rstr);
            System.out.println("sent");
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}
}

客户代码:

    public class Client {

    public static void main(String[] args) {
        Socket sock = null;
        PrintWriter output = null;
        BufferedReader input = null;

        try { 
            sock = new Socket(InetAddress.getLocalHost(), 1234);
            output = new PrintWriter(sock.getOutputStream());
            input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            String str;
            Scanner s = new Scanner(System.in);
            while(true){
                System.out.println("Ecrire une chaine de caractere : ");
                str = s.nextLine();
                System.out.println("i want to reverse : " + str);    
                output.println(str);
                       System.out.println("ch is sent");
                       String rr = input.readLine();
                System.out.print(rr);
                       System.out.println("reversed word is received");
            }
        } catch (UnknownHostException ex) {
            System.out.println(ex.getMessage());
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
}

}

服务器输出:

entered while loop

客户输出:

Ecrire une chaine de caractere : 
teste
i want to reverse : teste
ch is sent

1 个答案:

答案 0 :(得分:0)

您遇到的唯一问题与刷新输出流有关。因此,即使 accept 方法成功通过,服务器端也会在从客户端读取输入的步骤中保持挂起状态。

Flushing确保在调用flush()之前写入writer的任何内容都写入底层流,而不是放在某个内部缓冲区中。

该方法在几种情况下派上用场:

  • 如果另一个进程(或线程)在写入文件时需要检查该文件,那么另一个进程看到所有最近的写入是很重要的。

    < / LI>
  • 如果写入过程可能会崩溃,那么重要的是不会丢失对文件的写入。

  • 如果您正在写入控制台,并且需要确保每条消息在写完后立即显示

(参见:How to use flush for PrintWriter

我相应地更新了你的代码并且它正在运行。

更新的服务器代码:

public class Serveur {
    public static void main(String[] args) {
        ServerSocket sock = null;
        Socket link = null;
        BufferedReader input = null;
        PrintWriter output = null;

        try {
            sock = new ServerSocket(9890);
            link = sock.accept();
            input = new BufferedReader(new InputStreamReader(link.getInputStream()));
            output = new PrintWriter(link.getOutputStream());
            String str, rstr;
            while(true){
                System.out.println("entered while loop");
                str = input.readLine();
                System.out.println("we received : " + str);
                rstr="";
                for (int i = 0; i < str.length(); i++)
                    rstr = str.charAt(i) + rstr;
                System.out.println("we will send to the client : " + rstr);
                output.println(rstr);

                output.flush();
                System.out.println("sent");
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

}

更新的客户端代码:

public class Client {
    public static void main(String[] args) {
        Socket sock = null;
        PrintWriter output = null;
        BufferedReader input = null;

        try { 
            sock = new Socket(InetAddress.getLocalHost(), 9890);
            output = new PrintWriter(sock.getOutputStream());
            input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            String str;
            Scanner s = new Scanner(System.in);
            while(true){
                System.out.println("Ecrire une chaine de caractere : ");
                str = s.nextLine();
                System.out.println("i want to reverse : " + str);    
                output.println(str);
                output.flush();
                       System.out.println("ch is sent");
                       String rr = input.readLine();
                System.out.print(rr);
                       System.out.println("reversed word is received");
            }
        } catch (UnknownHostException ex) {
            System.out.println(ex.getMessage());
        } catch (IOException ex) {
            //Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
}

}

带有句子世界的程序员的插图。

enter image description here