gpt4 book ai didi

java - 多线程中的套接字 "deadlocked"Java

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

我试图在同一进程上启动服务器和客户端线程,但似乎服务器线程正在阻塞客户端线程(反之亦然)。我不允许在这些线程之间使用任何全局变量(例如信号量或互斥体,因为客户端和服务器线程是由我无权访问的上层启动的)。

我发现了类似的问题here ,但它仍然使用两个不同的进程(两个主要函数)。

这是我的代码示例

服务器代码:

public class MyServer implements Runnable{

ServerSocket server;
Socket client;
PrintWriter out;
BufferedReader in;

public MyServer() throws IOException{
server = new ServerSocket(15243, 0, InetAddress.getByName("localhost"));
}

@Override
public void run() {
while(true){
try {

ArrayList<String> toSend = new ArrayList<String>();

System.out.println("I'll wait for the client");
client = server.accept();
out = new PrintWriter(client.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(client.getInputStream()));

String inputLine;
while((inputLine = in.readLine()) != null){
toSend.add("answering : "+inputLine);
}

for(String resp : toSend){
out.println(resp);
}

client.close();
out.close();
in.close();

} catch (IOException ex) {
}

}
}
}

客户端代码:

public class MyClient implements Runnable{

Socket socket;
PrintWriter out;
BufferedReader in;

public MyClient(){
}

@Override
public void run() {
int nbrTry = 0;
while(true){
try {
System.out.println("try number "+nbrTry);
socket = new Socket(InetAddress.getByName("localhost"), 15243);

out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

out.println("Hello "+nbrTry+" !! ");

String inputLine;
while((inputLine = in.readLine()) != null){
System.out.println(inputLine);
}
nbrTry++;
} catch (UnknownHostException ex) {
} catch (IOException ex) {
}
}
}
}

以及所谓的上层类(Class)启动这些线程:

public class TestIt {

public static void main(String[] argv) throws IOException{
MyServer server = new MyServer();
MyClient client = new MyClient();
(new Thread(server)).start();
(new Thread(client)).start();
}
}

它给我作为输出:

I'll wait for the client
Try number 0

它就卡在这里了。我应该怎样做才能保持服务器和客户端代码运行?谢谢。

最佳答案

我愿意回答你的问题,但基本上你需要更仔细地思考你的逻辑。

MyServer.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServer implements Runnable {

ServerSocket server;

public MyServer() throws IOException {
server = new ServerSocket(15243, 0, InetAddress.getByName("localhost"));
}

@Override
public void run() {
while (true) {
try {
// Get a client.
Socket client = server.accept();

// Write to client to tell him you are waiting.
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
out.println("[Server] I'll wait for the client");
// Let user know something is happening.
System.out.println("[Server] I'll wait for the client");

// Read from client.
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String inputLine = in.readLine();

// Write answer back to client.
out.println("[Server] Answering : " + inputLine);

// Let user know what it sent to client.
System.out.println("[Server] Answering : " + inputLine);

in.close();
out.close();
client.close();
} catch (Exception e) {

}
}
}
}

MyClient.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class MyClient implements Runnable {

Socket socket;
PrintWriter out;
BufferedReader in;

public MyClient() throws UnknownHostException, IOException {
}

@Override
public void run() {
int nbrTry = 0;
while (true) {
try {
// Get a socket
socket = new Socket(InetAddress.getByName("localhost"), 15243);

// Wait till you can read from socket.
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine = in.readLine();
//inputLine contains the text '[Server] I'll wait for the client'. means that server is waiting for us and we should respond.

// Write to socket
out = new PrintWriter(socket.getOutputStream(), true);
out.println("[Client] Hello " + nbrTry + " !! ");

// Let user know you wrote to socket
System.out.println("[Client] Hello " + nbrTry++ + " !! ");

} catch (UnknownHostException ex) {
} catch (IOException ex) {
}
}
}
}

TestIt.java

import java.io.IOException;

public class TestIt {

public static void main(String[] argv) throws IOException {
MyServer server = new MyServer();
MyClient client = new MyClient();
(new Thread(server)).start();
(new Thread(client)).start();
}
}

关于java - 多线程中的套接字 "deadlocked"Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15500872/

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