Majid Al-RaimiSockets and web app architectureالمقابس (Sockets) ومعمارية تطبيقات الويب

COE 558Lecture 01المحاضرة 01Part 05الجزء 05

Sockets and web app architectureالمقابس (Sockets) ومعمارية تطبيقات الويب

The socket API between your program and the transport layer, TCP versus UDP sockets, and the frontend and backend of a web application.واجهة Socket API بين برنامجك وطبقة النقل، ومقابس TCP مقابل مقابس UDP، والواجهة الأمامية (frontend) والخلفية (backend) لتطبيق الويب.

Conceptsالمفاهيم
4
Slidesالشرائح
40-45
Readingمدة القراءة
24 min24 دقيقة
Understoodما فهمته
0/4 conceptsمفاهيم

Why this part mattersلماذا يهمّ هذا الجزء

Every service you will place on the edge-to-cloud continuum, whether a camera pipeline, an IoT gateway or a cloud API, ends up as processes talking through sockets. The socket type decides what happens to a lost or late packet. The split between frontend and backend decides which parts can move to the edge and which must stay near the database.

Exams ask for the socket call sequence and for a justified choice between TCP and UDP. Your research project needs the same reasoning to explain where latency comes from. This part builds both, climbing from the point where a program first touches the network up to a complete web application.

كل خدمة ستضعها على سلسلة الـ edge-to-cloud continuum، سواء كانت خط معالجة لكاميرا أو بوابة IoT أو API سحابية، تنتهي في الواقع إلى عمليات (processes) تتحدث مع بعضها عبر الـ sockets. نوع الـ socket هو الذي يقرر ما يحدث لحزمة مفقودة أو متأخرة. والتقسيم بين الـ frontend والـ backend هو الذي يقرر أي الأجزاء يمكن نقلها إلى الـ edge وأيها يجب أن يبقى قريبًا من قاعدة البيانات.

الاختبارات تطلب تسلسل استدعاءات الـ socket، واختيارًا مبرَّرًا بين TCP وUDP. ومشروعك البحثي يحتاج إلى طريقة التفكير نفسها ليشرح من أين يأتي الـ latency. هذا الجزء يبني الأمرين معًا، صاعدًا من النقطة التي يلمس فيها البرنامج الشبكة لأول مرة حتى تطبيق ويب كامل.

By the end you canفي نهاية هذا الجزء ستستطيع

  1. Explain a socket as a file descriptor, addressed by IP and port, that the socket API createsتشرح الـ socket على أنه file descriptor تنشئه الـ socket API، ويُعنوَن بعنوان IP ورقم port
  2. Order the socket calls a TCP server and client make, and say what accept returnsترتّب استدعاءات الـ socket التي يقوم بها خادم TCP وعميله، وتذكر ماذا تُرجع accept
  3. Choose TCP or UDP for a workload by what a lost or late unit of data costsتختار بين TCP وUDP لحِمل عمل معيّن بحسب كلفة فقدان وحدة بيانات واحدة أو تأخرها
  4. Split a web application into browser client, server and database, and trace a saved formتقسّم تطبيق الويب إلى عميل هو المتصفح وخادم وقاعدة بيانات، وتتتبّع مسار نموذج يتم حفظه
  5. Read a backend route as method plus path plus handler and trace one HTTP requestتقرأ الـ route في الـ backend على أنه method زائد path زائد handler، وتتتبّع طلب HTTP واحدًا

Think about what your browser does when you open the KFUPM portal. It cannot put signals on the Wi-Fi radio or build TCP segments by itself. Instead it makes a few requests to the operating system: open a connection to this address and port, send these bytes, give me whatever comes back. The operating system does everything below that. Understanding exactly what that request looks like is the foundation for everything else in this part.

فكّر فيما يفعله متصفحك عندما تفتح بوابة KFUPM. هو لا يستطيع أن يضع إشارات على راديو الـ Wi-Fi أو أن يبني مقاطع TCP (segments) بنفسه. بدلًا من ذلك يقدّم بضعة طلبات إلى نظام التشغيل: افتح اتصالًا إلى هذا العنوان وهذا الـ port، أرسل هذه البايتات، وأعطني ما يعود منها. ونظام التشغيل يتولى كل ما هو تحت ذلك. فهم الشكل الدقيق لهذا الطلب هو الأساس لكل ما يأتي بعده في هذا الجزء.

An API is a menu, and the socket API is the networking menuالـ API قائمة طعام، والـ socket API هي قائمة الشبكات

The set of operations one piece of software offers to another is an Application Programming Interface, or API. Earlier in this lecture you saw that a relationship between components is a protocol plus an Interface. Here the interface sits between the Application layer (your program) and the Transport layer (TCP and UDP inside the operating system). For networking, that interface is the Socket API.

مجموعة العمليات التي يقدّمها برنامج لبرنامج آخر تُسمّى واجهة برمجة التطبيقات (Application Programming Interface)، أو اختصارًا API. رأيت سابقًا في هذه المحاضرة أن العلاقة بين المكوّنات هي protocol زائد interface. هنا تقع الواجهة بين application layer (برنامجك) وtransport layer (TCP وUDP داخل نظام التشغيل). وفي مجال الشبكات، هذه الواجهة هي الـ socket API.

Kurose and Ross describe the socket as the door between the application process and TCP. As a developer you control everything on the application side of that door, and you have very little control over the transport side. The rest of this concept opens the door and looks at what is actually there.

يصف Kurose وRoss الـ socket بأنه الباب بين عملية التطبيق (application process) وTCP. بصفتك مطوّرًا، أنت تتحكم في كل شيء على جهة التطبيق من هذا الباب، ولديك تحكم ضئيل جدًا في جهة النقل (transport). بقية هذا المفهوم تفتح هذا الباب وتنظر إلى ما يوجد خلفه فعلًا.

What a program actually holds: a file descriptorما الذي يمسكه البرنامج فعلًا: file descriptor

Make it concrete. In C, calling socket() returns a small integer, for example 3. From then on the program writes write(3, ...) exactly as it would for an open file. POSIX states that when the descriptor refers to a socket, write() is equivalent to send() with no flags, and the Linux manual says the only difference between recv() and read() is the flags argument.

لنجعل الأمر ملموسًا. في لغة C، استدعاء socket() يُرجع عددًا صحيحًا صغيرًا، مثلًا 3. ومن تلك اللحظة يكتب البرنامج write(3, ...) تمامًا كما يفعل مع ملف مفتوح. ينص POSIX على أنه عندما يشير الـ descriptor إلى socket، فإن write() يكافئ send() بدون أي flags، ويقول دليل Linux إن الفرق الوحيد بين recv() وread() هو وسيط الـ flags.

So to a program, a Socket is an operating system object that it reads and writes like a file. The kernel takes the bytes you write and turns them into TCP segments or UDP datagrams, which is why the application never needs to know how either protocol works. Be careful with the words: the socket API is the interface, and a socket is the endpoint that the API creates and names.

إذن، بالنسبة إلى البرنامج، الـ socket هو كائن من كائنات نظام التشغيل يقرأ منه ويكتب فيه كأنه ملف. الـ kernel يأخذ البايتات التي تكتبها ويحوّلها إلى مقاطع TCP (segments) أو إلى datagrams من نوع UDP، ولهذا لا يحتاج التطبيق أبدًا إلى معرفة كيف يعمل أي من البروتوكولين. انتبه للمصطلحات: الـ socket API هي الواجهة (interface)، أما الـ socket فهو نقطة الطرف (endpoint) التي تنشئها هذه الـ API وتسمّيها.

Each process writes into its socket; the transport layers carry the bytes both waysكل عملية تكتب في الـ socket الخاص بها، وطبقتا النقل تحملان البايتات في الاتجاهين

Naming the endpoint: the socket addressتسمية نقطة الطرف: عنوان الـ socket

A file on disk has a path; a socket has an address: an IP address plus a Port number, for example 10.0.0.5:443. The IP address finds the host and the port finds the process on that host, the same two-level addressing you met with the TCP/IP stack. RFC 9293 identifies a TCP connection by a pair of sockets, one at each end, so a connection is really four numbers: client IP and port, server IP and port.

الملف على القرص له مسار، والـ socket له عنوان: وهو IP address زائد port number، مثلًا 10.0.0.5:443. عنوان الـ IP يحدد الجهاز (host)، والـ port يحدد العملية على ذلك الجهاز، وهي نفس العنونة على مستويين التي قابلتها مع حزمة TCP/IP. تعرّف RFC 9293 اتصال TCP بزوج من الـ sockets، واحد في كل طرف، لذا فالاتصال في الحقيقة أربعة أرقام: IP العميل وport العميل، وIP الخادم وport الخادم.

One more ingredient completes the name. The protocol is fixed when the socket is created, so TCP port 53 and UDP port 53 are different endpoints even on the same host. DNS, as RFC 1035 specifies, listens on both.

هناك عنصر إضافي يكمل الاسم. البروتوكول يُحدَّد لحظة إنشاء الـ socket، ولذلك فإن port 53 في TCP وport 53 في UDP هما نقطتا طرف مختلفتان حتى على الجهاز نفسه. والـ DNS، كما تنص RFC 1035، يستمع على الاثنين.

The lifecycle of a connectionدورة حياة الاتصال

A Server and a Client play different roles, so they use different calls. The server must be findable before anyone arrives: it creates a socket, binds it to a well-known address, marks it as listening, then accepts connections one by one. The client already knows where to go, so it only creates a socket and connects. Once connected, both sides send and receive, then close.

الـ server والـ client يلعبان دورين مختلفين، ولذلك يستخدمان استدعاءات مختلفة. الخادم يجب أن يكون قابلًا للعثور عليه قبل أن يصل أي أحد: ينشئ socket، ويربطه (bind) بعنوان معروف، ويعلّمه على أنه في وضع الاستماع (listening)، ثم يقبل الاتصالات واحدًا تلو الآخر. أما العميل فيعرف مسبقًا إلى أين يذهب، فهو فقط ينشئ socket ويتصل (connect). وبعد الاتصال يرسل الطرفان ويستقبلان، ثم يُغلقان.

Socket lifecycle, in call orderدورة حياة الـ socket، بترتيب الاستدعاءات

socket()
Both sides. Creates an endpoint and returns a file descriptor such as 3. It has no address yet.الطرفان. ينشئ نقطة طرف (endpoint) ويُرجع file descriptor مثل 3. ولا يكون له عنوان بعد.
bind()
Server. Attaches a local IP address and port, for example 0.0.0.0:443. The Linux manual calls this assigning a name to a socket, so clients know where to knock.الخادم. يربط عنوان IP محليًا ورقم port، مثلًا 0.0.0.0:443. ويسمّي دليل Linux هذا إعطاء اسم للـ socket، حتى يعرف العملاء أين يطرقون.
listen()
Server. Marks the socket as passive, ready to accept connections. The backlog argument caps the queue of pending connections. Connection-oriented sockets only (SOCK_STREAM or SOCK_SEQPACKET).الخادم. يعلّم الـ socket على أنه سلبي (passive)، جاهز لقبول الاتصالات. والوسيط backlog يحدد الحد الأقصى لطابور الاتصالات المنتظرة. يعمل فقط مع الـ sockets الموجّهة بالاتصال (SOCK_STREAM أو SOCK_SEQPACKET).
connect()
Client. For TCP it starts the connection, which runs the three-way handshake. For UDP it only records a default destination.العميل. في TCP يبدأ الاتصال، فتجري المصافحة الثلاثية (three-way handshake). وفي UDP يكتفي بتسجيل وجهة افتراضية.
accept()
Server. Takes the first pending connection and returns a new connected socket with a new descriptor, for example 4. The listening socket is unaffected and keeps waiting.الخادم. يأخذ أول اتصال منتظر ويُرجع socket متصلًا جديدًا له descriptor جديد، مثلًا 4. والـ listening socket لا يتأثر ويستمر في الانتظار.
send() / recv()
Both sides. Move bytes. On a socket, write() is send() with no flags, and read() is recv() with no flags.الطرفان. ينقلان البايتات. على الـ socket، فإن write() هو send() بدون flags، وread() هو recv() بدون flags.
close()
Both sides. Releases the descriptor and ends that side of the connection.الطرفان. يحرّر الـ descriptor وينهي جهة هذا الطرف من الاتصال.
One listening socket; accept opens a new connection socket per clientsocket واحد للاستماع، وaccept تفتح connection socket جديدًا لكل عميل

Worked exampleمثال محلول

One HTTPS request through the socket callsطلب HTTPS واحد عبر استدعاءات الـ socket

  1. Server preparesالخادم يستعد

    The server calls socket() and gets descriptor 3, calls bind() to 0.0.0.0:443, then listen().
    يستدعي الخادم socket() فيحصل على الـ descriptor رقم 3، ثم يستدعي bind() على 0.0.0.0:443، ثم listen().
  2. Client connectsالعميل يتصل

    The browser calls socket() and connect() to the server address. The TCP three-way handshake runs.
    يستدعي المتصفح socket() ثم connect() إلى عنوان الخادم. وتجري مصافحة TCP الثلاثية (three-way handshake).
  3. Server acceptsالخادم يقبل

    accept() returns descriptor 4, a socket dedicated to this client. Descriptor 3 keeps listening.
    تُرجع accept() الـ descriptor رقم 4، وهو socket مخصص لهذا العميل. ويبقى الـ descriptor رقم 3 في وضع الاستماع.
  4. TLS handshakeمصافحة TLS

    Both sides exchange TLS handshake messages with send() and recv() on the connected sockets. From here on the bytes on the wire are encrypted.
    يتبادل الطرفان رسائل مصافحة TLS باستخدام send() وrecv() على الـ sockets المتصلة. ومن هذه النقطة فصاعدًا تكون البايتات على السلك مشفّرة.
  5. Requestالطلب

    The client calls send() with the encrypted request bytes. The server calls recv() on descriptor 4.
    يستدعي العميل send() ببايتات الطلب المشفّرة. ويستدعي الخادم recv() على الـ descriptor رقم 4.
  6. Responseالاستجابة

    The server calls send() on descriptor 4; the client calls recv().
    يستدعي الخادم send() على الـ descriptor رقم 4، ويستدعي العميل recv().
  7. Finishالإنهاء

    Both sides call close() on their connected sockets.
    يستدعي الطرفان close() على الـ sockets المتصلة الخاصة بهما.
  8. Resultالنتيجة

    The server held two sockets for this exchange: descriptor 3 listening and descriptor 4 connected.
    أمسك الخادم اثنين من الـ sockets في هذا التبادل: الـ descriptor رقم 3 للاستماع، والـ descriptor رقم 4 المتصل.

Recall

What does a program actually hold when it has a socket, and which ordinary calls can it use on it?ما الذي يمسكه البرنامج فعلًا عندما يكون لديه socket، وما الاستدعاءات العادية التي يمكنه استخدامها عليه؟

A file descriptor, a small non-negative integer from the operating system. It can read and write it like a file: write() on a socket equals send() with no flags, and recv() with no flags behaves like read().
يمسك file descriptor، وهو عدد صحيح صغير غير سالب يعطيه إياه نظام التشغيل. ويمكنه أن يقرأ منه ويكتب فيه كأنه ملف: write() على socket يساوي send() بدون flags، وrecv() بدون flags يتصرف مثل read().

Recall

List the server-side calls in order. What does accept return, and what happens to the listening socket?اذكر استدعاءات جهة الخادم بالترتيب. ماذا تُرجع accept، وماذا يحدث للـ listening socket؟

socket(), bind(), listen(), accept(), then recv() and send(), then close(). accept() returns a new connected socket with its own descriptor for that one client. The listening socket is unaffected and keeps waiting.
socket()، ثم bind()، ثم listen()، ثم accept()، ثم recv() وsend()، ثم close(). تُرجع accept() socket متصلًا جديدًا له descriptor خاص به لذلك العميل وحده. والـ listening socket لا يتأثر ويستمر في الانتظار.

Quick checkاختبار سريع

An HTTPS server process has 1000 clients connected on port 443. Counting the listening socket, how many TCP sockets does it hold?عملية خادم HTTPS متصل بها 1000 عميل على port 443. مع احتساب الـ listening socket، كم TCP socket تمسك؟

Once you know a program holds a socket, the next decision is which kind. Take two jobs. First, downloading a 2 GB dataset for your project: one missing byte corrupts the file, so every byte must arrive, in order. Second, a live video call: a voice frame that arrives 1 s late is useless, so waiting for it to be resent makes the call worse, not better. These two jobs need different sockets, and the difference is fixed the moment socket() is called.

بعد أن عرفت أن البرنامج يمسك socket، فالقرار التالي هو: أي نوع منه؟ خذ مهمتين. الأولى: تنزيل مجموعة بيانات حجمها 2 GB لمشروعك. بايت واحد مفقود يفسد الملف، لذا يجب أن يصل كل بايت، وبالترتيب. الثانية: مكالمة فيديو مباشرة. إطار صوتي يصل متأخرًا 1 s لا فائدة منه، لذا فانتظار إعادة إرساله يجعل المكالمة أسوأ لا أفضل. هاتان المهمتان تحتاجان إلى نوعين مختلفين من الـ sockets، وهذا الفرق يُحدَّد في اللحظة التي يُستدعى فيها socket().

Two services behind the same APIخدمتان خلف الـ API نفسها

A TCP socket is a stream socket. RFC 9293 describes TCP as a reliable, in-order, byte-stream service. It sets up a connection with a three-way handshake, numbers every byte, checks it with a checksum and retransmits what is lost. That reliability costs setup time and state on both hosts, which is exactly the connection that listen(), connect() and accept() manage.

الـ socket من نوع TCP هو stream socket. تصف RFC 9293 بروتوكول TCP بأنه خدمة موثوقة، تحافظ على الترتيب، وتنقل تيارًا من البايتات (byte stream). يُنشئ اتصالًا عبر مصافحة ثلاثية (three-way handshake)، ويرقّم كل بايت، ويتحقق منه بـ checksum، ويعيد إرسال ما يُفقد. هذه الموثوقية تكلّف وقتًا للإعداد وحالة (state) محفوظة على الجهازين، وهذا بالضبط هو الاتصال الذي تديره listen() وconnect() وaccept().

A UDP socket is a datagram socket. RFC 768 says UDP offers a minimum of protocol mechanism: delivery and duplicate protection are not guaranteed, and the header is just four 16-bit fields (source Port number, destination port, length, checksum), 8 bytes in total. There is no listen or accept: each datagram stands alone. POSIX names the two kinds SOCK_STREAM and SOCK_DGRAM.

الـ socket من نوع UDP هو datagram socket. تقول RFC 768 إن UDP يقدّم الحد الأدنى من آليات البروتوكول: التسليم والحماية من التكرار غير مضمونين، والـ header مجرد أربعة حقول كل منها 16 بت (port المصدر، وport الوجهة، والطول، والـ checksum)، أي 8 بايتات إجمالًا. لا يوجد listen ولا accept: كل datagram قائم بذاته. ويسمّي POSIX النوعين SOCK_STREAM وSOCK_DGRAM.

PropertyالخاصيةTCP socketTCP socketUDP socketUDP socket
POSIX typeنوع POSIXSOCK_STREAMSOCK_STREAMSOCK_DGRAMSOCK_DGRAM
Connection setupإنشاء الاتصالThree-way handshake before any dataمصافحة ثلاثية (three-way handshake) قبل أي بياناتNone, each datagram stands aloneلا يوجد، كل datagram قائم بذاته
DeliveryالتسليمLost segments detected and retransmittedالمقاطع (segments) المفقودة تُكتشف ويُعاد إرسالهاNo delivery or duplicate guaranteeلا ضمان للتسليم ولا لعدم التكرار
OrderingالترتيبBytes reach the application in orderالبايتات تصل إلى التطبيق بالترتيبDatagrams may arrive out of orderقد تصل الـ datagrams بغير ترتيبها
Message boundariesحدود الرسائلA continuous byte stream, no boundariesتيار بايتات متصل، بلا حدود بين الرسائلEach datagram is one whole messageكل datagram رسالة واحدة كاملة
Header overheadكلفة الـ header20 bytes minimum, plus connection state20 بايتًا كحد أدنى، إضافة إلى حالة الاتصال8 bytes, four 16-bit fields8 بايتات، أربعة حقول بطول 16 بت
Typical usesاستخدامات نموذجيةWeb pages, file transfer, emailصفحات الويب، ونقل الملفات، والبريد الإلكترونيDNS queries, voice and video callsاستعلامات DNS، ومكالمات الصوت والفيديو
TCP socket versus UDP socketTCP socket مقابل UDP socket
TCP recovers a late box 3 and delivers in order; UDP drops box 3 and swaps boxes 2 and 4TCP يسترجع الصندوق 3 المتأخر ويسلّم بالترتيب، أما UDP فيُسقط الصندوق 3 ويبدّل ترتيب الصندوقين 2 و4

Choosing a socket for a workloadاختيار الـ socket المناسب لحِمل العمل

The right question is not which is faster. Ask what happens to your application when one unit of data is lost or late. If it must be recovered, use TCP. If late data is worthless, or the exchange is tiny and easy to retry, UDP avoids overhead that buys you nothing. For interactive media this is really about Latency: a retransmitted frame arrives after its moment has passed.

السؤال الصحيح ليس: أيهما أسرع؟ اسأل: ماذا يحدث لتطبيقك عندما تُفقد وحدة بيانات واحدة أو تتأخر؟ إذا كان لا بد من استرجاعها، فاستخدم TCP. وإذا كانت البيانات المتأخرة بلا قيمة، أو كان التبادل صغيرًا وسهل الإعادة، فإن UDP يجنّبك كلفة إضافية لا تكسب منها شيئًا. وفي الوسائط التفاعلية، المسألة في حقيقتها مسألة latency: الإطار المعاد إرساله يصل بعد أن فات وقته.

Workloadحِمل العملSocketالـ socketWhyالسبب
Web page over HTTP/1.1 or HTTP/2صفحة ويب عبر HTTP/1.1 أو HTTP/2TCPTCPEvery byte of HTML and script must arrive intact. Default ports 80 and 443.كل بايت من الـ HTML والـ script يجب أن يصل سليمًا. الـ ports الافتراضية 80 و443.
File transfer (FTP) and emailنقل الملفات (FTP) والبريد الإلكترونيTCPTCPOne missing byte corrupts the file or message.بايت واحد مفقود يفسد الملف أو الرسالة.
DNS queryاستعلام DNSUDPUDPA small single exchange that can simply be retried. Port 53, with TCP 53 for zone transfers and long answers.تبادل واحد صغير يمكن ببساطة إعادته. Port 53، مع TCP 53 لنقل المناطق (zone transfers) والإجابات الطويلة.
Video call (WebRTC)مكالمة فيديو (WebRTC)UDPUDPA late frame is useless, so retransmission only hurts. TCP through a relay is a fallback.الإطار المتأخر لا فائدة منه، لذا فإعادة الإرسال تضر فقط. وTCP عبر relay هو خيار احتياطي.
Web page over HTTP/3صفحة ويب عبر HTTP/3UDP (carrying QUIC)UDP (يحمل QUIC)QUIC adds reliable streams and encryption on top of UDP.QUIC يضيف تيارات موثوقة (reliable streams) وتشفيرًا فوق UDP.
Which socket for which workloadأي socket لأي حِمل عمل

The last row of that table shows that the choice is not always binary. RFC 9114 defines HTTP/3 as the same HTTP semantics over a new transport, QUIC, and RFC 9000 says QUIC packets are carried in UDP datagrams. QUIC rebuilds reliable streams, flow control and encryption in user space on top of UDP. So choosing UDP does not always mean giving up reliability. It means a protocol or library above UDP decides how much reliability to add, and when.

الصف الأخير في ذلك الجدول يبيّن أن الاختيار ليس ثنائيًا دائمًا. تعرّف RFC 9114 بروتوكول HTTP/3 بأنه دلالات (semantics) HTTP نفسها فوق طبقة نقل جديدة هي QUIC، وتقول RFC 9000 إن حزم QUIC تُحمل داخل UDP datagrams. يعيد QUIC بناء التيارات الموثوقة (reliable streams) والتحكم في التدفق (flow control) والتشفير في user space فوق UDP. إذن اختيار UDP لا يعني دائمًا التخلي عن الموثوقية. بل يعني أن بروتوكولًا أو مكتبة فوق UDP هي التي تقرر كم من الموثوقية تضيف، ومتى.

Quick checkاختبار سريع

A video call plays 50 voice frames per second and playback never pauses to wait. Which socket fits best, and why?مكالمة فيديو تشغّل 50 إطارًا صوتيًا في الثانية، والتشغيل لا يتوقف أبدًا للانتظار. أي socket هو الأنسب، ولماذا؟

Quick checkاختبار سريع

HTTP/3 keeps the same HTTP semantics but changes the transport underneath. What does it run on?يحتفظ HTTP/3 بدلالات HTTP نفسها لكنه يغيّر طبقة النقل تحتها. على ماذا يعمل؟

Recall

Why do DNS queries use UDP while file transfers use TCP, and where does HTTP/3 fit?لماذا تستخدم استعلامات DNS بروتوكول UDP بينما يستخدم نقل الملفات TCP، وأين يقع HTTP/3 من ذلك؟

A DNS query is a small single exchange that can simply be retried, so datagrams have less overhead (RFC 1035 prefers them for queries). A file needs every byte in order, so it uses TCP. HTTP/3 uses QUIC, which runs over UDP and adds its own reliability and streams.
استعلام DNS تبادل واحد صغير يمكن ببساطة إعادته، لذا فالـ datagrams أقل كلفة (وRFC 1035 تفضّلها للاستعلامات). أما الملف فيحتاج إلى كل بايت وبالترتيب، لذا يستخدم TCP. ويستخدم HTTP/3 بروتوكول QUIC، الذي يعمل فوق UDP ويضيف موثوقيته وتياراته الخاصة.

Now bring sockets to the web. You type https://uni.majid.app and press Enter. The browser resolves the name, asks the operating system to open a TCP connection to port 443 (or QUIC over UDP for HTTP/3), sends the HTTP request, reads the response, and may reuse the same connection for the next requests. The page code only calls fetch("/api/...") and never sees a Socket.

الآن لننقل الـ sockets إلى الويب. تكتب https://uni.majid.app وتضغط Enter. يحلّ المتصفح الاسم، ثم يطلب من نظام التشغيل فتح اتصال TCP إلى port 443 (أو QUIC فوق UDP في حالة HTTP/3)، ويرسل طلب HTTP، ويقرأ الاستجابة، وقد يعيد استخدام الاتصال نفسه للطلبات التالية. أما كود الصفحة فلا يفعل إلا استدعاء fetch("/api/...")، ولا يرى أي socket أبدًا.

The browser owns the client socketالمتصفح هو من يملك الـ client socket

A web application is a Network application in which the browser is the Client program and owns the client socket: when to open it, reuse it and close it. MDN puts it simply: browsers communicate with web servers using HTTP, by sending a request to the server. Every socket call from the lifecycle above is still happening, just inside the browser and inside the server runtime.

تطبيق الويب هو network application يكون فيه المتصفح هو برنامج الـ client، ويملك الـ client socket: متى يفتحه، ومتى يعيد استخدامه، ومتى يغلقه. تقولها MDN ببساطة: المتصفحات تتواصل مع خوادم الويب باستخدام HTTP، عن طريق إرسال طلب إلى الخادم. كل استدعاءات الـ socket من دورة الحياة السابقة ما زالت تحدث، لكنها تحدث داخل المتصفح وداخل بيئة تشغيل الخادم (server runtime).

This changes the level at which you work. Frontend code for the World Wide Web is written in terms of HTTP requests, and backend code is written in terms of routes that answer them. The socket becomes infrastructure you rely on rather than an object you manage.

هذا يغيّر المستوى الذي تعمل عليه. كود الـ frontend في World Wide Web يُكتب بلغة طلبات HTTP، وكود الـ backend يُكتب بلغة الـ routes التي تجيب عنها. ويصبح الـ socket بنية تحتية تعتمد عليها، لا كائنًا تديره بنفسك.

Recall

In a web application, who manages the client socket, and at what level does the frontend code work?في تطبيق الويب، من يدير الـ client socket، وعلى أي مستوى يعمل كود الـ frontend؟

The browser opens, reuses and closes the connection (TCP, or QUIC over UDP for HTTP/3). Frontend code works at the HTTP level, for example with fetch, and never touches the socket.
المتصفح هو من يفتح الاتصال ويعيد استخدامه ويغلقه (TCP، أو QUIC فوق UDP في حالة HTTP/3). وكود الـ frontend يعمل على مستوى HTTP، مثلًا باستخدام fetch، ولا يلمس الـ socket أبدًا.

Three components: client, server, databaseثلاثة مكوّنات: العميل، والخادم، وقاعدة البيانات

So what is on the other end of that connection? Picture a course portal. You submit a homework answer. The browser sends a request over the Internet. The server checks it and writes a row to the database. When you reopen the page later, the server reads that row and sends it back in a response.

إذن ما الذي يوجد في الطرف الآخر من ذلك الاتصال؟ تخيّل بوابة مقرر دراسي. أنت ترسل إجابة واجب. يرسل المتصفح طلبًا عبر الإنترنت. يتحقق الخادم منه ويكتب صفًا (row) في قاعدة البيانات. وعندما تعيد فتح الصفحة لاحقًا، يقرأ الخادم ذلك الصف ويعيده إليك في استجابة.

The pieces have names. The Frontend is one Component: the client. The Backend is two components: the Server and the database. The client talks only to the server, never directly to the database. MDN describes the same flow: on a request, the server fetches data from the database and builds the response. This is the Request-response loop you met with network applications, and the client always starts it.

لهذه الأجزاء أسماء. الـ frontend هو component واحد: العميل. والـ backend مكوّنان: الـ server وقاعدة البيانات. العميل يتحدث مع الخادم فقط، ولا يتحدث مع قاعدة البيانات مباشرة أبدًا. وتصف MDN التدفق نفسه: عند وصول طلب، يجلب الخادم البيانات من قاعدة البيانات ويبني الاستجابة. هذه هي request-response loop التي قابلتها مع تطبيقات الشبكة، والعميل هو من يبدؤها دائمًا.

Client
Frontend

Browser: HTML, CSS, JavaScript

المتصفح: HTML وCSS وJavaScript

HTTP request and responseطلب HTTP واستجابته
Server
Backend

Checks requests, runs the logic, builds responses

يتحقق من الطلبات، وينفذ المنطق، ويبني الاستجابات

database protocol over its own socketبروتوكول قاعدة البيانات عبر socket خاص به
Database
Backend

Stores data that must survive between requests

تخزن البيانات التي يجب أن تبقى بين الطلبات

Frontend and backend of a web applicationالـ frontend والـ backend في تطبيق الويب

Look at the server to database link closely, because it reuses everything from the first concept. It is itself a client-server connection over sockets, with the web server acting as the database client. Writes flow from server to database and reads flow back. A web application is therefore a chain of socket conversations, each with its own client and server role.

انظر بدقة إلى الرابط بين الخادم وقاعدة البيانات، لأنه يعيد استخدام كل شيء من المفهوم الأول. فهو بحد ذاته اتصال client-server عبر sockets، يكون فيه خادم الويب هو عميل قاعدة البيانات. عمليات الكتابة تتدفق من الخادم إلى قاعدة البيانات، وعمليات القراءة تعود في الاتجاه المعاكس. إذن تطبيق الويب سلسلة من المحادثات عبر الـ sockets، لكل منها دور العميل ودور الخادم الخاصان بها.

Request, write, read, response: the client only ever talks to the serverطلب، ثم كتابة، ثم قراءة، ثم استجابة: العميل لا يتحدث إلا مع الخادم

Why server and database often live apartلماذا يعيش الخادم وقاعدة البيانات غالبًا منفصلين

  • They scale separately: many identical server instances can share one carefully managed database.يتوسّعان بشكل مستقل: يمكن لعدد كبير من نسخ الخادم المتطابقة أن تتشارك قاعدة بيانات واحدة تُدار بعناية.
  • Their resource needs differ: the server wants CPU, the database wants disk and memory.احتياجاتهما من الموارد مختلفة: الخادم يحتاج إلى CPU، وقاعدة البيانات تحتاج إلى القرص والذاكرة.
  • The database can be kept off the public Internet, reachable only from the servers.يمكن إبقاء قاعدة البيانات بعيدًا عن الإنترنت العام، بحيث لا يصل إليها إلا الخوادم.

This is the first placement decision on the edge-to-cloud continuum. Later parts ask where the server and the database should each live, and every placement has a cost: each read or write between separate machines adds its own network Round-trip time (RTT), which later feeds into Response time (RT).

هذا هو أول قرار توزيع (placement) على سلسلة الـ edge-to-cloud continuum. الأجزاء اللاحقة تسأل أين يجب أن يعيش كل من الخادم وقاعدة البيانات، ولكل توزيع ثمن: كل عملية قراءة أو كتابة بين جهازين منفصلين تضيف RTT شبكيًا خاصًا بها، وهذا يدخل لاحقًا في response time.

Quick checkاختبار سريع

A user saves a form on a web app and the data must persist. Which path does it take?يحفظ مستخدم نموذجًا في تطبيق ويب، ويجب أن تبقى البيانات محفوظة. ما المسار الذي تسلكه؟

Recall

Name the two backend components of a web application, and give one reason they often run on different machines.سمِّ مكوّني الـ backend في تطبيق الويب، واذكر سببًا واحدًا لكونهما يعملان غالبًا على جهازين مختلفين.

The server and the database. They scale independently, their resource needs differ (CPU versus storage and memory), and the database can stay off the public Internet. The cost is an extra network round trip per read or write.
الخادم وقاعدة البيانات. يتوسّعان بشكل مستقل، واحتياجاتهما من الموارد مختلفة (CPU مقابل التخزين والذاكرة)، ويمكن إبقاء قاعدة البيانات بعيدًا عن الإنترنت العام. والثمن رحلة ذهاب وإياب إضافية عبر الشبكة لكل عملية قراءة أو كتابة.

The architecture becomes real when you read the code on each side. Consider a calculator running in the browser and the backend that serves it. Walk one request through: the user opens the About view, and the frontend sends GET /calculadora/about.

تصبح البنية حقيقية عندما تقرأ الكود في كل جهة. خذ آلة حاسبة تعمل في المتصفح، والـ backend الذي يخدمها. تتبّع طلبًا واحدًا: المستخدم يفتح صفحة About، فيرسل الـ frontend الطلب GET /calculadora/about.

From request to handler to responseمن الطلب إلى الـ handler إلى الاستجابة

On the Backend, the framework compares the request method and path against its routes. It finds the route with method GET and path /calculadora/about and calls its handler, handler(request, h). The handler builds an object with a message field and returns it. In hapi, a returned plain object becomes the response payload, serialized as JSON. The browser receives that JSON in the HTTP response body and the Frontend renders it.

في الـ backend، يقارن الـ framework الـ method والـ path في الطلب بالـ routes المسجلة لديه. فيجد الـ route الذي الـ method فيه GET والـ path فيه /calculadora/about، ويستدعي الـ handler الخاص به، handler(request, h). يبني الـ handler كائنًا (object) فيه حقل message ويُرجعه. في hapi، الكائن العادي الذي يُرجَع يصبح هو محتوى الاستجابة (payload)، مُحوَّلًا إلى JSON. يستقبل المتصفح هذا الـ JSON في جسم استجابة الـ HTTP، ويعرضه الـ frontend.

The general rule: a route is a method plus a path plus a handler. The set of routes a backend exposes is its Web API, the Interface the frontend programs against, while HTTP is the Protocol carried between them. Protocol plus interface again, one level higher than the socket API. Underneath, the Node.js runtime owns a listening socket and one connection socket per Client, exactly the welcoming door picture from the first concept.

القاعدة العامة: الـ route هو method زائد path زائد handler. ومجموعة الـ routes التي يكشفها الـ backend هي الـ Web API الخاصة به، أي الـ interface التي يبرمج الـ frontend بالاعتماد عليها، بينما HTTP هو الـ protocol المنقول بينهما. مرة أخرى protocol زائد interface، لكن في مستوى أعلى بدرجة من الـ socket API. وتحت ذلك كله، تملك بيئة تشغيل Node.js listening socket واحدًا وconnection socket واحدًا لكل client، وهي بالضبط صورة باب الاستقبال من المفهوم الأول.

Worked exampleمثال محلول

Tracing GET /calculadora/aboutتتبّع الطلب GET /calculadora/about

  1. Requestالطلب

    The browser sends GET /calculadora/about over its connection to the server.
    يرسل المتصفح GET /calculadora/about عبر اتصاله بالخادم.
  2. Route matchمطابقة الـ route

    The framework matches method GET and path /calculadora/about to the registered route.
    يطابق الـ framework الـ method GET والـ path /calculadora/about مع الـ route المسجّل.
  3. Handlerالـ handler

    handler(request, h) runs and returns an object with a message field.
    يعمل handler(request, h) ويُرجع كائنًا فيه حقل message.
  4. Serializeالتحويل إلى JSON

    The framework turns the object into a JSON response body.
    يحوّل الـ framework الكائن إلى جسم استجابة بصيغة JSON.
  5. Responseالاستجابة

    The response travels back over the same connection, closing the Request-response loop.
    تعود الاستجابة عبر الاتصال نفسه، فتُغلق الـ request-response loop.
  6. Resultالنتيجة

    What the handler returns is exactly what the browser receives, as JSON.
    ما يُرجعه الـ handler هو بالضبط ما يستقبله المتصفح، بصيغة JSON.

Same language, different machinesاللغة نفسها، وأجهزة مختلفة

Notice the languages. The frontend uses HTML for structure, CSS for presentation and JavaScript for behaviour, all inside the browser. The backend also runs JavaScript, but in the Node.js runtime on the Server. Same language, different processes on different machines, joined only by HTTP over a socket.

لاحظ اللغات. الـ frontend يستخدم HTML للبنية، وCSS للعرض، وJavaScript للسلوك، وكلها داخل المتصفح. والـ backend يشغّل JavaScript أيضًا، لكن داخل بيئة تشغيل Node.js على الـ server. اللغة نفسها، لكن في عمليات مختلفة على أجهزة مختلفة، لا يربطها إلا HTTP فوق socket.

SideالجهةRuns whereأين يعملWritten inمكتوب بـRoleالدور
Frontendالـ FrontendInside the browser on the user machineداخل المتصفح على جهاز المستخدمHTML, CSS, JavaScriptHTML وCSS وJavaScriptDraws the calculator and sends HTTP requestsيرسم الآلة الحاسبة ويرسل طلبات HTTP
Backendالـ BackendIn the Node.js runtime on a serverداخل بيئة تشغيل Node.js على خادمJavaScript (hapi on Node.js)JavaScript (hapi فوق Node.js)Matches routes and returns data in responsesيطابق الـ routes ويُرجع البيانات في الاستجابات
The two halves of the calculator appنصفا تطبيق الآلة الحاسبة

Recall

What three things make up a backend route, and what does the client receive when the route is hit?ما الأشياء الثلاثة التي يتكون منها الـ route في الـ backend، وماذا يستقبل العميل عندما يُستدعى هذا الـ route؟

A method, a path and a handler. The framework matches the request method and path, calls the handler, and sends what the handler returns (here, an object serialized as JSON) back in the HTTP response.
method وpath وhandler. يطابق الـ framework الـ method والـ path في الطلب، ويستدعي الـ handler، ثم يرسل ما يُرجعه الـ handler (هنا كائن محوَّل إلى JSON) في استجابة الـ HTTP.

Recapالخلاصة

If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا

  • An API is how software asks other software for a service. The socket API is the door from the application layer to the transport layer.الـ API هي الطريقة التي يطلب بها برنامج خدمة من برنامج آخر. والـ socket API هي الباب من طبقة التطبيق إلى طبقة النقل.
  • A socket is a file descriptor. Programs read and write it like a file, and it is addressed by IP address plus port.الـ socket هو file descriptor. البرامج تقرأ منه وتكتب فيه كأنه ملف، ويُعنوَن بعنوان IP زائد رقم port.
  • Servers call socket, bind, listen and accept. Clients call socket and connect. accept returns a new socket per client.الخوادم تستدعي socket وbind وlisten وaccept. والعملاء يستدعون socket وconnect. وتُرجع accept socket جديدًا لكل عميل.
  • TCP sockets give a reliable, ordered byte stream with connection overhead. UDP sockets send independent datagrams with minimal overhead.الـ TCP sockets تعطي تيار بايتات موثوقًا ومرتبًا مع كلفة إنشاء الاتصال. والـ UDP sockets ترسل datagrams مستقلة بأقل كلفة ممكنة.
  • Choose by what a lost or late unit costs: files and web pages use TCP, DNS queries and video calls use UDP, and HTTP/3 runs on QUIC over UDP.اختر بحسب كلفة فقدان وحدة بيانات أو تأخرها: الملفات وصفحات الويب تستخدم TCP، واستعلامات DNS ومكالمات الفيديو تستخدم UDP، وHTTP/3 يعمل على QUIC فوق UDP.
  • In a web app the browser manages the client socket. The backend is a server plus a database that may run on different machines.في تطبيق الويب، المتصفح يدير الـ client socket. والـ backend هو خادم زائد قاعدة بيانات، وقد يعملان على جهازين مختلفين.
  • A backend route is method plus path plus handler, and what the handler returns becomes the HTTP response.الـ route في الـ backend هو method زائد path زائد handler، وما يُرجعه الـ handler يصبح استجابة الـ HTTP.

Sourcesالمصادر