需要为每个连接创建一个新线程。线程应该具有写入套接字的功能,即使它在 run() 等待客户端输入:String clientCommand = in.readLine()
。该函数update
应满足此要求并且能够使用PrintWriter outWr
但它似乎是写入套接字null
,即使在线程构造函数中设置它也是如此。无法PrintWriter outWr
在构造函数中初始化。待try
施工后。如何解决?outWr
null
public class Server {
public static void main(String[] args) {
...
ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++, auction);
...
}
public class ClientServiceThread extends Thread {
private PrintWriter outWr;
ClientServiceThread(Socket s, int clientID, Auction a) {
try {
PrintWriter outWr = new PrintWriter(new OutputStreamWriter(
m_clientSocket.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {....; String clientCommand = in.readLine(); ... }
public void update(String msg){outWr.println(msg);}