开发者

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? Pleas开发者_高级运维e be descriptive! :D Thanks!

Also, how do you transmit files across a socket?


If you are considering writing an IM system then I'd suggest you to work with a more higher level protocol and API like Jabber/XMPP. Here is the example of transferring a file over socket.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜