Java 服务器未接收数据/客户端未发送数据
我正在制作一个聊天程序。问题发生在连接阶段。理论上,连接是这样发生的:Java 服务器未接收数据/客户端未发送数据,java,networking,socket.io,client-server,Java,Networking,Socket.io,Client Server,我正在制作一个聊天程序。问题发生在连接阶段。理论上,连接是这样发生的: [服务器]接受()传入连接 [客户端]尝试连接到服务器 [服务器和客户端]初始化IO [客户端]发送带有后缀的用户名,该后缀指示是否有密码 [服务器]接收用户名,检查是否有传入密码(如果有,也请阅读) [服务器]如果客户端已注册,则签入数据库 [服务器]。。。(多次检查客户账户等) [服务器]如果客户机被接受或未被接受,则向客户机发送答复 [客户]接收并解释答案 请注意,步骤6和7尚未实现,服务器跳过直接创建新客户机帐户并在
public void establishConnection() {
Socket client = null;
BufferedReader input = null;
PrintWriter output = null;
String name = null;
String password = null;
try {
client = server.accept();
System.out.println("A connection is being established");
input = new BufferedReader(new InputStreamReader(client.getInputStream()));
output = new PrintWriter(client.getOutputStream());
//LOCATION OF THE BUG
//DESCRIPTION: If the name has been sent, the server seems not to receive it.
System.out.println("Waiting for the client's name...");
name = input.readLine();
if (name.charAt(name.length() - 1) == Keywords.PASSWORD) {
System.out.println("Waiting for the client's password...");
password = input.readLine();
} else
System.out.println("The client has no password");
name = name.substring(0, name.length() - 1);
System.out.println("Creation of the metaClient");
MetaClient metaClient = new MetaClient(name);
if (password != null)
metaClient.setPassword(password);
metaClient.setSocket(client);
System.out.println("Activating listening thread");
metaClient.activateCommunications(this, this);
/* If there is no already registered clients,
* the first to connect shall be the owner.
*/
if (clientsList.size() == 0)
metaClient.addRight(MetaClient.OWNER);
clientsList.add(metaClient);
output.write(Keywords.CONNECTION_ACCEPTED);
System.out.println("The connection process of user " + metaClient.getName()
+ " is complete.");
} catch (IOException e) {
System.out.println("Error when establishing a new connection");
e.printStackTrace();
} finally {
try {
if (input != null)
input.close();
if (output != null)
output.close();
} catch (IOException e) {
System.out.println("Error when closing the IO of the new connection.");
e.printStackTrace();
}
}
}
以下是尝试连接到服务器的客户端生成器的代码:
public ChatClient(String ip, int port, String name, String password) throws ConnectException {
this.name = name;
boolean connectionRefused = false;
try {
System.out.println("Establishing connection to the server...");
server = new Socket(ip, port);
BufferedReader input = new BufferedReader(new InputStreamReader(server.getInputStream()));
PrintWriter output = new PrintWriter(server.getOutputStream());
//LOCATION OF THE BUG:
//BUG DESCRIPTION: The name seems not to be sent to the server
System.out.println("Sending name to the server...");
if (password == null || password == "") {
name += Keywords.NO_PASSWORD;
output.println(name);
} else {
name += Keywords.PASSWORD;
output.println(name);
System.out.println("Sending password to the server...");
output.println(password);
}
System.out.println("Waiting for the server's response...");
//Wait for the server's response
String response = input.readLine();
if (response.equals(Keywords.CONNECTION_ACCEPTED))
System.out.println("The connection has been accepted");
else
connectionRefused = true;
} catch (IOException e) {
System.out.println("Error when connecting to the server");
e.printStackTrace();
System.exit(1);
}
if (connectionRefused)
throw new ConnectException("The connection was refused by the server");
else {
communication = new CommunicationProtocol(server, this, this);
communication.start();
}
}
任何帮助都将非常感谢,这让我花了相当长的时间来解决这个问题
编辑回答恐怖袋熊的评论:
是的,一台机器运行服务器,另一台运行客户端。但是,即使两个程序在同一台机器上作为两个独立的程序运行,也会发生错误。客户端和服务器是否在两个不同的进程/线程中?@ScaryWombat我对帖子进行了编辑,解释了它是如何运行的。简言之:是的。它们实际上在两个不同的应用程序中运行,一个用于最终用户,另一个用于网络所有者。抛出了什么错误?@ScaryWombat没有抛出错误,都只是一直等待对方的名称/答案。readLine()是一个阻塞函数,它等待所有信息到达后再继续。如果您指的是客户端构造函数可以抛出的ConnectException,那么如果服务器拒绝连接,则会抛出该异常,通常是因为客户端已被禁止或是黑名单的一部分。