简体   繁体   English

如何实现聊天客户端

[英]How to implement a chat client

I'm having trouble interpreting a question for a college assignment. 我在解释大学作业的问题时遇到了麻烦。 It seems my solution is not an acceptable answer. 看来我的解决方案不是一个可接受的答案。 I'm not looking for the solution just mainly an explanation on what I'm doing wrong. 我不是在寻找解决方案,主要是解释我做错了什么。

The question is: 问题是:

Implement a simple chat client which transmits user messages to a multicast address and receives messages sent from other clients on other machines sent to the same multicast address. 实现一个简单的聊天客户端,它将用户消息发送到多播地址,并接收从发送到同一多播地址的其他计算机上的其他客户端发送的消息。

The way I am interpreting this is that I have is a server class with a multicast address, then n-amount of client classes that connect or join the server group. 我解释这个的方式是我有一个带有多播地址的服务器类,然后是连接或加入服务器组的n个客户端类。 Then when the client has connected to the server class. 然后当客户端连接到服务器类时。 The server sends the same typed message out to the multiple clients which is displayed on screen of the client. 服务器将相同类型的消息发送到客户端屏幕上显示的多个客户端。 Am I way off with whats asked??. 我什么时候离开,问什么? My code for the multicast server is, 我的组播服务器代码是,

package multicastchatter;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class multicastServer {

    final static String INet_addr = "224.0.0.3";
    final static int PORT = 8888;


    public static void main(String[] args) throws InterruptedException, UnknownHostException {
        InetAddress addr = InetAddress.getByName(INet_addr);

        try(DatagramSocket serverSocket = new DatagramSocket())//open the datagram
        {
                for(int i = 0; i<5; i++)
                {
                    String message = "Sent message number "+i;
                    //create a packet and send
                    DatagramPacket messPack = new DatagramPacket(message.getBytes(),message.getBytes().length, addr, PORT);
                    serverSocket.send(messPack);

                    System.out.println("The server says"+ message);
                    Thread.sleep(500);
                }
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }

    }

}

and my multicast client is 我的多播客户端是

package multicastchatter;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;

public class multicastClient {

    final static String INet_addr = "224.0.0.3";
    final static int PORT = 8888;
    //final static int PORT = 8080;

    public static void main(String[] args) throws UnknownHostException {
        InetAddress address = InetAddress.getByName(INet_addr);//get address
        byte[] buf = new byte[256];//create a buffer of bytes

        //create the multicast socket
        try(MulticastSocket clientSocket = new MulticastSocket(PORT))
        {
            clientSocket.joinGroup(address);//join the group
            while(true)
            {//recieve the info
                DatagramPacket messPack = new DatagramPacket(buf, buf.length);
                clientSocket.receive(messPack);

                String message = new String(buf, 0, buf.length);
                System.out.println("Socket recieved message saying" + message+ " by "+ messPack.getAddress());
            }

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

    }

}

Any advice is appreciated. 任何建议表示赞赏。 All I can think off is that I need to send messages back to the server from the client? 我能想到的是,我需要从客户端将消息发送回服务器吗?

Being that your question is asking if your conceptualization of the client-server architecture is correct, then yes. 因为你的问题是询问你的客户端 - 服务器架构的概念是否正确,那么是的。 What your teacher wants is a server that accepts client connections, and broadcasts client messages it receives to all clients. 您的老师想要的是一台接受客户端连接的服务器,并将收到的客户端消息广播给所有客户端。

A multi-threaded server approach is typically chosen in this situation, as many concurrent connections are being utilized, and each of them waits for a message. 在这种情况下通常选择多线程服务器方法,因为正在使用许多并发连接,并且它们中的每一个都等待消息。 Upon receiving the message, the server will take that message, append an identifier to the front so that we know what client said the message, and distribute that message once only to each client. 收到消息后,服务器将接收该消息,在前面附加一个标识符,以便我们知道客户端所说的消息,并将该消息仅分发给每个客户端一次。

As for the client, it just takes input, sending when necessary, and always listens for packets from the server, displaying whatever it receives. 对于客户端,它只需要输入,必要时发送,并始终侦听来自服务器的数据包,显示它收到的任何内容。 A valuable word of advice: 一个有价值的建议:

Do not allow the client side to display what is sent until it is received. 在收到之前,不允许客户端显示发送的内容。 In other words, sending input from the client program should only display what was sent when it is received back from the server. 换句话说,从客户端程序发送输入应仅显示从服务器接收回来时发送的内容。 It is a good practice to employ in server-client architecture in most instances. 在大多数情况下,在服务器 - 客户端体系结构中使用是一种很好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM