【发布时间】:2014-10-17 15:18:43
【问题描述】:
我正在为 JAVA 中的单个服务器和多个客户端进行编码。 This is my code for server 和This is my code for client。
更具体地说,我的服务器中有以下代码。
String Name;
try {
os.println("Enter Your Name:");
Name=is.readLine();
os.println("Hello "+ Name +" Welcome To Our Exam System.Please type Exam to take an Exam or type QUIT to Exit");
line=is.readLine();
while(line.compareTo("QUIT")!=0){
os.println(line);
os.flush();
System.out.println("Response to Client : "+line);
line=is.readLine();
}
} catch (IOException e) {
line=this.getName(); //reused String line for getting thread name
System.out.println("IO Error/ Client "+line+" terminated abruptly");
}
catch(NullPointerException e){
line=this.getName(); //reused String line for getting thread name
System.out.println("Client "+line+" Closed");
}
以及client.java中的以下代码
try {
s1=new Socket(address, 4445); // You can use static final constant PORT_NUM
br= new BufferedReader(new InputStreamReader(System.in));
is=new BufferedReader(new InputStreamReader(s1.getInputStream()));
os= new PrintWriter(s1.getOutputStream());
}
catch (IOException e){
e.printStackTrace();
System.err.print("IO Exception");
}
System.out.println("Client Address : "+address);
System.out.println("Enter Data to echo Server ( Enter QUIT to end):");
String response=null;
try{
response=is.readLine();
System.out.println(response);
line=br.readLine();
os.println(line);
response=is.readLine();
line=br.readLine();
while(line.compareTo("QUIT")!=0)
{
os.println(line);
os.flush();
response=is.readLine();
System.out.println("Server Response : "+response);
line=br.readLine();
}
}
catch(IOException e){
e.printStackTrace();
System.out.println("Socket read Error");
}
从这段代码中,我期待以下任务:
- 最初服务器和客户端之间已建立连接
- 将从服务器向客户端发送一条名为“请输入您的姓名”的消息
- 此消息将打印在客户端控制台中。
- 从客户端,一个名称将被发送到服务器。
然后服务器将执行以下行:
os.println("Hello "+ Name +" Welcome To Our Exam System.Please type Exam to take an Exam or输入 QUIT 退出");但是在建立连接后,服务器没有向客户端发送类似“输入你的名字”这样的消息
为什么?
【问题讨论】:
标签: java networking