gpt4 book ai didi

java - TCP服务器接受连接后新线程的代码不执行

转载 作者:行者123 更新时间:2023-11-30 01:44:04 25 4
gpt4 key购买 nike

我有以下 TCP 服务器:

public class Server {

private Connection db;
private Statement statement;
private ServerSocket socket;

public static void main(String[] args) {
Server server = new Server();
server.initializeServer();
System.out.println("Server initialized");
server.listenConnections();
}

private void initializeServer() {
try {
db = DriverManager.getConnection("jdbc:mysql://localhost:3306/courseworkschema" +
"?verifyServerCertificate=false" +
"&useSSL=false" +
"&requireSSL=false" +
"&useLegacyDatetimeCode=false" +
"&amp" +
"&serverTimezone=UTC",
"Sergei",
"12345");
statement = db.createStatement();
socket = new ServerSocket(1024);
} catch (SQLException | IOException e) {
e.printStackTrace();
}
}

private void listenConnections() {
System.out.println("Listening connections ... ");
while (true) {
try {
Socket client = socket.accept();
new Thread(() -> {
System.out.println("Client accepted");
try {
OutputStream outputStream = client.getOutputStream();
InputStream inputStream = client.getInputStream();

String clientAction;
String queryContent;

boolean flag = true;

while (flag) {
byte[] msg = new byte[100];
int k = inputStream.read(msg);
clientAction = new String(msg, 0, k);
clientAction = clientAction.trim();
msg = new byte[100];
k = inputStream.read(msg);
queryContent = new String(msg, 0, k);
queryContent = queryContent.trim();
System.out.println(clientAction);
System.out.println(queryContent);

if (clientAction.equalsIgnoreCase("END")) {
flag = false;
}
else if (clientAction.equalsIgnoreCase("LOGIN")) {
System.out.println("Login action");
}
}
} catch (IOException e) {
e.printStackTrace();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

创建此服务器是为了与数据库通信。这是我尝试连接到该服务器的方式

public class LoginController {
private LoginWindow window;
private Socket socket;
private InputStream is;
private OutputStream os;

public LoginController() {
connectToServer();
}

public void logInUser(String login, String password) {
if (!login.isEmpty() && !password.isEmpty()) {
sendDataToServer("LOGIN");
sendDataToServer("");
} else {
window.showMessageDialog("Fill the fields!", JOptionPane.ERROR_MESSAGE);
}
}

public void attachView(LoginWindow window) {
this.window = window;
}

private void connectToServer() {
try {
socket = new Socket("127.0.0.1", 1024);
System.out.println("Connected");
} catch (IOException e) {
e.printStackTrace();
}
}

private void sendDataToServer(String res) {
try {
os = socket.getOutputStream();
os.write(res.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}

}

当我运行服务器然后运行客户端时,我在服务器中有这样的日志:

Server initialized
Listening connections ...

Process finished with exit code -1

所以,我不明白为什么服务器不等待并接受来自客户端的连接,而是在初始化和监听后关闭。那么,到底是怎么回事呢?我将不胜感激任何帮助。提前致谢!

UPD

当我运行我的应用程序时,它开始工作,但我发现 Thread block 中的代码未执行。我什至不明白,为什么会这样

最佳答案

在您的 private void ListenConnections() 中,您正在创建一个 Thread 对象,但您没有告诉它在创建后启动,因此它不会执行。

您的线程创建行应如下所示:

new Thread(() -> {
//your code
}).start();

来自 javadoc: https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start()

public void start()

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

Throws: IllegalThreadStateException - if the thread was already started.

See Also: run(), stop()

关于java - TCP服务器接受连接后新线程的代码不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58825056/

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