简体   繁体   中英

Client-Server chat application in Java

I'm currently trying to implement a client-server chat application in Java. The brief for this project is that a number of clients should be able to connect to a central server, through which each client communicates. It should work as follows:

  • Server is started up
  • A number of clients are started up and "connected" to the server
  • Client A sends a message to the server, which relays it to clients B, C etc.
  • Server is stopped, all connections are closed
  • I've been messing around with different ways of implementing this, and would like a bit of advice on whether TCP or UDP is a better protocol to implement the communication between client and server. I was considering using UDP Multicast for the server-to-client communications, but then that's caused trouble with the UDP sockets used for the client-to-server communication. Would using TCP to communicate with the server and then UDP Multicast make sense? Or should I scrap the Multicast and just use UDP datagrams all round, with the server keeping a collection of all the clients it's received messages from to model "connections".

    I'd recommend you to use TCP. It's fairly easy and you wont have to handle packet order, loss of packets etc since TCP already does it for you. You could have a look at the java.nio package which have some functionality which may come in handy.

    The overhead that stems from using TCP can almost be neglected these days since more and more users get faster connection speed and those few bytes extra per package will not do much in the end. What I understand from your question it will only handle a limited group of clients, and thus it will be noticed even less.

    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