为什么我的else-if代码没有在服务器端运行?我想在给定循环在客户端运行的次数的情况下打印输入



MyServer.java

import java.io.*;  
import java.net.*;  
public class MyServer {  
public static void main(String[] args){  
try{  
ServerSocket ss=new ServerSocket(5001);  
Socket s=ss.accept();//establishes connection   
DataInputStream dis=new DataInputStream(s.getInputStream());  
String  str=(String)dis.readUTF();  
System.out.println("message= "+str);  
DataOutputStream dout=new DataOutputStream(s.getOutputStream());  
if ("Hi".equals(str)){
dout.writeUTF("How are you?");
} else if ("Bye".equals(str)){
dout.writeUTF("Thankyou! Have a Good day!"); 
} **else if (str != null)){
try {
String numbers;
numbers = str.replaceAll("[^0-9]", ""); 
int number = Integer.parseInt(numbers);
dout.writeUTF("The line is being printed"); 
for (int i = 0; i < number; i++) {
dout.writeUTF(str.replaceAll("[^a-z,^A-Z]", ""));
}
} catch (Exception e) {
//TODO: handle exception
}**


} else {
dout.writeUTF("Sorry!");
}       
dout.flush();  
dout.close();  
s.close();  
ss.close();  
} catch(Exception e) {
System.out.println(e);
} 
}  
}  

MyClient.java

import java.io.*;  
import java.net.*;  
import java.util.*;  
public class MyClient {  
public static void main(String[] args) {  
try{
Scanner sc = new Scanner(System.in);  
Socket s=new Socket("localhost",5001);  
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
String str1= sc.nextLine();           
dout.writeUTF(str1);  
dout.flush(); 

DataInputStream dis=new DataInputStream(s.getInputStream());  
String  str=(String)dis.readUTF(); 
System.out.println("message= "+str);
dout.close();  
dis.close();
s.close();  
} catch(Exception e) {
System.out.println(e);} 
}  
} 

我从客户端向服务器提供输入,并希望该输入在客户端上打印多次。但不能做到这一点。有人能告诉我我在这里犯了什么错误吗?它正在回复消息";嗨"以及";再见";,其他一切都很好。

以下是您的代码和我的更正
(代码后的注释。)

MyServer

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) {
try (ServerSocket ss = new ServerSocket(5001)) {
Socket s = ss.accept();// establishes connection
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
String str = dis.readUTF();
System.out.println("message= " + str);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
if ("Hi".equals(str.trim())) {
dout.writeUTF("How are you?");
}
else if ("Bye".equals(str)) {
dout.writeUTF("Thankyou! Have a Good day!");
}
else if (str != null) {
try {
String numbers;
numbers = str.replaceAll("[^0-9]", "");
int number = Integer.parseInt(numbers);
dout.writeUTF("The line is being printed");
for (int i = 0; i < number; i++) {
dout.writeUTF(str.replaceAll("[^a-z,^A-Z]", ""));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
dout.writeUTF("END");
dout.flush();
}
catch (Exception e) {
e.printStackTrace();
}
try {
Thread.sleep(2000L);
}
catch (InterruptedException xInterrupted) {
// Ignore.
}
}
}

MyClient

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.Socket;
import java.util.Scanner;
public class MyClient {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
try (Socket s = new Socket("localhost", 5001)) {
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF(str1);
dout.flush();
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
String str = dis.readUTF();
while (!"END".equals(str)) {
System.out.println("message= " + str);
str = dis.readUTF();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}

通过套接字发送和接收数据不是即时的。方法readUTF将等待,直到有数据要读取。它将读取上次调用方法writeUTF时发送的所有数据。方法readUTF将无限期等待。因此,服务器需要向客户端发送没有更多数据要发送的通知。在上面的代码中,我发送字符串END来指示服务器正在发送的数据的结束。还要注意,您只需要关闭显式创建的资源。在类别MyServer中,这意味着仅ServerSocket。我使用try with resources来确保ServerSocket已关闭。

类似地,在类MyClient中,唯一需要显式关闭的资源是Socket——我再次使用try with resources来执行此操作。

如果服务器终止,则套接字关闭。因此,在类MyServer中,在向客户端发送数据之后,服务器等待两秒钟,这有望足够客户端读取该数据。

相关内容

最新更新