[英]Java client-socket:Cannot receive messages from server to client
So there this problem that has been giving me headaches for days now.I am making a multi-user chat application.My design is as follows: 1.There is a login window. 所以这个问题现在已经让我头疼了几天。我正在制作一个多用户聊天应用程序。我的设计如下:1.有一个登录窗口。 2.As soon as the details are entered, the client-side chat window opens.
2.输入详细信息后,客户端聊天窗口将打开。 3.Now the user starts typing.
3.现在用户开始输入。 4.As soon as he hits enter or clicks on the send button,the message is sent to the server.
4.一旦他按下回车键或单击发送按钮,该消息就会发送到服务器。 5.The server sends it to all clients, including the one that send it the original message.
5,服务器将其发送给所有客户端,包括向其发送原始消息的客户端。
The problem:I am unable to receive any messages from the server to the client. 问题:我无法从服务器接收到客户端的任何消息。
Here is my server class: 这是我的服务器类:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server implements Runnable {
static InetAddress address;
static ArrayList<Integer> clients=new ArrayList<Integer>();
static ArrayList<Socket> socs=new ArrayList<>();
static String message="";
static DataOutputStream toClient;
static ServerSocket socket;
static Socket socketNew;
static boolean running=false;
public static void main(String[] args) throws IOException
{
socket=new ServerSocket(8000);
System.out.println("Server started on port 8000");
running=true;
while(true)
{
socketNew=socket.accept();
socs.add(socketNew);
address=socketNew.getInetAddress();
System.out.println("connected to client at address: "+address);
Server server=new Server();
new Thread(server).start();
}
}
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socketNew.getInputStream()));
String message;
PrintWriter out;
while ((message = br.readLine()) != null) {
System.out.println(message);
for (Socket s : socs) // sending the above msg. to all clients
{
out = new PrintWriter(s.getOutputStream(), true);
out.write(message);
out.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
And here is the receive_message function in the client class.Note that this method,I've run on a separate thread that starts as soon as the user logs-in. 这是客户端类中的receive_message函数。请注意,此方法在一个单独的线程上运行,该线程在用户登录后立即启动。
public void receive_data()
{while(true)
{
try {
BufferedReader in;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(in.readLine()!=null)
{
System.out.println(in.readLine());
console(in.readLine());
}
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
Any suggestions?Thanks for your time. 有什么建议吗?谢谢您的时间。 :-)
:-)
You are writing messages without a line ending, while your client is waiting for a line ending character in the readLine loop. 您正在编写没有行尾的消息,而客户端正在等待readLine循环中的行尾字符。 By placing
out.write('\\n')
in your server send loop, it will also send a newline character. 通过在服务器的send循环中放置
out.write('\\n')
,它还将发送换行符。
Example: 例:
for (Socket s : socs) {
out = new PrintWriter(s.getOutputStream(), true);
out.write(message);
out.write('\n'); // added this line
out.flush();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.