简体   繁体   English

Java客户端套接字:无法从服务器接收消息到客户端

[英]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.

相关问题 Java中的客户端套接字编程-从服务器端写入客户端套接字的麻烦 - Client-socket programming in Java - trouble with writing to the client socket from server side 客户端无法从服务器套接字编程Java接收消息 - Client not able to receive messages from Server Socket programming Java 目标C套接字服务器未收到Java套接字客户端消息 - Objective C Socket Server does not receive Java Socket Client Messages 客户端通过套接字从服务器接收消息时出现一些问题 - some problem when client receive messages from server though socket Java套接字客户端无法从节点服务器接收数据 - Java socket client can't receive data from Node Server C ++套接字服务器从Java客户端接收味精,但不是 - C++ socket server receive msg from Java client, but not otherwise Java套接字客户端无法接收来自C服务器的响应 - Java socket client can't receive response from C server 除非服务器套接字端口经过硬编码,否则客户端套接字无法从服务器套接字接收 - client socket cannot receive from server socket unless server socket port is hardcoded 客户端无法将消息发送到服务器Java套接字 - Client unable to send messages to server Java socket 客户端套接字将不会从服务器接收数据 - Client socket will not receive data from server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM