简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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