繁体   English   中英

java中的TCP套接字

[英]TCP socket in java

在此代码片段中,客户端将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);
        }
    }

}

服务器输出:

循环进入

客户输出:

Ecrire une chaine de caractere:
泰斯特
我想逆转:teste
ch被发送

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

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

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

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

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

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

(请参阅: 如何为PrintWriter使用flush

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

更新的服务器代码:

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);
        }
}

}

和世界上的句子Hello程序员一起插图。

在此输入图像描述

暂无
暂无

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

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