gpt4 book ai didi

java - 在 Java 和 Android 中通过套接字获取服务器响应

转载 作者:行者123 更新时间:2023-11-29 21:12:17 25 4
gpt4 key购买 nike

我正在尝试编写一个 Android 客户端和一个 Java 服务器。

它可以从 Android 客户端向服务器发送消息(如果测试代码被注释掉),但我无法从服务器向客户端返回响应。

你能帮帮我吗?

这是客户端:

public class MainActivity extends Activity {

private PrintWriter printwriter;
private EditText textField;
private TextView textView;
private Button button;
private String message;
private String serverResponse;
String fromServer;
Socket clientSocket;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textField = (EditText) findViewById(R.id.editText1);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button1);

final String hostName = "10.111.207.253";
final int portNumber = 4444;

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
message = textField.getText().toString();
textField.setText("");

new Thread(new Runnable() {
public void run() {
try {
clientSocket = new Socket(hostName, portNumber);
printwriter = new PrintWriter(clientSocket.getOutputStream(),true);
printwriter.write(message);

// testing only - trying to get the response back from the server
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while(true) {
if ((serverResponse = in.readLine()) != null) {
Log.i("server says", serverResponse);
break;
}
}
// testing only

printwriter.flush();
printwriter.close();
clientSocket.close();

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

}).start();

textView.setText("Server: " + serverResponse);
}
});
}
}

这是服务器:

public class MyServer {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;

public static void main(String[] args) {
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
}

System.out.println("Server started. Listening to the port 4444");

while (true) {
try {
clientSocket = serverSocket.accept(); //accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); //get the client message
message = bufferedReader.readLine();

if (message != null) {
System.out.println("Client says: " + message);

//testing only
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println("response from server");
//testing only

inputStreamReader.close();
clientSocket.close();
}

} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}
}

请帮忙!

最佳答案

在刷新客户端的 PrintWriter 之前,您已插入代码以读取来自服务器的回复。您没有使用 println(),因此您传递给构造函数以启用自动刷新的 boolean 值并不适用,如 in the javadoc 所述:

Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output. These methods use the platform's own notion of line separator rather than the newline character.

这意味着客户端永远不会发送任何东西,因此服务器永远不会收到任何东西。回到客户端,您将永远阻塞等待永远不会到来的回复。

要么使用 printwriter.println() 发送您的消息,要么将 printwriter.flush() 移动到您尝试从服务器读取回复之前。

关于java - 在 Java 和 Android 中通过套接字获取服务器响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22438808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com