javasocketsclient

Multi-client sockets in Java?


Soon I will be finished with a single-client chat system as a recreational personal project, and I was wondering... Is it simple to convert something like this to have multiple clients, or would it require a large rewrite?

And in either case, how would you do it? Please be descriptive! :D Thanks!

Also, how do you transmit files across a socket?


Solution

  • It can be simple to convert a single client to multiple clients. It depends on how you have written your application. Since you are rewriting an application which has been written many times before, I assume you are not afraid to rewrite code and you are doing it to as a learning exercise.

    The simplest change would be that where you accept() one client now, you should use a loop to accept many clients. (and pass processing that client to another thread).

    To transfer a file over a socket you need to be able to connect to the server as you do now, read the file and send the contents to the server. Instead of passing text chat messages to the receiver, you pass a file message with the contents of the file.