"안녕, 아미고!"
"안녕하세요, 다람쥐 선장입니다!"
"자연재해 발생 시 대체 소통 채널이 필요합니다."
"나에게 말하지 않은 것이 있습니까? 무슨 자연재해입니까?"
"민간인과 달리 우리는 항상 백업 통신 채널이 있어야 합니다."
"스카이프?"
"매우 신뢰할 수 없고 우리는 그들의 서버를 제어할 수 없습니다. 우리는 자체 블랙잭이 있는 채팅 클라이언트를 작성할 것입니다... 제 말은 자체 서버와 클라이언트가 있는 것입니다!"
"그리고 그래픽 인터페이스가 있습니까?"
"Agent IntelliJ IDEA가 지시한 대로 정확히 수행하면 그래픽 인터페이스가 표시됩니다."
"와, 드디어! 다른 질문 해도 될까요?"
"질문이 너무 많습니다. 보너스 연습으로 자신의 질문에 답하는 봇을 작성할 수 있습니다. "비켜!"
"알겠습니다!"

8
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 1)
Today we're going to write a chat system: A set of programs that you can use
to exchange text messages. The system will consist of one server and
multiple clients, one for each chat participant.
Let's start with the server. We'll need the following classes:
1) Server - The server's main class.
14
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 2)
First of all, for convenience in working with the console, we'll implement the ConsoleHelper class. In
the future, any work with the console should happen through this class.
Add the following to it:
1) A static BufferedReader field initialized using System.in.
8
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 3)
Before moving on, we need to work out the protocol for communication between the client and server.
Let's outline the main aspects of the protocol: When a new client wants to connect to the server, the server should request the client name.
8
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 4)
A Message is data that one party sends and the other receives.
Each message must have a MessageType and some additional data,
for example, a text message must contain text.
Since the messages will be created in one program and read in another.
28
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 5)
The client and server will communicate through a socket connection.
One side will write data to the socket, while the other will read. They interact by exchanging Messages.
The Connection class will wrap the java.net.Socket class.
28
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 6)
Let's move on to the most important part: writing the Server class.
The server must support multiple simultaneous connections with different clients.
This can be done using the following algorithm:
- The server creates a server socket connection.
14
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 7)
Because the server can work with multiple clients simultaneously, we're going to need
a method to send a message to everyone all at once.
Add the following to the Server class:
1) A static Map<String, Connection> connectionMap field, where the key is the client name.
28
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 8)
The Handler class must implement the client communication protocol.
Let's identify independent stages of the protocol and implement them in separate methods:
The first stage is the handshake (in which the server meets the client).
14
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 9)
The second stage, which is no less important, is sending information to the client (new participant) about the
other clients (chat participants).
14
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 10)
The third stage is the server's main message-processing loop.
Add a private void serverMainLoop(Connection connection, String userName) throws IOException, ClassNotFoundException
method, where the meaning of the parameters is the same as in the notifyUsers() method.
28
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 11)
It's time to write the main method of the Handler class, which will call all
the helper methods we wrote earlier. Implement the void run() method in the Handler class.
14
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 12)
Let's start writing the client. When the client starts running, it should request the server address and server port,
connect to the specified address, receive a name request from the server, ask the user for his or her name,
send the username to the server.
14
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 13)
Let's continue to implement helper methods in the Client class.
Add the followings methods, which will be available to subclasses,
but not to other classes outside the package:
1) String getServerAddress() - It should ask the user to enter the server address,
and return the entered value.
28
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 14)
Let's start writing the main functionality of the Client class.
1) Add a public void run() method. It should create a helper SocketThread, wait until it establishes a connection with the server, and then in a loop, read messages from the console and send them to the server.
14
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 15)
Let's write the implementation of the SocketThread class. We'll start with simple helper methods.
Add methods that will be available to subclasses but not accessible to other
classes outside the package:
1) void processIncomingMessage(String message) - It should display the message on the console.
28
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 16)
Now everything is ready for us to add the necessary methods to the SocketThread class.
1) Add a protected clientHandshake() throws IOException, ClassNotFoundException method.
This method will represent the client to the server. It must:
a) In a loop, use the connection field to receive messages.
8
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 17)
The last, but most important, method of the SocketThread class is the void run() method. Add it.
Given the methods we've already created, implementing this method looks very simple.
Let's do it:
1) Request the server address and server port using the getServerAddress() and getServerPort() methods.
14
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 18)
Sometimes you can't find someone worth talking to. Don't start talking to yourself :).
Let's write a bot that will be a client that automatically respond to certain commands.
The easiest version would be a bot that sends the current time or date when someone asks for it.
28
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 19)
Now we'll implement the BotSocketThread class, or rather we'll override some
methods. All of its main functionality is inherited from SocketThread.
1) Override the clientMainLoop() method.
14
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 20)
We've already implemented a console-based client and a chat bot. Why not a client with a GUI?
It will also work with our server, but it will have a graphic window, buttons, etc.
Well, let's get started. The Model-View-Controller (MVC) pattern is well suited for a GUI-based client.
28
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 21)
I have great news for you. The view component is already done. I've added a ClientGuiView class.
It uses the javax.swing library. You should really study every line of this class.
If everything is clear, that's wonderful.
8
과제
자바 멀티스레딩, 레벨 6, 레슨 15
Chat (part 22)
So let's recap:
• You wrote a text messaging server.
• You wrote a console-based client that can connect to the server and exchange messages with other chat participants.
• You wrote a bot client that can receive requests and send information about the current date and time.