java中套接字读取输入时While循环失败
因此,我在读取客户端输入时遇到了问题。每当我在服务器类中使用if语句而不使用while语句时,它都可以完全正常工作。有人能告诉我为什么这会失败吗 服务器类:java中套接字读取输入时While循环失败,java,sockets,while-loop,client-server,client,Java,Sockets,While Loop,Client Server,Client,因此,我在读取客户端输入时遇到了问题。每当我在服务器类中使用if语句而不使用while语句时,它都可以完全正常工作。有人能告诉我为什么这会失败吗 服务器类: import java.io.*; import java.net.*; public class Server { public static void main(String[] args) throws Exception { Server myServer = new Server();
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws Exception
{
Server myServer = new Server();
myServer.run();
}
public void run() throws Exception
{
//Initializes the port the serverSocket will be on
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("The Server is waiting for a client on port 9999");
//Accepts the connection for the client socket
Socket socket = serverSocket.accept();
InputStreamReader ir = new InputStreamReader(socket.getInputStream());
BufferedReader br = new BufferedReader(ir);
String message = br.readLine();
//Confirms that the message was received
System.out.println(message);
//When this while is here. The match fails and it goes to the else statement.
//Without the while statement it will work and print "Received our hello message."
//when the client says HELLO.
while(message != null)
{
if(message.equals("HELLO"))
{
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.println("Received our hello message.");
}
else
{
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.println("Did not receive your hello message");
}
}
}
}
客户端类:
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
public static void main(String[] args) throws Exception
{
Client myClient = new Client();
myClient.run();
}
public void run() throws Exception
{
Socket clientSocket = new Socket("localhost", 9999);
//Sends message to the server
PrintStream ps = new PrintStream(clientSocket.getOutputStream());
Scanner scan = new Scanner(System.in);
String cMessage = scan.nextLine();
ps.println(cMessage);
//Reads and displays response from server
InputStreamReader ir = new InputStreamReader(clientSocket.getInputStream());
BufferedReader br = new BufferedReader(ir);
String message = br.readLine();
System.out.println(message);
}
}
您永远不会在while循环中修改消息,因此您有一个无限循环 试一试
while((message=br.readLine())!=null)
您永远不会在while循环中修改消息,因此您有一个无限循环
试一试
while((message=br.readLine())!=null)
您的代码在发送一条“HELLO”消息时工作正常。
然而,正如^Tyler所指出的,如果您想继续发送消息,则需要在while循环中移动“while((message=br.readLine())!=null)”。您的代码在发送一条“HELLO”消息时运行良好。
然而,正如^Tyler所指出的,如果你想继续发送消息,你需要在while循环中移动while((message=br.readLine())!=null)。你只在服务器端循环,而你忘记在客户端循环,我为你做了一个快速修复,还帮你关闭了连接 Server.java
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws Exception
{
Server myServer = new Server();
myServer.run();
}
public void run() throws Exception
{
//Initializes the port the serverSocket will be on
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("The Server is waiting for a client on port 9999");
//Accepts the connection for the client socket
Socket socket = serverSocket.accept();
InputStreamReader ir = new InputStreamReader(socket.getInputStream());
BufferedReader br = new BufferedReader(ir);
String message;
//= br.readLine();
//Confirms that the message was received
//When this while is here. The match fails and it goes to the else statement.
//Without the while statement it will work and print "Received our hello message."
//when the client says HELLO.
PrintStream ps = new PrintStream(socket.getOutputStream());
while((message =br.readLine())!=null)
{
System.out.println(message);
if(message.equals("HELLO"))
{
ps.println("Received our hello message.");
}
if(message.equals("END"))
{
ps.println("Client ended the connection");
break;
}
else
{
ps.println("Did not receive your hello message");
}
}
ps.close();
br.close();
ir.close();
serverSocket.close();
}
}
Client.java
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
public static void main(String[] args) throws Exception
{
Client myClient = new Client();
myClient.run();
}
public void run() throws Exception
{
Socket clientSocket = new Socket("localhost", 9999);
//Sends message to the server
PrintStream ps = new PrintStream(clientSocket.getOutputStream());
Scanner scan = new Scanner(System.in);
String cMessage ="";
InputStreamReader ir = new InputStreamReader(clientSocket.getInputStream());
BufferedReader br = new BufferedReader(ir);
while(!(cMessage.trim().equals("END"))){
cMessage = scan.nextLine();
ps.println(cMessage);
//Reads and displays response from server
String message = br.readLine().trim();
System.out.println(message);
}
br.close();
ir.close();
scan.close();
ps.close();
clientSocket.close();
}
}
您只在服务器端循环,而您忘记在客户端循环,我为您做了一个快速修复,并帮助您关闭连接 Server.java
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws Exception
{
Server myServer = new Server();
myServer.run();
}
public void run() throws Exception
{
//Initializes the port the serverSocket will be on
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("The Server is waiting for a client on port 9999");
//Accepts the connection for the client socket
Socket socket = serverSocket.accept();
InputStreamReader ir = new InputStreamReader(socket.getInputStream());
BufferedReader br = new BufferedReader(ir);
String message;
//= br.readLine();
//Confirms that the message was received
//When this while is here. The match fails and it goes to the else statement.
//Without the while statement it will work and print "Received our hello message."
//when the client says HELLO.
PrintStream ps = new PrintStream(socket.getOutputStream());
while((message =br.readLine())!=null)
{
System.out.println(message);
if(message.equals("HELLO"))
{
ps.println("Received our hello message.");
}
if(message.equals("END"))
{
ps.println("Client ended the connection");
break;
}
else
{
ps.println("Did not receive your hello message");
}
}
ps.close();
br.close();
ir.close();
serverSocket.close();
}
}
Client.java
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
public static void main(String[] args) throws Exception
{
Client myClient = new Client();
myClient.run();
}
public void run() throws Exception
{
Socket clientSocket = new Socket("localhost", 9999);
//Sends message to the server
PrintStream ps = new PrintStream(clientSocket.getOutputStream());
Scanner scan = new Scanner(System.in);
String cMessage ="";
InputStreamReader ir = new InputStreamReader(clientSocket.getInputStream());
BufferedReader br = new BufferedReader(ir);
while(!(cMessage.trim().equals("END"))){
cMessage = scan.nextLine();
ps.println(cMessage);
//Reads and displays response from server
String message = br.readLine().trim();
System.out.println(message);
}
br.close();
ir.close();
scan.close();
ps.close();
clientSocket.close();
}
}
使用像你这样的循环
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
sleep(in);
if(!in.ready()){
break;
}
}
我发现了一种作弊的方法
private static void sleep(BufferedReader in) throws IOException {
long time = System.currentTimeMillis();
while(System.currentTimeMillis()-time < 1000){
if(in.ready()){
break;
}
}
}
私有静态无效睡眠(BufferedReader in)引发IOException{
长时间=System.currentTimeMillis();
而(System.currentTimeMillis()-时间<1000){
if(in.ready()){
打破
}
}
}
这可能有点草率,但如果您使用一个等待一段时间的睡眠方法,然后继续检查BufferedReader是否“就绪”。如果是,您可以中断,但当您出来时,请再次检查。-也许你可以只返回一个布尔值,而不是检查两次,但概念就在那里。像你这样使用循环
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
sleep(in);
if(!in.ready()){
break;
}
}
我发现了一种作弊的方法
private static void sleep(BufferedReader in) throws IOException {
long time = System.currentTimeMillis();
while(System.currentTimeMillis()-time < 1000){
if(in.ready()){
break;
}
}
}
私有静态无效睡眠(BufferedReader in)引发IOException{
长时间=System.currentTimeMillis();
而(System.currentTimeMillis()-时间<1000){
if(in.ready()){
打破
}
}
}
这可能有点草率,但如果您使用一个等待一段时间的睡眠方法,然后继续检查BufferedReader是否“就绪”。如果是,您可以中断,但当您出来时,请再次检查。-也许您可以只返回一个布尔值,而不是检查两次,但概念就在那里。什么是不起作用的?我编译了你的代码,在客户端输入hello后,我收到了“Received our hello message”。当我取出while循环时,它就会工作。除了当我将它保留在中时,当它收到HELLO并在服务器窗口中打印它时,什么都不会发生。一个好的做法是在程序结束时关闭所有套接字和读卡器。它有什么不起作用的?我编译了你的代码,在客户端输入hello后,我收到了“Received our hello message”。当我取出while循环时,它就会工作。除非我将其保留为空,否则当它收到HELLO并在服务器窗口中打印它后,将不会发生任何情况。一个好的做法是在程序结束时关闭所有套接字和读卡器。我尝试过这样做,因此当客户端说HELLO时。。服务器收到它并在服务器窗口中打印HELLO,然后什么也不发生。如果我尝试在客户端到服务器之间键入任何其他内容,它不会打印收到的内容。@Shawn,假设不会发生任何事情,您只会循环br.readline,而忘记循环ps.printline。我尝试了,因此当客户端打招呼时。。服务器收到它并在服务器窗口中打印HELLO,然后什么也不发生。如果我尝试从客户端到服务器键入任何其他内容,它不会打印收到的内容。@Shawn,假设不会发生任何事情,您只循环br.readline,而忘记循环ps.printlineOk,因此如果我将其移动到while语句(这两行):String message=br.readline();System.out.println(消息);它将打印出我们收到的问候信息。但是当它在Server.run(第30行)和Server.main(第9行)上的线程“main”java.lang.NullPointerException中出现异常时失败。Shawn,您一定忘了在while循环外删除br.readLine(),因为您正在while循环中使用它。这就是为什么它只是打印,甚至不进入while循环!我刚才提到你应该阅读while循环中的消息。我会编辑我的帖子,让它更清晰!还记得在客户端循环输入。好的,如果我将其移动到while语句(这两行):String message=br.readLine();System.out.println(消息);它将打印出我们收到的问候信息。但是当它在Server.run(第30行)和Server.main(第9行)上的线程“main”java.lang.NullPointerException中出现异常时失败。Shawn,您一定忘了在while循环外删除br.readLine(),因为您正在while循环中使用它。这就是为什么它只是打印,甚至不进入while循环!我刚才提到你应该阅读while循环中的消息。我会编辑我的帖子,让它更清晰!还记得在客户端循环输入。Awesome工作得很好。。。只需为消息添加else if==以再见。谢谢你的帮助,先生,标记为回答。真棒工作完美。。。只需为消息添加else if==以再见。谢谢您的帮助,先生,标记为已回答。