我的 Java 服务器端没有从 Java 客户端读取更多的传入消息
My Java server Side not reading more one incoming message from Java Client
我在 Java 中创建简单的服务器-客户端程序
我的服务器是
public class Server {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static BufferedReader bufferedReader;
private static String inputLine;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try
{
serverSocket = new ServerSocket(5000);
clientSocket = serverSocket.accept();
// Create a reader
bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// Get the client message
while((inputLine = bufferedReader.readLine()) != null)
System.out.println(inputLine); //Print
}catch(IOException e){
System.out.println(e);
}
}
}
客户端是:
public class Client {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String hostName = "localhost";
try (
Socket echoSocket = new Socket(hostName, 5000);
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in= new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " + hostName);
System.exit(1);
}
}
}
为什么我的服务器不再读取?当我调试程序时,我的程序在服务器中时会跳出循环……我不知道为什么
你的服务器似乎没问题。您的服务器无法多次读取的原因是客户端:
这一行
System.out.println("echo: " + in.readLine());
客户端需要服务器的答复,但您的服务器没有返回任何内容。所以删除上面的行或者让服务器发送响应。
您的客户端没有向服务器发送任何新行。尝试从服务器读取响应时卡住了。
- System.out.println("echo: " + in.readLine()); *
你有两种选择来解决这个问题
1 - 将服务器中的响应发送回客户端
2 - 删除 in.readLine()
祝你好运
好的,现在我的服务器 运行 完美..现在我在 android 中有一个客户端,当我发送一条消息时,服务器程序向我显示消息,然后服务器终止>..这是 android 中的程序 当我按下按钮时
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
messsage = textField.getText().toString(); // get the text message on the text field
textField.setText(""); // Reset the text field to blank
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
}
private class SendMessage extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
client = new Socket("192.168.2.3", 5000); // connect to the server
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(messsage); // write the message to output stream
printwriter.flush();
printwriter.close();
// client.close(); // closing the connection
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
我在 Java 中创建简单的服务器-客户端程序 我的服务器是
public class Server {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static BufferedReader bufferedReader;
private static String inputLine;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try
{
serverSocket = new ServerSocket(5000);
clientSocket = serverSocket.accept();
// Create a reader
bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// Get the client message
while((inputLine = bufferedReader.readLine()) != null)
System.out.println(inputLine); //Print
}catch(IOException e){
System.out.println(e);
}
}
} 客户端是:
public class Client {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String hostName = "localhost";
try (
Socket echoSocket = new Socket(hostName, 5000);
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in= new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " + hostName);
System.exit(1);
}
}
}
为什么我的服务器不再读取?当我调试程序时,我的程序在服务器中时会跳出循环……我不知道为什么
你的服务器似乎没问题。您的服务器无法多次读取的原因是客户端:
这一行System.out.println("echo: " + in.readLine());
客户端需要服务器的答复,但您的服务器没有返回任何内容。所以删除上面的行或者让服务器发送响应。
您的客户端没有向服务器发送任何新行。尝试从服务器读取响应时卡住了。
- System.out.println("echo: " + in.readLine()); *
你有两种选择来解决这个问题
1 - 将服务器中的响应发送回客户端 2 - 删除 in.readLine()
祝你好运
好的,现在我的服务器 运行 完美..现在我在 android 中有一个客户端,当我发送一条消息时,服务器程序向我显示消息,然后服务器终止>..这是 android 中的程序 当我按下按钮时
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
messsage = textField.getText().toString(); // get the text message on the text field
textField.setText(""); // Reset the text field to blank
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
}
private class SendMessage extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
client = new Socket("192.168.2.3", 5000); // connect to the server
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(messsage); // write the message to output stream
printwriter.flush();
printwriter.close();
// client.close(); // closing the connection
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}