Java套接字没有接收到来自Print Writer的响应



服务器从这里开始:

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(" ");
}

由于某种原因,它似乎被冻结在最后一个用Stringpassed=input.readLine()发布的方法中;

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

在为服务器和客户端的outputStream写入out.flush()之后,添加对它的调用,如这里的

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

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

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

但根据文件:如果启用了自动刷新,则只有在调用println、printf或format方法之一时才会执行,而不是在输出换行符时执行。

所以不要被所困扰

最新更新