在这段代码中,客户机向服务器发送一个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);
}
}
}
服务器输出:
进入while循环
客户端输出:
特征链:
睾丸
我想逆转:teste
发送ch
最佳答案
您所面临的唯一问题与刷新输出流有关。因此,即使accept方法成功地传递,服务器端在从客户机读取输入的步骤中仍然处于挂起状态。
刷新确保在调用flush()之前写入编写器的任何内容都写入底层流,而不是位于某个内部缓冲区中。
这种方法在几种情况下都很有用:
如果另一个进程(或线程)在写入文件时需要检查该文件,并且另一个进程看到所有最近的写入非常重要。
如果写入过程可能会崩溃,那么重要的是不要丢失对文件的写入。
如果您正在向控制台写入,并且需要确保每个消息一经写入就显示出来
(见: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);
}
}
}
还有“世界程序员你好”这句话的插图。
关于java - java中的TCP套接字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42890816/