无法在Java中运行多线程服务器程序
这是服务器代码无法在Java中运行多线程服务器程序,java,sockets,Java,Sockets,这是服务器代码 package echoserver; import java.net.*; import java.io.*; public class EchoServer { public static void main(String[] args) { try { //establish server socket ServerSocket s = new ServerSocket(1981);
package echoserver;
import java.net.*;
import java.io.*;
public class EchoServer {
public static void main(String[] args) {
try {
//establish server socket
ServerSocket s = new ServerSocket(1981);
//Thread client connectionsincoming
while (true) {
//wait for incoming connection
Socket incoming = s.accept();
Runnable r = new ThreadedEchoHandler(incoming);
Thread t = new Thread(r);
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package echoserver;
import java.net.*;
import java.util.*;
import java.io.*;
class ThreadedEchoHandler implements Runnable {
public ThreadedEchoHandler(Socket i) {
//initializing socket
incoming = i;
}
public void run() {
try {
try {
//recieve input stream from socket
InputStream inStream = incoming.getInputStream();
//recieve output stream from socket
OutputStream outStream = incoming.getOutputStream();
//Create a scanner from input stream
Scanner scan = new Scanner(inStream);
//Create printer writer from output stream and enabled auto flushing
PrintWriter out = new PrintWriter(outStream, true);
//prompt users on how to exit program soon as a long in into the server
out.println("Enter BYE to exit");
boolean done = false;
//while done is not true and scanner has next line loop
while (!done && scan.hasNextLine()) {
//reading text that came in from the socket
String line = scan.nextLine();
//On the server print the ip address of where the text is coming from and the text they typed
System.out.println("Recieved from " + incoming.getInetAddress().getHostAddress() + ": " + line);
//Echo back the text the client typed to the client
out.println("Echo: " + line);
//if they type BYE in caps terminate there connection and I also trimmed whitespaces
if (line.trim().equals("BYE")) {
done = true;
}
}
} //finally close the socket connection
finally {
incoming.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private Socket incoming;
}
这是客户端的代码
package client;
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] args) throws IOException {
PrintWriter out = null;
try {
Socket s = new Socket(InetAddress.getLocalHost(), 1981);
System.out.println("Connected to server on port 1981");
out = new PrintWriter(s.getOutputStream());
out.println("Hello");
s.close();
} catch (Exception ex) {
System.err.println(ex.getMessage());
}
}
}
sockets已成功创建,但当控件转到t.start()方法调用时,它没有调用ThreadedEchoHandler类的run()方法
为什么会这样?有什么想法吗?我猜acept语句会永远阻塞,因为没有客户端连接到服务器。您可以将accept()包装在打印件中以证明或反驳。客户机将
“Hello”
写入PrintWriter
。到目前为止,一切顺利
您可能希望,PrintWriter
会将此文本直接发送到套接字,但它不会。PrintWriter(OutputStream)
构造函数中的文档说明,它创建了一个PrintWriter
,而无需自动换行。这意味着您必须在需要实际发送某些内容时调用out.flush()
在您调用
out.flush()
之前,文本只存在于某个内部缓冲区中,服务器将无法看到它。如果我们尝试调试它,则控制转到t.start(),然后再次转到s.accept()。它不叫跑步method@Nilesh巴拉伊,你怎么知道的?如果使用调试器单步执行代码,则不会,调试器不会切换到新创建的线程,您必须在该代码中设置断点。找到它。。。它正在调用run方法。在while循环scan.hasNextLine()中的run()方法中,没有要扫描的内容,它退出。所以我认为客户有问题