<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Chat | 정진영의 포트폴리오</title><link>https://wjdwlsdud.github.io/en/tags/chat/</link><atom:link href="https://wjdwlsdud.github.io/en/tags/chat/index.xml" rel="self" type="application/rss+xml"/><description>Chat</description><generator>Hugo Blox Builder (https://hugoblox.com)</generator><language>en-us</language><lastBuildDate>Wed, 26 Jun 2024 00:00:00 +0000</lastBuildDate><image><url>https://wjdwlsdud.github.io/media/sharing.jpg</url><title>Chat</title><link>https://wjdwlsdud.github.io/en/tags/chat/</link></image><item><title>Linux TCP chat program</title><link>https://wjdwlsdud.github.io/en/projects/linux/</link><pubDate>Wed, 26 Jun 2024 00:00:00 +0000</pubDate><guid>https://wjdwlsdud.github.io/en/projects/linux/</guid><description>&lt;p&gt;Based on what I learned in my Linux System Programming class, I implemented a simple multi-client TCP chat program as a project.&lt;/p&gt;
&lt;p&gt;It was a meaningful experience to directly apply the concepts of C socket communication and multithreading (pthreads). I&amp;rsquo;d like to summarize the project&amp;rsquo;s structure, core operating principles, and the key takeaways.&lt;/p&gt;
&lt;h2 id="1-project-overview"&gt;1. Project Overview&lt;/h2&gt;
&lt;p&gt;This program is a simple console-based (terminal) chatroom that allows a single server and multiple clients to chat in real-time over the TCP/IP protocol.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Key Technologies&lt;/strong&gt;: C Language, TCP/IP Socket Programming, pthread (Multithreading)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Core Features&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Server: Manages multiple client connections simultaneously.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Broadcasting: Transmits a message from one client to all other connected clients (except the sender).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Multi-Chat: Allows multiple users to send and receive messages at the same time.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="2-core-architecture-how-concurrency-is-achieved"&gt;2. Core Architecture: How Concurrency is Achieved&lt;/h2&gt;
&lt;p&gt;The core challenge of a chat program is handling multiple requests simultaneously. Functions like accept(), recv(), and fgets() are blocking by default. This means the entire program pauses and waits for an operation to complete before moving on.&lt;/p&gt;
&lt;p&gt;I used multithreading to solve this problem.&lt;/p&gt;
&lt;p&gt;⚙️ &lt;strong&gt;Server: Thread-per-Client Model&lt;/strong&gt;
The server needs to handle connection requests and receive messages from multiple clients at the same time.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;The main thread waits for new client connections using the accept() function.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;When a new client connects, it calls pthread_create() to create a new, dedicated thread (client_chat) just for that client.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;This dedicated thread waits for messages from its client using the recv() function.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The main thread immediately loops back to accept() to wait for the next client.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In effect, the &amp;ldquo;connection handling&amp;rdquo; thread (main) and the &amp;ldquo;per-client message receiving&amp;rdquo; threads all run independently and concurrently.&lt;/p&gt;
&lt;p&gt;When a thread receives a message, it calls the send_msg function, which iterates through the clientList to broadcast the message to all other clients.&lt;/p&gt;
&lt;p&gt;💻 &lt;strong&gt;Client: Separation of Sending and Receiving Threads The client has its own major&lt;/strong&gt;
challenge: it must be able to receive messages from others in real-time, even while the user is typing their own message.&lt;/p&gt;
&lt;p&gt;If it were single-threaded, the program would block on fgets() (waiting for user input), making it impossible to call recv() and receive incoming messages.&lt;/p&gt;
&lt;p&gt;To solve this problem, the client program is also split into two threads:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Main Thread (main function): This thread is dedicated only to the &amp;ldquo;sending&amp;rdquo; role. It waits for the user&amp;rsquo;s keyboard input (fgets) and sends (send) the message to the server.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Receiving Thread (receive function): Created with pthread_create(), this thread is dedicated only to the &amp;ldquo;receiving&amp;rdquo; role. It blocks on recv(), waiting for messages to arrive from the server.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;By splitting the roles this way, the receive thread can display incoming messages from the server in real-time, even while the user is busy typing a new message in the main thread.&lt;/p&gt;
&lt;p&gt;You can check out the code via the link(s) attached above!&lt;/p&gt;</description></item></channel></rss>