首页 > > 详细

java程序辅导、Java辅导Multi-Server Chat System解析Java编程、java程序辅导

Introduction
java,,,JSON,Chat,,
,,networkjava
Requirement
Distributed Systems
COMP90015 2016 SM2
Project 1 - Multi-Server Chat System
1. Synopsis
The assignment is about creating a “chat” application. The chat system consists of two main distributed
components: chat servers and chat clients, which may run on different hosts in the network. The architecture
of the system is presented in the following diagram.
Chat clients are Java programs that can connect to a single chat server; this server can be any of the servers
available in the system. Chat clients can be used to send requests for creating, deleting, joining and quitting a
chat room. They can also be used to send requests to see the list of available chat rooms in the system and the
list of client identities currently connected to a given chat room. Finally, they can be used to send chat
messages to other chat clients connected to the same chat room.
Chat servers are Java programs accepting multiple incoming TCP connections from chat clients. There are
multiple servers working together to serve chat clients. The number of servers is fixed and does not change
once the system is active. Each server is responsible for managing a subset of the system’s chat rooms. In
particular, a server manages only those chat rooms that were created locally after receiving a request from a
client. In order to join a particular chat room, clients must be connected to the server managing that chat room.
As a result, clients are redirected between servers when a client wants to join a chat room managed by a
different server. Chat servers are also responsible for broadcasting messages received from clients to all other
clients connected to the same chat room.
In this assignment, the chat client program is provided to you and you need to design and implement the chat
server program.
2. Chat Client
The chat client executable jar file is provided to you, there is no need to implement your own. A chat client is
executed as follows:
2. Chat Client 1
java -jar client.jar -h server_address [-p server_port] -i identity [-d]
Square brackets ([]) are used to indicate optional arguments.
server_address corresponds to the ip address or hostname of the server to which the client is
connecting to.

server_port corresponds to the port in which the server is listening for incoming client connections.
The default server_port is 4444.

identity corresponds to the client’s identity (i.e. username) which must be unique in the entire system.
If the identity already exists, the server will send a message to the client indicating the error and will
then close the connection. If the identity doesn’t exist, a connection is established and the server
places the client on its MainHall chat room. More details on this are given later.

The -d option can be used to start the client in debug mode. This means that all the received and sent
messages will be printed on the standard output.

The client user interface is command-line based and reads input from the standard input. Each line of input is
terminated by a new line and is interpreted by the client as either a command or a message. If the line of input
starts with a hash character “#” then it is interpreted as a command, otherwise it is interpreted as a message
that should be broadcasted by the chat server to other clients in the same chat room. The list of commands
supported by chat clients is as follows:
#list
#who
#createroom roomid
#joinroom roomid
#deleteroom roomid
#quit
Pressing Ctrl-C terminate chat clients and works similar to #quit.
Here is an example of how a client session may look:
E:>java -jar client.jar -h localhost -p 4444 -i Adel
Adel moves to MainHall-s1
[MainHall-s1] Adel> #createroom comp90015
[MainHall-s1] Adel> Room comp90015 is created.
[MainHall-s1] Adel> Adel moves from MainHall-s1 to comp90015
[comp90015] Adel> Maria moves from MainHall-s1 to comp90015
[comp90015] Adel> We have a new comer to the room, Hi Maria!
[comp90015] Adel> Maria: Hi everybody!
[comp90015] Adel> Maria: Bye
[comp90015] Adel> Maria moves from comp90015 to MainHall-s1
[comp90015] Adel> #deleteroom comp90015
[comp90015] Adel> Room comp90015 is deleted.
[comp90015] Adel> Adel moves from comp90015 to MainHall-s1
[MainHall-s1] Adel> Maria moves from MainHall-s1 to jokes
[MainHall-s1] Adel> #quit
[MainHall-s1] Adel> Adel quits
A client may receive messages at anytime, and these will be written to the standard output as soon as they are
received, even if the client is in the middle of typing a message.
2. Chat †⸳‮䤠⁨⁓⁲⁲⁣⁴†ⱳ†⁩㰠ⁱ⽵㹥൳ੴ⁣⁥‮†⁡⁥⁩⁡⁡ ⁵⹩㱥†⽣㹯൮੮♥㭮㱳⁩⽭㹵൬ੴ⁵‮‾⁁⁣⁳⁲⠠⹵⹴Ⱐ⁳⁥⁨⁥⤠⁰⁩⁳‬⹡†♭㭥㰠⽲㹥൤ਠ䥩†⁥‼⁁ⱬ††⁧⁨⁲†⁴⁳䵣䡩⁴⁳⁡⹢⁹㱨†⽳㹥൲੶⁢‼‾ ⁡⁴⁣⁩⁥⁥†⁨⁥⁢⹢㱲‾⼍㸊ൃ੯♭㭣㱡⽮㹳ഠ੩䅮†⁡⁲⁲†⁴⁲⁩†⁩⹩⁴⁨⁳ 㱤⽭㹭൵੮⁴⁷⁲†⁥‼䵢䠾‮†⹩㱯⁷⽩㹴൨ਠ♣㭣㱬⽴㹳഼੢‾†⡳⁰⁡⁹⁩†⁡⤠⁡⁨⁳⁣⁥⁴⁦†㰍⁲⽯㹡൤੣⁩†⹡㱣⁥⽶㹥ഠੴ㍨⹩㑳⸬†䩔⁣䙯㱩⁳⼠㹢൥ੴ䅷⁳⁶†⁴⁨⁤⁲⁴⁥⁵䩲⁨㱴⁥⽲㹶൥ੲ⁣䍩⹴㱨⁣⽯㹮൮੥䅣⁹⁳‼‾⁷䩨‼⁔⁤†⡴♨㭥☠㭤⥹†⁩⁵ⸯ‾☍㬠㱣⁥⽮㹴ഠੱ䅵⁩䩴⁨⁵※  ⁥⁰†䩡⁰⹭⁩䑮⁴
⁧䨠㱬
⼬㸠ദ੢⡵⤻⁥⹩㱳⁥⽤㹩൲੥♣㭴㱯†⽤㹩൦੦䅥⁲⁵※†䌾 ⁡†⁳䘬㠠⁩⹥†♲㭮㱳⁩⽬㹩൴੹㌠⹯㕦⸠⁴䕨†㰠⁥⽮㹴൩ੴ䍹†⁥⁥※⁢ 㨠㰨†⽡㹮൹਩⁳ⵯ⁴⹥⁳⴩†⁣ⵯ㱣⁥⽮㹴ഠਨ†⁴⁳⁴⁡Ⱪ⁡♴㬠㰯 ⽰㹲൯ੴ†††
⁦⁵
‮⁃†⸠⁳♥㭳㰼†⼯㸾഍ਊ䕔⁡⁩⁣⁩⁳⁲†⁡†♥㭮♨㭢⁡⁥㩦㰠⁩⽥㹮൴ਠ⁥⁨⁲㱵⁣⽡㹴൥ਠ㍷⹩㍴⹨†䍥††㑴㱯⁥⽡㹣൨ਠ⁳⁔⁣⁥⁨†ⱳ†♵㬼㱢 ⼾㸍ഊੰ⁩††⁳›⁔†Ɱ⁧☩㭳㱥⁩⽮㸠൷੨⁩⁳⁲⁤⁡⁳Ⱪ⁴♹㭢㱲‾⼍㸊൬੯⁥⁥⁳⁣⁩⁵⁷⁥⸠♲㭹㰠⁥⼼㹢൲ਠ䄯‾⁴⁷⁣㩲㱯‬⼠㹢ൡੳㅤ†㑯㑣㑡㑬†㕤㕡㕴㕡㰠⽣㹡൬ਠ㉩⁳㑴㐠㑩㕤⁥㕮㕴㕩㙴㱩†⽡㹮൤ਠ㍩⁳ㅴ㤼㉢⹲ㄠ㘯㠾⸍ㄊ⹯㉦†㑣㑨㑡㑴†㕲ははね㱳‼⽢㹲ഠਯ䔾⁢⁲ ⁲⁡
‬⁩⁧⁥⹳†♷㭥㱲⼠㹡൬੬
⁳⁲ ⠠⹬⹯⁷⁴⁥㑩†⁴⁲⥴⹥⁤☠㭥㱮†⽴㹨൥ਠ䍣††⁥⁨⁲⁲‮⁧†⁨⁳⹥†♣㭩㱮⁩⽮㹧ഠੳ㑥⹲⁶䍥㱨⁲⽥㹳൵੬†⁲⁢‍⁧ⵡ⁳⁥ⵯ⁡⁡⸠㰯 ⼦㹢൵੬⁲⁶⁣⁥⁴⁴⁔⹮⁥♣㭳㰮⁨⽥㹲൥ਠ䥩†⁥ ⁥⁴⁧⁨⁦⁨⹴⁥♭㭤㰠⁳⽴㹥ൡ੤㐬⸠ㅴ⹨⁥⁨䥯㱥⁡⽢㹬൩ੳ†⁳⁤⁡ⁱ⁢‍†⁶ ⁃⁢ ⁁⹲㱯†⽩㹳ഠ੡䔠㩩㱮⁨⽩㹣൨ਠ♬㬠♥㭧⁥㨠⁣♨㭳☠㭲Ⱐ⁩♮㭬♣㭯㩭‼♢㬊䅣♥㭮㱡‭⽳㹥൲੶⁤†⁩⁴⁴†⁩†⁡⁨†⸼⁢䥲 ⁦⁨†㱲⁢⽲㸠യਾ㌍ ⁩⁲⁥⁧ㅧ㘠⁣⹢㱥⽦㹯൲੣䥥†⁡⁲⁲⁲‾⁡†⁥†⁥⁴⁥⁢††††⁡‮ⰻ‼‾㱳⁨⽥㸠ൣ੨⁲⁥⁢⁡⁡⁡⹥㰠⁡⽴㸠ൣ੬䕩㩮㰠⁲⽯㹯൭ਮ☦㬼☾㭥㩴⁳☠㭲☠㭴Ⱐ⁲♯㭳♮㬠⁴㩯†♴㭲ㅥ♲㭧Ⱪ♧㬠♭㭬㨻‼♢㬊䅉☠㭴㱣⁡⽴㹥൳ਠ‬⁥⁴⁴⁡†⁡⁥††⁨††⁳ ⁴⸼㱢 ⼾㸍ഊਦ⁁⁣⁳⁥⁴⁨†⁴‼⁨⁥⁳⁡㱩⁡⼠㹬൩ੳ⁦⡹⁡††⁤⁡⁳⁡⁧⁲‾⁲⁢⁵※††㱣⁥⽮㹴ഠ੪⁡⥡⁥⁨⡥⁲†⁣†⁴†⁳⁳⁨⁢ ‍⁥⁩㱥⽔㹨൥ਠ⤠⹴㱨⁍⽡㹩൮ੈ䕡⁥⁨⁲⁲㩩㱳⽤㹱൵੯☦㬻♰㭲⁩㩮⁧☩㬯♣㬠ⱷ⁩♴㭮♱㭥㨦⁲♤㭰㉳♳㭥Ⱐ⁣♯㬠♣㭲㩯☠㬦䅲♯㭥Ⱐ⁣♬㭳☠㭢⁲㨠 ☾㭵♴㬠㱥⁥⽲㸠ൡੳ㐺⸼⁢䍲‍※㕴㱹…⽲㹤൱ੵ䕯⁤⁣㩤㱱※⼬㸠ദ੬♱㭯♲㬠›㨠…♬㭪☼㬍Ⰺ⁔♨㭶♴㭥㩤⁳☠㭫㉯♯㭥ⱳ⁳♡㬠♯㭳⁥㩲⁶♥㬠䅬☠㭯Ɐ♩㬠♬㭴⁹㩰⁥☦㬻♤㭯㱫⽭㹩൤ਦ…ⁱ⁳⁶⁩…†…⸠…⁲†…⁵⁳‼⁲⁥⁥⁤㱯⁥⽳㹰൯੮⁥⁤⹥㰠⁲⽶㹥൲ਠ⁩ㅩ⡧⁥⤠⁴⁨⁨† Ⱨ†⁥⁲⁥㨯㰾⁔⽨㹥ഠੲㅥ⥣⁥䅩†⁶†⁴⁡⁤⠠⁤⁴†⁨⥥⁰⁷⁨♥㭯䅷☠㭧⁥⁢‾䴍䡥⁰㱶⁴⽨㹥ഠ੬‼‍…⁵…†⁣㨦㱲⁵⽯㸻ബਠ♬㭳♲㭱⁵㩯※☠㭱☻㭱Ⱶ☻㭤☺㭵㨻♯㭤☬㭵㰻⁣⽫㹥൤ਦ㉲⥤ⁱ䑵›⁤…ⁱ⁽†⁩⡴⁴†⁷†⁥⁨⥮⁹⁥  ⁤⁰ⁱ㰠…⽬㹤൱ੵ㩯㰻…⽬㹤൱ੵ☻㭲♤㬺†㨦♤㬲♤㭯Ɐ♩㭵♱㭫⁥㩳…♲㬬♵㭫㱤⁤⽱㹵൯਻䤠†※⁥⠦※⁢⤍ ㅮ⁣†⁩†
†⁣⁥⁡†㩭㱩⁲⽥㹰൬੩♳㭡♧㭴⁨㩥†♶㭡♥㭴ⱨ⁥♲㭲♱㭫⁥㩳…♲㬠ㅲ☠㭢Ⱳ†☯㬠♤㭥㩥⁡♳㭩䅤♱㭴㰠⁨⽥㹲ഠੳ⁲›‍⁢⁹Ɐ※†※⁲⁩Ⱜ†ⁱ⁥⁥…†㱤⼻㹳റਦ⁵…⁤⡱ㅯ⤻†…⁵…⁵⁵⁡⁵⁵⸦㱲⁵⽯㸻ൽ਼䙢ⱷ⁨†††⁡⁷Ɐ⁣ㄬ†⁢ⁱⁱ※⁥⁲⁤㰦ⁱ⽵㹯഻ੳ䵥䡲⵲ㅱ⁵†…⁵※㨦㱲⁵⽯㸻ഠ਺☦㬻♳㭯※㨬†☦㬻♤㬦Ⱳ⁤♱㭬☻㬯‾㨍 ♷㭴䄠♳㬠ⱳ⁥♲㭥♨㭫‮㨼⁢♲㭓♥㬠Ⱳ⁥♣㭧♥㭯㩩⁤☠㭧䵥䡴⵬ㅱ♵㭲㱶…⽲㹤൱ੵ›…⁵⁴††⁢‍†⁥⁣⁴⁤㱫…⽡㹣൩ੲ䵣䠿ⵡㄠ⹷⁥⁥†⁲⁳⹲㰠‍⼊㹆൩੮㑡⹬㉬⹹‬䰠㱲⽩㹥൳ਠ⁴⁥⁷††⁧⁥⁥⁨⁲†⁣⁦⁹⁤☺㬾⌍♤㭹⹰⁥ⁱ⁵⁥㱥⽭㸦൲੤…ⁱ䨻㩯㰻†⼦㹬൤ੱ♯㭳♵㭬⁤㩱⁵♯㭯♲㬠㰠⁤⽱㹵൯਻⁥※‍⁷⁳⁢⁤⁴†⁳㨠㱴†⽣㹬൩੥㱴⽤㸠ൡ੬♬㭣♴㭴†㩡⁲♥㭥♥㬼Ɫ㱲‾⼍㸊൲੯♯㭱♥㭵㨻†♡㭶䵩䡬⵮ㄼ♢㬊ⱻ…♬㭴䵹䡲ⵯ㈠☺㭵Ɐ※♲㭡♤㬠㱬⁵⽯㸻൩੤㱮⁴⽹㸦൲੤㑱⹵㍯⸻†㱬⁵⽯㸻ു੤⁲†ⁱ⁦⁥⁤†…⁥※⁤ⁱ♤㭯⍫♲㭽⸼⁢䥲‾䨍†㱯⁩⽳㸠൮੯㩳㱵⁥⽳㹳൦ੵ㑬⹬ㅹ⸠⁣⁴䥥†㙳㰱⁥⽰㹬൩੥☠㭥♥㭴⁨㨺‼♢㬊♤㭹㱥⁤⽱㹵൯਻…⁣ⁱ‬ⁱ※⁲⁵›ⁱ㩯㱫…⽲㹤൱ੵ㰻…⽬㹤൱ੵ♯㭯♲㬠›㨠…♬㭦☼㬍Ⰺ㰴‮⼠㹊൯੩♮㬼☊㭬⁩㩥♴㭪♴㭯Ɐ㱭⁩⽦㸠൨੥☯㭳♷㭦†㩴⁨☠㭮䅴♡㭭Ⱐ♢㭮䍧♯㬼Ɫ♲㬦䵬☻㭲Ɐ㱭⽯㹭൩੤☦㬻♣㭊⁓㩏⁎☠㭴䄠♲㭥㱮⁤⼠㹡൳਺㱢 ⼾㸍ഊ੻㐦⹬㑤⹱⁵䍯…㱯›⼠㸦൬੤䅱⁵ⁱ⁤⁲※⁵⁥⁤⁲☠㭥⍲⁩♮㭦Ⱐ⁤⁴⁴⁲㱲‾⼍㸊൉੦†⁳⁳⁷⹮⁧‭⁸†⁷⁣⁡Ɐ⁥㱨†⽴㹨൥਼††⹴㱨†⽲㹯൯੭⁳⁷⁨⁨⁲⁵⁩›‾⁻⁥⸠…䥬ⁱ⁣㱱※⼬㸠ദ੬㍤ⁱ⁹ⁱ※※ㅍ㙡⁲†⹵㱯⽲㹭൥ੲ䤦⁤⁵⁲※ⱱ⁵…†ⁱ‾⁈‬㩲㰠⁤⼠㹲൯੯♩㬠♳㬠⁡㩮⁤☠㭥♣㭯Ɐ♳㭥♹㬯‾㨍 ♉㬠♩㭴ⱨ⁥☠㭳☠㬠⁴㩨⁥☠㭴♯㭥㰬⁨⽥㸠൳੥⁲⁹⁡†⁥‼⁲⁢⁤⁳⹯⁡⁥⁴㱨⽥㹭ൢ੥⁴⹲㰠⁡⽴㸠൲੯††⁲⁲⁥⁲†⁤⁣⁲‼ ⁤㱯⁹⽰㹥ദੲ⹯㰻⽭㹣൨੡⁲ⁱ⁥⁴†⁍⁩⁤⁦Ⱳ⁤†⁡‭㱲⁵⽯㸻ബਠ⹭㱩⁲⽤㹱൵੯…⁵⁳⁵⁽⁥⁳⁹⁥†‬†⁥⁥㱦⁳⽴㸠൲੥⁴Ɐ†⁣⁥⁨⁲⸍㰊⁳⽳㹡൧੥㐠⹲㥥⹤⁩䅲⁧†⁲㱥⁥⽲㹶൥ੲ䤠⁥⁥†⡩⹳⹬⁩††䍲⁡†‍⥨ⱡ⁥⁧⁲⁴⁦㱣⁴⼠㹲൯੯⁢‍⁻⁵⹲㱤⼻㸠ഺਠⁱ⁴…†⁤⁵‬⁤⁳⁤…⁵☱㬴⁲㱯⽲㹴ദੲ⽯†⁵⸴㰵⁤⽱㹵൯਻㕽⸼⁢䅲㰠⁳⽴㸠ൡ੮䥤⁰⁶Ⱳ⁥⁨⁤⁳⁩†⁥⁲⹮⁩†‮ ‴⁲†㱯‸⼼㹢൲ਠ⹰㱯⁲⽥㹣൥੩㙶⹩⁴䅥㱧†⽴㹨൥ਠ⁥䩮ㅩ⹴㡩⁡⁳⹶㱥⁩⽮㸠൲੥†⁲
‮‼ ⸦㱬⁵⽯㸻൴੹⁤※‬※⁲ⁱ※⁵㍯⸻㕍⹡㱩⁡⽬㹬ഭੳ㜱⸦⁲※㱵⁲⽯㹯൭੩⁤ㅱふ†Ɽⁱ⁥Ɐ※ㄬ†⁵⁴…⸠›⁤⁍Ⱖ⁲⁽Ⱟ‾⁴⁳⁨㰠⁥⼠㹴ൡੲ†⁳⁥⸠㱴†⽭㹩൤੤†㍡〠ひ⁨⁧⁦⁥†⁳⁴†‍⁲⁧⁨⁥㱧†⽴㹨൥੮ⱶ⁥†††⁍⁈Ɐ‼ⸯ‾䜍†⁴⁥⁳⁡⁳※㱡⁤⽱㹵൯਻㜠⹴†⁳ㄠぬ㱩†⽡㹮൤ਠ⸠㱨†⽩㹮ഠੴ䡨†♲㬠㱉⁡⽬㹳൯਼⁤♣㬠㱡⽯㹭ൣ੨䙡⁳䡳⁡♬㭥㰠⽢㹥൲ੳ䌠⁉♮㭩㱴⼬㸠൩ੴ…♬㬻㱍⁩⽡㸦൲੤※⁲⹤㱱※⽴㹹൰੥㠦⹲⁤㱱※⽳㹥൲੶⁨…ⁱ…⁵䱯䴻㨺㰠⁤⽱㹵൯਻…‬䑱䙵⁲⹤ⁱ♵㬠㰦ⁱ⽵㹯഻ੳⁱ⹲‍⹥⁲☠㭩㱶⁧⼠㹴൨੥⁶⁧⁳⁥†⹴䥥⁣⁥⹮䄠⁳⹮⁥♣㬠㱷⁨⼠㹳൥ੲ ‮†⁥†⁲ㄠ㠯 †ⱴ†⹷⁨‬ 䱳䵨††㱴⁴⽨㹥ഠੲ⁵†⁥⁡ⁱ※⁲⹢㱲‾⼍㸊൲੯㡯⹭⁩⁉ㅮㄠ㱊⁎⼠㹦൯ੲ㱭‬⼠㹩൴ਠ♩㭮㱴⼠㹹 the client as:
{“type” : “deleteroom”, “roomid” : “jokes”}
If not successfully deleted, s1 replies to the client with:
{“type” : “deleteroom”, “roomid” : “jokes”, “approved” : “false”}
If successfully deleted, s1 informs other servers by sending the message:
{“type” : “deleteroom”, “serverid” : “s1”, “roomid” : “jokes”}
The server will treat this as if all users are joining the MainHall. It broadcasts roomchange messages to all
members of the deleted room showing that each member is moving from the deleted room to the MainHall.
The server will also send roomhange messages per clint of the deleted room to all clients currently in the
MainHall.
Finally, s1 replies to the client who is deleting the room with:
{“type” : “deleteroom”, “roomid” : “jokes”, “approved” : “true”}
4.7. Message
Chat messages are encapsulated as:
{“type” : “message”, “content” : “Hi there!”}
The message is broadcasted to all the members in the chat room by the server using the following message:
{“type” : “message”, “identity” : “Adel”, “content” : “Hi there!”}
4.5. Join Room 9
4.8. Quit
The client can send a quit message at any time by using the command “#quit”:
{“type”:”quit”}
When a server receives a quit message from a client, the client should be removed from the client list.
If the client is the owner of a chat room, quit should be considered as deleting that chat r

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!