gpt4 book ai didi

Java Client/Server 不通过 Socket/ServerSocket 进行通信

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

我有一个简单的 socket/serversocket例如,我正在尝试运行,但客户端和服务器在其 BufferedReader 时都挂起让我们试着阅读。以下是每个代码:

服务器

package picturePerfect;
--imports--

public class PictureServer implements Runnable{
static ServerSocket serverSocket;

public static void main(String[] args) throws IOException {
serverSocket = new ServerSocket(2342);
Thread firstSessionThread = new Thread(new PictureServer());
firstSessionThread.start();
}

@Override
public void run() {
Socket socket = null;
try {
socket = serverSocket.accept();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String clientRequest = bufferedReader.readLine();
System.out.println(clientRequest);
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
printWriter.println("Sent from server!");
} catch (IOException e) {
e.printStackTrace();
}
}

客户

package picturePerfect;
--imports--

public class PictureClient {
public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
Socket socket = new Socket("localhost", 2342);
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
printWriter.write("Sent from client!");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String response = bufferedReader.readLine();
System.out.println(response);
socket.close();
}
}

这是我可以将我的代码简化到的最基本的。我有一个我一直在关注的示例程序,它看起来几乎完全一样。这是示例服务器和客户端(确实工作):

示例服务器

--imports--

public class Server implements Runnable{
static ServerSocket ss;

public static void main(String[] args) throws IOException {
ss = new ServerSocket(3142);
Thread thread = new Thread(new Server());
thread.start();
}

@Override
public void run() {
while ( true ) {
Socket s = null;

try {
s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String operands = br.readLine();
System.out.println(operands + " was received");
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
pw.println(operands + " right back!");
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
}

示例客户端

--imports--

public class Server implements Runnable{
static ServerSocket ss;


public static void main(String[] args) throws IOException {
ss = new ServerSocket(3142);
Thread thread = new Thread(new Server());
thread.start();
}

@Override
public void run() {
while ( true ) {
Socket s = null;

try {
s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String operands = br.readLine();
System.out.println(operands + " was received");
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
pw.println(operands + " right back!");
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
}

我已经尝试将 while 循环放入我的服务器并将我的客户端和服务器移动到默认包,但都没有帮助。我也尝试使用 read()而不是 readLine() ,并以 \r\n 结束 printWriter 的行, 但在那里同样不成功。

为什么我的代码卡在 readLine() 上,尤其是当示例代码没有时?

最佳答案

readline() 所做的是等待直到它看到换行符,直到它返回,因此 readLine()。

在您的客户端中,您不写新行。你使用:

printWriter.write("Sent from client!");

相反,使用 println 将换行符写入流中,

printWriter.println("Sent from client!");

关于Java Client/Server 不通过 Socket/ServerSocket 进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23067911/

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