我有基本的客户端 - 服务器聊天应用程序。服务器端似乎工作,当我通过telnet连接它时,它接收消息并将其发送回所有连接的客户端。我无法使用自己的客户端实现同样的目标。
从一开始,public class Server {
private Properties properties;
private ServerSocket serverSocket;
private Set<ClientConnection> clientConnections;
public Server() throws IOException {
clientConnections = new HashSet<>();
serverSocket = new ServerSocket(9999);
while(true){
Socket clientSocket = serverSocket.accept();
ClientConnection clientConnection = new ClientConnection(clientSocket, this);
clientConnections.add(clientConnection);
clientConnection.start();
}
}
public Set<ClientConnection> getClientConnections() {
return clientConnections;
}
}
类
ClientConnection
在每个连接上都创建了新的public class ClientConnection extends Thread {
private final Socket clientSocket;
private final Server server;
private OutputStream outputStream;
private InputStream inputStream;
public ClientConnection(Socket clientSocket, Server server) {
this.clientSocket = clientSocket;
this.server = server;
}
@Override
public void run(){
try {
handleClient();
} catch (IOException e) {
e.printStackTrace();
}
}
private void handleClient() throws IOException {
outputStream = clientSocket.getOutputStream();
inputStream = clientSocket.getInputStream();
outputStream.write("Hello from server".getBytes());
System.out.println("New client connected");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String incomingMessage;
while((incomingMessage = bufferedReader.readLine()) != null) {
for(ClientConnection connection : server.getClientConnections()) {
connection.getOutputStream().write(incomingMessage.getBytes());
}
System.out.println(incomingMessage);
}
clientSocket.close();
}
public OutputStream getOutputStream() {
return outputStream;
}
}
,在开始时,发送&#34; Hello来自服务器&#34;到新客户端(如果通过telnet连接工作)然后,侦听所有传入的消息并再次将它们广播到所有连接的客户端 - 如果telnet是客户端,则工作。
ServerConnection
然后,我获得了 public class ServerConnection{
private Socket socket;
private OutputStream outputStream;
private InputStream inputStream;
private BufferedReader bufferedReader;
private String host;
private int port;
public ServerConnection(String host, int port) {
this.host = host;
this.port = port;
}
public void connect() throws IOException {
socket = new Socket(host, port);
outputStream = socket.getOutputStream();
inputStream = socket.getInputStream();
outputStream.write("Hello from client".getBytes());
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String incommingMessage;
while((incommingMessage = bufferedReader.readLine()) != null) {
System.out.println(incommingMessage);
}
}
}
类
gets
它实际上是由服务器端注册的(打印&#34;新客户连接&#34;)但它没有接收到#34;来自客户端的问候&#34;并且客户端没有从服务器接收任何消息。
答案 0 :(得分:1)
我不会为你编写代码,因为你有你需要的东西。但这是一个流程,我花了一段时间才完全理解,这应该可以帮助你。
服务器:
Start -> Accept connection -> Read InStream -> Write OutStream (FLUSH THE TOILET OF DATA) -> Loop
客户:
Start -> Connect -> Write OutStream (FLUSH THE TOILET OF DATA) -> Read InStream -> Close Connection
答案 1 :(得分:1)
请尝试在邮件末尾发送新行,即:outputStream.write("Hello from client\r\n\".getBytes());
,因为您在服务器代码中使用bufferedReader.readLine()
。所以BufferedReader正在等待行结束,没有任何事情发生。