0% found this document useful (0 votes)
16 views

TCP Multi Connection

This document discusses implementing multiclient TCP connections using threading. It involves: 1. Creating a server with a GUI and a HandleSocket class that extends Thread to handle individual clients. 2. The HandleSocket class receives and sends messages to its assigned client. 3. The server creates a HandleSocket for each new client and adds it to an arraylist to broadcast messages. 4. Messages are received in the HandleSocket run method and displayed in the server GUI, then broadcast to all clients. 5. Clients connect to the server and send/receive messages similarly to the single client TCP chat example from the previous week.

Uploaded by

tikowanaasu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

TCP Multi Connection

This document discusses implementing multiclient TCP connections using threading. It involves: 1. Creating a server with a GUI and a HandleSocket class that extends Thread to handle individual clients. 2. The HandleSocket class receives and sends messages to its assigned client. 3. The server creates a HandleSocket for each new client and adds it to an arraylist to broadcast messages. 4. Messages are received in the HandleSocket run method and displayed in the server GUI, then broadcast to all clients. 5. Clients connect to the server and send/receive messages similarly to the single client TCP chat example from the previous week.

Uploaded by

tikowanaasu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Distributed Programming

Multiclient connection in
TCP
Program Studi Teknik Informatika
Fakultas Teknik – Universitas Surabaya
Outline
1. Common Technical Procedure to develop “Multiclient
Connection in TCP (one server and many clients)”
2. Case study: Simple Chat TCP
1. Technical Procedures
2. Code Implementation
Multiclient Connection in TCP
Today, we are going to combine 2 concepts between: TCP and
multithreading
● TCP Communication is implemented for transferring data from
another device over network.
● Multithreading concept is implemented to handle many requests
from many clients at the same time.
Multiclient Connection in TCP Concept

Architecture Software

Server
(HTTP/TCP/etc.)
Common Procedure: TCP Multiclient
Step-by-step:
1. Make Client-Server Projects (2 projects)
2. Make a new Class in Server side (as Controller) to send and
receive message from client. This class extends Thread or
Implements Runnable.
Please read carefully the Implementation TCP communication below
TCP Single Client vs TCP Multiclient

Server Server

Controller Controller Controller


Client

Client Client Client

Single Client Multiclient


The Structure Project
- FormServer.java is a Server UI
- HandleSocket.java is a controller
class for handling many clients
Controller
Case Study: Multiclient Simple Chat
It is like a group chat,
every client and server can
broadcast and receive the
message.
Server-side
Technical Procedures
Step-by-step:
1. Load your TCP Chat with GUI from previous week (Week 6)
Result:
2. Create the HandleSocket.java and add these following codes. HandleSocket Class
will handle one client. So, we need to add Socket, DataOutputStream, and
BufferedReader. We also add FormServer class (Java GUI) as data member. We
will discuss it later.
HandleSocket is a
Thread object. This
class will receive
and send message
to client continously
3. Edit the FormServer.java class (Server GUI) with these following codes. We
need to add array list of HandleSocket because the server will handle multi
clients. FormServer.java only need ServerSocket and Socket object because
DataInputStream and BufferedReader is in HandleSocket class.
4. Because FormServer.java is a Thread Object and can handle multiclient every time,
we need to add run() method. When there is a client connect, then Server will create
a HandleSocket Object to handle it. Server also add the HandleSocket object to
arraylist for broadcasting the message (we will discuss it later).
5. Move to HandleSocket class then Add sendChat method. This method is used for
sending the message to client which is handled by HandleSocket Class.
6. Back to FormServer class then Add showChat and broadCast method.
showChat is used for receiving message and show it to textArea. When the
server get the message from one client, it also broadcast to all client.
showChat is called when server receive the message from handleSocket (we
will discuss it later in handleSocket). broadCast method is for sending the
message to all clients.
7. Back to HandleSocket. Because HandleSocket is Thread object then we need
to implement run() method. This method is used for receiving the message from
client continuously. When HandleSocket get the message from client then it will
be shown in FormServer TextArea (call the showChat method in FormServer).
8. Back to FormServer. Update the send button like this following code. When the
server send the message then it will broadcast to all clients.
Client-Side
9. There is no significant changes in client side. add “nama” textField to distinguish
one client from another. Add the following code inside send button action
Exercise after class
• Add the new features below
1. Before Server start, user must declare the name of group.
2. Before the client join to the group chat (connect with Server), Client
have to fill his/her name first.
3. The new client will get a greeting message from server like “Welcome
to [group name] group chat”.
4. The server side will be show message: “**RING** New Client with
(name_client) will joined!. Please send “YES” to confirm and “NO” to
decline!”
5. The client who have joined before will get the message that there is
new client with its name has joined.

You might also like