gpt4 book ai didi

java - 服务器收不到客户端的输入

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:17:36 25 4
gpt4 key购买 nike

我有一个服务器客户端。我的问题是,当客户端写入消息时,服务器没有收到消息。服务器代码

public class BankServer {

private int port;

private BufferedReader reader = null;
private PrintWriter writer = null;

public BankServer(int port)
{
this.port = port;
}

public void startServer() {
print("Contact server at: " + getServerAddress() + ": port: " + port);

while(true) {
try {
ServerSocket ss = new ServerSocket(port);
Socket client = ss.accept();

if(client != null) {
print("Client connected");
}

reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
writer = new PrintWriter(client.getOutputStream(), true);

String msg = null;

print("Server waiting for input");

while((msg = reader.readLine()) != null) {
print("Printer: " + msg);
writer.write(msg);
}

client.close();
ss.close();
closeStreams();

} catch (IOException e) {
e.printStackTrace();
}
}
}

private void closeStreams() {
if(reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
} }

if(writer != null) {
writer.close();
}
}

private String getServerAddress() {

InetAddress inetAdr = null;

try {
inetAdr = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}

String host = inetAdr.getHostAddress();

return host;
}

public void print(String msg) {
System.out.println(msg);
}

public static void main(String[] args) {
new BankServer(2222).startServer();
}

}

我有这段代码,服务器在其中等待输入:

ServerSocket ss = new ServerSocket(port);
Socket client = ss.accept();

if(client != null) {
print("Client connected");
}

从这段代码中,我可以看到建立了连接。但是当客户端发送消息时,服务器不会打印消息

这是客户端代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class BankClient {

private PrintWriter writer = null;
private BufferedReader reader = null;
private Socket socket;

private int port;
private String adr;

public BankClient(String address, int port)
{
adr = address;
this.port = port;

try {
socket = new Socket(adr, this.port);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream());


} catch (IOException e) {
e.printStackTrace();
}
}

public void writeMsg(String msg) {

try {
while((msg = reader.readLine()) != null) {
writer.print("From client: " + msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {

BankClient client = new BankClient("127.0.0.1", 2222);
Scanner scanner = new Scanner(System.in);

System.out.println("Waiting for input");

while(true) {
String msg = scanner.nextLine();
client.writeMsg(msg);

if(msg.equals("quit")) {
System.out.println("Bye");
break;
}
}

scanner.close();
}
}

这是我第一次使用流,而且我对编程也很陌生。我在 stackoverflowgoogle 上找到了很多帮助,但我就是想不通。感谢您提供的任何帮助

最佳答案

替换:

public void writeMsg(String msg) {

try {
while((msg = reader.readLine()) != null) {
writer.print("From client: " + msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}

作者:

public void writeMsg(String msg) {
try {
DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream());
outToServer.writeBytes("From client: " + msg + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}

客户端:

enter image description here

服务器端:

enter image description here

关于java - 服务器收不到客户端的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33086862/

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