Lecture 01 · Introduction
المحاضرة 01 · مقدمة
Sockets, TCP vs UDP, and web app routes
المقابس (Sockets)، وTCP مقابل UDP، ومسارات تطبيقات الويب
P2 SHOULDP2 مهمThe socket is the door between your program and the network. Pick the socket type by what a lost packet costs you, then climb up to a web app: browser, server, database, and a route that is method plus path plus handler.
الـsocket هو الباب بين برنامجك والشبكة. اختر نوعه حسب كلفة ضياع حزمة عليك، ثم اصعد إلى تطبيق الويب: متصفح، خادم، قاعدة بيانات، وroute يتكوّن من method وpath وhandler.
The socket API: the door between AL and TL
الـ socket API: الباب بين AL وTL
Slide 40 asks how network applications interface to the Internet, and answers with three letters: API, Application Programming Interface, the set of operations one piece of software offers another. Slide 41 names the one that matters here: the socket API.
تسأل الشريحة 40 كيف تتصل التطبيقات الشبكية بالإنترنت، وتجيب بثلاثة أحرف: API، أي Application Programming Interface، وهي مجموعة العمليات التي يقدّمها برنامج لآخر. وتسمّي الشريحة 41 الواجهة المهمة هنا: socket API.
- The socket is the interface between the application layer (AL) and the transport layer (TL).
الـsocket هو الواجهة بين application layer (AL) وtransport layer (TL).
- In OS terms it looks like a file descriptor: writing to or reading from a socket is like writing to or reading from a file.
بمصطلحات نظام التشغيل يشبه file descriptor: الكتابة في الـsocket أو القراءة منه تشبه الكتابة في ملف أو القراءة منه.
- Socket address = IP address + port number. The IP finds the host; the port finds the process on that host.
Socket address = IP address + port number. عنوان IP يحدد الجهاز، والـport يحدد العملية على ذلك الجهاز.
- The bridge idea: your program does not need to know how TCP or UDP work. The socket API gives a high-level way to send and receive without the underlying complexity.
فكرة الجسر: برنامجك لا يحتاج أن يعرف كيف يعمل TCP أو UDP. الـsocket API يعطيك طريقة عالية المستوى للإرسال والاستقبال دون التعقيدات التحتية.
Concretely, in C, socket() returns a small integer such as 3, and the program then calls write(3, ...) exactly as for an open file. The kernel turns those bytes into TCP segments or UDP datagrams. Precise wording: the socket API is the interface; a socket is the endpoint the API creates.
عملياً، في لغة C يعيد socket() عدداً صغيراً مثل 3، ثم يستدعي البرنامج write(3, ...) تماماً كما مع ملف مفتوح. والنواة (kernel) تحوّل هذه البايتات إلى TCP segments أو UDP datagrams. الصياغة الدقيقة: الـsocket API هو الواجهة، والـsocket هو نقطة الاتصال التي تنشئها الواجهة.
The server's sockets: one listening, one per client
مقابس الـ server: واحد للاستماع، وواحد لكل client
A server and a client use different calls. The server must be findable first: socket(), bind() to a well-known address such as port 443, listen(), then accept(). The client only needs socket() and connect(), which for TCP runs the three-way handshake. The key fact: accept() returns a brand new connected socket for that one client, and the listening socket stays open waiting for the next.
الخادم والعميل يستخدمان استدعاءات مختلفة. يجب أن يكون الخادم قابلاً للعثور عليه أولاً: socket() ثم bind() على عنوان معروف مثل المنفذ 443 ثم listen() ثم accept(). أما العميل فيحتاج فقط socket() وconnect()، الذي ينفّذ في TCP المصافحة الثلاثية (three-way handshake). الحقيقة المهمة: accept() يعيد socket متصلاً جديداً تماماً لهذا العميل وحده، ويبقى socket الاستماع مفتوحاً ينتظر العميل التالي.
An HTTPS server has 1,000 clients connected on port 443. How many TCP sockets does the server process hold?
خادم HTTPS متصل به 1,000 عميل على المنفذ 443. كم TCP socket تملك عملية الخادم؟
accept() ran once per client -> 1,000 connected sockets the listening socket is still open -> 1 listening socket total = 1,001
Not 1 (one shared socket), not 1,000 (forgot the listener), not 2. Each connected socket is told apart by the client's IP address and port, even though all share server port 443.
ليس 1 (socket واحد مشترك)، ولا 1,000 (نسيان socket الاستماع)، ولا 2. كل socket متصل يتميّز بعنوان IP ومنفذ العميل، مع أنها كلها تشترك في منفذ الخادم 443.
TCP socket vs UDP socket
مقبس TCP مقابل مقبس UDP
Slide 42 lists two types of sockets. The type is fixed the moment socket() is called (POSIX: SOCK_STREAM vs SOCK_DGRAM).
تسرد الشريحة 42 نوعين من الـsockets. يتحدد النوع لحظة استدعاء socket() (في POSIX: SOCK_STREAM مقابل SOCK_DGRAM).
| TCP socketمقبس TCP | UDP socketمقبس UDP | |
|---|---|---|
| Kindالنوع | Stream (connection-oriented)تدفّق (Stream)، موجّه بالاتصال | Datagram (connectionless)Datagram، بلا اتصال |
| Setupالإعداد | Three-way handshake before dataمصافحة ثلاثية (three-way handshake) قبل البيانات | None, each datagram stands aloneلا يوجد، كل datagram مستقل بذاته |
| Lossالفقد | Detected and retransmitted: app sees no lossيُكتشف ويُعاد إرساله: التطبيق لا يرى أي فقد | Data can be lost, no retransmissionقد تُفقد البيانات، ولا إعادة إرسال |
| Orderالترتيب | Bytes arrive in orderالبايتات تصل بالترتيب | Can arrive out of orderقد تصل بغير ترتيب |
| Overheadالعبء الإضافي | Overhead maintaining the connection; header ≥ 20 bytesعبء في الحفاظ على الاتصال، والترويسة ≥ 20 بايت | Minimal; header 8 bytesأدنى حد، والترويسة 8 بايت |
| Typical useالاستخدام المعتاد | Web pages (HTTP/1.1, HTTP/2), files, emailصفحات الويب (HTTP/1.1، HTTP/2)، والملفات، والبريد | Live voice / video, DNS queriesالصوت والفيديو المباشر، واستعلامات DNS |
يقارن هذا الجدول النوعين. TCP اتصالي (connection-oriented)، يبدأ بمصافحة، ويكتشف الضياع ويعيد الإرسال، ويوصل البايتات بالترتيب، مقابل كلفة الحفاظ على الاتصال. UDP غير اتصالي (connectionless)، كل datagram مستقل، قد يضيع أو يصل بغير ترتيب، لكن كلفته دنيا ورأسه 8 bytes فقط. ملاحظة دقيقة: عبارة الشريحة "لا يمكن أن تضيع البيانات" في TCP تعني أن التطبيق لا يرى الضياع، لأن الحزم تضيع في الشبكة لكن TCP يعيد إرسالها.
The right question is not "which is faster". Ask: what does losing or delaying one unit of data cost this application? If it must be recovered, use TCP. If late data is worthless, retransmission only adds delay, so use UDP.
السؤال الصحيح ليس "أيهما أسرع". بل: ما كلفة ضياع أو تأخر وحدة واحدة من البيانات على هذا التطبيق؟ إذا وجب استرجاعها فاستخدم TCP. وإذا كانت البيانات المتأخرة بلا قيمة، فإعادة الإرسال تضيف تأخيراً فقط، فاستخدم UDP.
Web page → TCP
صفحة ويب ← TCP
Every byte of HTML and script must arrive intact and in order. HTTP/1.1 and HTTP/2 run on TCP (ports 80, 443). One missing byte breaks the page.
كل بايت من HTML والسكربت يجب أن يصل سليماً ومرتباً. HTTP/1.1 وHTTP/2 يعملان فوق TCP (المنفذان 80 و443). بايت واحد مفقود يكسر الصفحة.
Live video / voice → UDP
فيديو أو صوت مباشر ← UDP
At 50 frames per second each frame has a 20 ms slot. A frame resent after its slot is thrown away anyway, so skip it and play the next. Loss is tolerable; delay is not.
عند 50 إطاراً في الثانية لكل إطار نافذة 20 ms. الإطار المعاد إرساله بعد نافذته يُرمى على أي حال، فتخطَّه وشغّل التالي. الضياع مقبول، والتأخير غير مقبول.
"TCP is a phone call with a transcript. UDP is shouting across a room."
"TCP مكالمة هاتفية مع محضر مكتوب. وUDP صراخ عبر الغرفة."
The call is set up first and nothing in the transcript goes missing. A shout needs no setup; if one word is lost, you carry on.
المكالمة تُنشأ أولاً ولا يضيع شيء من المحضر. الصراخ لا يحتاج تجهيزاً، وإن ضاعت كلمة تكمل الكلام.
One modern twist worth knowing: HTTP/3 runs on QUIC, which is carried inside UDP datagrams and rebuilds reliability on top. So "UDP" does not always mean "no reliability"; it means the layer above decides.
لمسة حديثة تستحق المعرفة: HTTP/3 يعمل فوق QUIC المحمول داخل UDP datagrams، ويعيد بناء الموثوقية فوقه. إذن UDP لا يعني دائماً "بلا موثوقية"، بل يعني أن الطبقة الأعلى هي التي تقرر.
Web applications: the browser manages the client socket
تطبيقات الويب: المتصفح يدير مقبس الـ client
Slide 43's one line is the shift: the browser manages the client socket. Page code calls fetch("/api/...") and works at the HTTP level; the browser decides when to open, reuse and close the connection. Every socket call still happens, just inside the browser and the server runtime.
سطر الشريحة 43 الوحيد هو التحوّل: المتصفح يدير client socket. كود الصفحة يستدعي fetch("/api/...") ويعمل على مستوى HTTP، والمتصفح يقرر متى يفتح الاتصال ويعيد استخدامه ويغلقه. كل استدعاءات الـsocket ما زالت تحدث، لكن داخل المتصفح وبيئة تشغيل الخادم.
Slide 44 gives the architecture: Frontend = one component, the Client. Backend = two components, Server + Database, and "these two components may run on different machines". The client talks only to the server; the server writes to and reads from the database.
تعطي الشريحة 44 البنية: Frontend = مكوّن واحد هو الـClient. وBackend = مكوّنان: Server + Database، و"قد يعمل هذان المكوّنان على أجهزة مختلفة". الـclient يتكلم مع الخادم فقط، والخادم يكتب في قاعدة البيانات ويقرأ منها.
- The user presses Save. The browser sends an HTTP request over its socket across the Internet to the server.
يضغط المستخدم "حفظ". يرسل المتصفح طلب HTTP عبر الـsocket خلال الإنترنت إلى الخادم.
- The server validates the input and writes a row to the database.
يتحقق الخادم من المدخلات ويكتب صفاً في قاعدة البيانات.
- The database acknowledges the write. Later page loads read the row back the same way.
تؤكد قاعدة البيانات الكتابة. وعند فتح الصفحة لاحقاً يُقرأ الصف بالطريقة نفسها.
- The server sends the HTTP response; the browser renders the result.
يرسل الخادم رد HTTP، ويعرض المتصفح النتيجة.
A backend route: method + path + handler
مسار في الـ backend: الطريقة + المسار + المعالج (method + path + handler)
Slide 45 shows the calculator from both sides: the front-end in the browser (HTML, CSS, JavaScript), the back-end in Node.js, joined by HTTP. The back-end code registers a route:
تعرض الشريحة 45 الآلة الحاسبة من الجهتين: الـfront-end في المتصفح (HTML, CSS, JavaScript)، والـback-end في Node.js، ويربطهما HTTP. كود الـback-end يسجّل route:
server.route({
method: 'GET',
path: '/calculadora/about',
handler: function (request, h) {
var data = { message: 'Calculator API ...' };
return data;
}
});
A route = method + path + handler. When a request arrives, the framework matches both the method and the path, calls the handler, and sends back whatever the handler returns (here an object, serialized as JSON) in the HTTP response.
route = method + path + handler. عند وصول طلب يطابق الإطار كلاً من method وpath، ثم يستدعي الـhandler، ويرسل ما يعيده (هنا كائن يتحول إلى JSON) في رد HTTP.
Trace two requests against that one route:
تتبّع طلبين على هذا الـroute الوحيد:
GET /calculadora/about method matches, path matches -> handler runs
response body: {"message": "Calculator API ..."}
POST /calculadora/about path matches, method does NOT -> no route matched
the handler never runs
Same path, different method, different outcome. That is why a route is identified by method and path.
المسار نفسه، وmethod مختلف، والنتيجة مختلفة. لهذا يتحدد الـroute بالـmethod والـpath معاً.
Two details from the slide: the heading "Font-end" is a typo for Front-end, and server.route with a (request, h) handler is the hapi framework running on Node.js. Node.js itself is a JavaScript runtime, not a language. Same language on both sides, different processes on different machines, joined only by HTTP over a socket.
تفصيلتان من الشريحة: عنوان "Font-end" خطأ مطبعي صوابه Front-end، وserver.route مع handler بالشكل (request, h) هو إطار hapi الذي يعمل فوق Node.js. وNode.js نفسه بيئة تشغيل لـJavaScript وليس لغة. اللغة نفسها في الجهتين، لكن في عمليتين على جهازين، لا يربطهما إلا HTTP عبر socket.
- "One socket shared by all clients" is wrong. accept() creates one connected socket per client. With the listener, 1,000 clients means 1,001 sockets.
عبارة "socket واحد يشترك فيه كل العملاء" خاطئة. accept() ينشئ socket متصلاً لكل عميل. ومع socket الاستماع يصبح 1,000 عميل = 1,001 sockets.
- "TCP for live video because lost frames get resent" is the wrong reasoning. A resent frame arrives after its playback moment and is discarded. Live media uses UDP because loss is tolerable and delay is not.
"TCP للفيديو الحي لأن الإطارات المفقودة يُعاد إرسالها" استدلال خاطئ. الإطار المعاد يصل بعد لحظة تشغيله فيُرمى. الوسائط الحية تستخدم UDP لأن الضياع مقبول والتأخير غير مقبول.
- A route is method AND path, not the path alone. GET and POST on the same path are different routes.
الـroute هو method وpath معاً، لا الـpath وحده. GET وPOST على المسار نفسه routes مختلفة.
- The browser never talks to the database. Client to server, server to database.
المتصفح لا يتكلم مع قاعدة البيانات أبداً. العميل إلى الخادم، والخادم إلى قاعدة البيانات.
Check yourself
اختبر نفسك
A TCP server process has 1,000 clients connected on port 443. Counting the listening socket, how many TCP sockets does it hold?
عملية خادم TCP متصل بها 1,000 عميل على المنفذ 443. مع احتساب socket الاستماع، كم TCP socket تملك؟
C. accept() returns a new connected socket per client (1,000), and the listening socket stays open (1). All share port 443 and are told apart by client IP and port. B forgets the listener; A and D confuse a port with a socket.
الجواب C. accept() يعيد socket متصلاً جديداً لكل عميل (1,000)، ويبقى socket الاستماع مفتوحاً (1). كلها تشترك في المنفذ 443 وتتميّز بعنوان IP ومنفذ العميل. الخيار B ينسى socket الاستماع، وA وD يخلطان بين المنفذ والـsocket.
A video call plays 50 voice frames per second and playback never pauses to wait. Which socket fits, and why?
مكالمة فيديو تشغّل 50 إطاراً صوتياً في الثانية ولا يتوقف التشغيل للانتظار. أي socket يناسب، ولماذا؟
B. Each frame has a 20 ms slot; a frame TCP resends arrives after its slot and is thrown away anyway, while TCP's in-order delivery stalls the frames behind it. A and C are the classic wrong reasoning, and D is false: UDP datagrams can be lost.
الجواب B. لكل إطار نافذة 20 ms، والإطار الذي يعيد TCP إرساله يصل بعد نافذته فيُرمى، كما أن التسليم المرتب في TCP يوقف الإطارات التي بعده. A وC هما الاستدلال الخاطئ الشائع، وD خطأ: UDP datagrams قد تضيع.
Per slide 41, what is a socket address?
بحسب الشريحة 41، ما هو socket address؟
A. The IP address identifies the host and the port identifies the process on it. MAC addresses are link-layer and never leave the local network; the file descriptor is the program's local handle, not an address; a port alone does not say which host.
الجواب A. عنوان IP يحدد الجهاز، والمنفذ يحدد العملية عليه. عناوين MAC تخص link layer ولا تغادر الشبكة المحلية، والـfile descriptor مقبض محلي للبرنامج وليس عنواناً، والمنفذ وحده لا يحدد الجهاز.
A user saves a form on a web app and the data must persist. Which path does it take?
يحفظ مستخدم نموذجاً في تطبيق ويب ويجب أن تبقى البيانات. أي مسار تسلكه؟
A. The frontend is one component (client) and talks only to the server; the backend's two components are server and database, and the server does the write. Every other option has the browser touching the database.
الجواب A. الـfrontend مكوّن واحد (الـclient) ويتكلم مع الخادم فقط، والـbackend مكوّنان هما الخادم وقاعدة البيانات، والخادم هو من يكتب. كل الخيارات الأخرى تجعل المتصفح يلمس قاعدة البيانات.
A backend has the route from slide 45. A client sends POST /calculadora/about. What happens?
في الـbackend الـroute نفسه من الشريحة 45. يرسل عميل POST /calculadora/about. ماذا يحدث؟
D. The registered route is GET + /calculadora/about. The framework matches both the method and the path, so a POST to the same path does not reach that handler. A is the "path alone" trap; B and C invent behaviour no framework does.
الجواب D. الـroute المسجَّل هو GET + /calculadora/about. الإطار يطابق method وpath معاً، فطلب POST على المسار نفسه لا يصل إلى هذا الـhandler. A هو فخ "المسار وحده"، وB وC يخترعان سلوكاً لا يفعله أي إطار.