gpt4 book ai didi

java - 无法在 Java 中运行多线程服务器程序

转载 作者:行者123 更新时间:2023-11-30 09:38:35 26 4
gpt4 key购买 nike

这是服务器代码

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

Socktes 已成功创建,但当控制转到 t.start() 方法调用时,它不会调用 ThreadedEchoHandler 类的 run() 方法。

为什么会这样?有什么想法吗?

最佳答案

客户端将"Hello" 写入PrintWriter。到目前为止,还不错。

您可能希望 PrintWriter 将此文本直接发送到套接字,但它没有。 PrintWriter(OutputStream) 构造函数的文档说它创建了一个 PrintWriter 没有自动行刷新。这意味着无论何时您想要实际发送某些内容,都必须调用 out.flush()

在您调用 out.flush() 之前,文本仅存在于某个内部缓冲区中,服务器将无法看到它。

关于java - 无法在 Java 中运行多线程服务器程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10042609/

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