gpt4 book ai didi

Java 套接字未收到来自 Printwriter 的响应

转载 作者:行者123 更新时间:2023-12-01 13:09:42 25 4
gpt4 key购买 nike

服务器从这里启动:

public static void main(String[] args){
System.out.println("Server has started");
try {
ServerSocket socket = new ServerSocket(17000);
while(true){
ThreadedClass w;
w = new ThreadedClass(socket.accept());
Thread t = new Thread(w);
t.start();
}
} catch (IOException e) {
System.out.print("Failed");
// TODO Auto-generated catch block
e.printStackTrace();
}
}

然后这个类:

package com.sandislandsrv.rourke750;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ThreadedClass implements Runnable{

private Socket socket;

public ThreadedClass(Socket socket){
this.socket = socket;
}

@Override
public void run() {
MysqlDataClass db = Start.getData();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
String cred = in.readLine();
String[] creds = cred.split(" ");
System.out.print(creds[0] + creds[1]);
boolean authenticate = db.getUsernamePassValid(creds[0], creds[1]);
if (!authenticate){
System.out.println("Failed to log in, bad password");
out.println("BAD");
out.close();
return;
}
out.println("GOOD");
String line;
while ((line = in.readLine()) != null){
if (line.equals("END")){
out.close();
return;
}
if (line.equals("GET")){
out.println(db.getMessages(creds[0]));
}
if (line.equals("IN")) break;
if (line.equals("REMOVE")){
line = in.readLine();
db.removeMessage(creds[0], line);
}
}
line = in.readLine();
String[] format = line.split("Theamjgfdngkngfd8998906504906595665");
String[] timeformat = format[1].split(":");
long time = System.currentTimeMillis()/1000;
if (Long.parseLong(timeformat[0]) != 0)
time += 3600 * Long.parseLong(timeformat[0]);
if (Long.parseLong(timeformat[1]) != 0)
time += 60 * Long.parseLong(timeformat[1]);
if (Long.parseLong(timeformat[2]) != 0)
time += Long.parseLong(timeformat[2]);
db.addMessage(creds[0], format[0], time);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void remove(){

}

}

然后我调用这个方法

public String[] getNames(){
StringBuilder builder = new StringBuilder();
try {
Socket socket = new Socket("share.betterassociations.com", 17000);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println(username.getText().toString() + " " + password.getText().toString());
System.out.print("test");
String passed = input.readLine();
System.out.print("test2");
if (passed.equals("BAD")) {
System.out.print("fail");
loginerror.setVisible(true);
socket.close();
return null;
}
System.out.print("test2");
out.println("GET");
String line;
while ((line = input.readLine()) != null){
if (line.equals("END")) break;
builder.append(line);
}
out.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return builder.toString().split(" ");
}

由于某种原因,它似乎被卡住在最后一个方法中,其中传递了 String = input.readLine();

我不明白为什么会发生这种情况,因为我正在从服务器向客户端发送一个字符串,但客户端没有收到它。

最佳答案

在为服务器和客户端的输出流写入 out.flush() 后添加对 out.flush() 的调用,如下所示

....
out.println("GOOD");
out.flush();
....

或者,通过在此处更改服务器(客户端已启用)来启用自动刷新:

....
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
....

但是根据文档: 如果启用自动刷新,则仅当调用 println、printf 或 format 方法之一时才会执行此操作,而不是在恰好输出换行符时执行。

所以不要被迷惑

关于Java 套接字未收到来自 Printwriter 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22977161/

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