好的,我正在尝试制作一个简单的聊天服务器和客户端。这个程序是聊天服务器。我有两个正在运行的线程。第一个线程处理所有客户端并使用 ServerSocket 将每个新客户端绑定到一个新套接字,第二个线程现在允许用户向连接的客户端发送消息。但是,我无法从该线程中获取用户输入!语句“String inputvar = br.readLine()”不会执行!我究竟做错了什么?
public void relay() throws IOException
{
new Thread(new Runnable()
{
@Override
public void run()
{
try{
try
{
while (true)
{
Socket socket2 = null;
Socket socket1 = socketmethod(socket2,0);
System.out.println(socket1);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (socket1 != null)
{
PrintWriter outputriver = new PrintWriter(socket.getOutputStream(),true);
boolean cfd = true;
while(cfd==true)
{
System.out.println("this worked");
System.out.print("Message: ");
String inputvar = br.readLine();
System.out.println("this worked2");
if (inputvar.equals("disconnect_now"))
{
cfd = false;
}
outputriver.println("Unknown User: " + inputvar);
}
}
}
}
finally
{
System.out.println("error 1");
}
}
catch (IOException e)
{
System.out.println("Unexpected error: IOException in thread2");
}
}
}).start();