Debian Codegraph @

po/ar/man2/select_tut.2.po

raw
1 # Arabic translation of manpages
2 # This file is distributed under the same license as the manpages-l10n package.
3 # Copyright © of this file:
4 # زايد السعيدي <[email protected]>, 2026.
5 msgid ""
6 msgstr ""
7 "Project-Id-Version: manpages-l10n\n"
8 "POT-Creation-Date: 2026-06-05 06:42+0200\n"
9 "PO-Revision-Date: 2026-06-03 04:49+0400\n"
10 "Last-Translator: Zayed Al-Saidi <[email protected]>\n"
11 "Language-Team: Arabic <[email protected]>\n"
12 "Language: ar\n"
13 "MIME-Version: 1.0\n"
14 "Content-Type: text/plain; charset=UTF-8\n"
15 "Content-Transfer-Encoding: 8bit\n"
16 ".MIME-Version: 1.0\n"
17 "Plural-Forms: nplurals=6; plural=n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 "
18 "&& n%100<=10 ? 3 : n%100>=11 && n%100<=99 ? 4 : 5;\n"
19 "X-Generator: Lokalize 23.08.5\n"
20
21 #. type: TH
22 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
23 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
24 #, no-wrap
25 msgid "SELECT_TUT"
26 msgstr "SELECT_TUT"
27
28 #. type: TH
29 #: archlinux debian-unstable mageia-cauldron opensuse-leap-16-1
30 #: opensuse-tumbleweed
31 #, no-wrap
32 msgid "2026-02-08"
33 msgstr "8 فبراير 2026"
34
35 #. type: TH
36 #: archlinux debian-unstable
37 #, no-wrap
38 msgid "Linux man-pages 6.18"
39 msgstr "صفحات دليل لينكس 6.18"
40
41 #. type: SH
42 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
43 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
44 #, no-wrap
45 msgid "NAME"
46 msgstr "الاسم"
47
48 #. type: Plain text
49 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
50 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
51 msgid "select, pselect - synchronous I/O multiplexing"
52 msgstr "select, pselect - تعدد إدخال/إخراج متزامن"
53
54 #. type: SH
55 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
56 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
57 #, no-wrap
58 msgid "LIBRARY"
59 msgstr "المكتبة"
60
61 #. type: Plain text
62 #: archlinux debian-unstable fedora-44 fedora-rawhide mageia-cauldron
63 #: opensuse-leap-16-1 opensuse-tumbleweed
64 msgid "Standard C library (I<libc>,\\ I<-lc>)"
65 msgstr "مكتبة سي المعيارية (I<libc>،\\ I<-lc>)"
66
67 #. type: SH
68 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
69 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
70 #, no-wrap
71 msgid "SYNOPSIS"
72 msgstr "موجز"
73
74 #. type: Plain text
75 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
76 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
77 msgid "See B<select>(2)"
78 msgstr "انظر B<select>(2)"
79
80 #. type: SH
81 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
82 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
83 #, no-wrap
84 msgid "DESCRIPTION"
85 msgstr "الوصف"
86
87 #. type: Plain text
88 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
89 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
90 msgid ""
91 "The B<select>() and B<pselect>() system calls are used to efficiently "
92 "monitor multiple file descriptors, to see if any of them is, or becomes, "
93 "\"ready\"; that is, to see whether I/O becomes possible, or an \"exceptional "
94 "condition\" has occurred on any of the file descriptors."
95 msgstr ""
96 "تُستخدم استدعاءات النظام B<select>() وB<pselect>() لمراقبة واصفات ملفات "
97 "متعددة بكفاءة، لمعرفة ما إذا كان أي منها جاهزًا أو يصبح جاهزًا؛ أي لمعرفة ما "
98 "إذا كان الإدخال/الإخراج ممكنًا، أو حدثت \"حالة استثنائية\" على أي من واصفات "
99 "الملفات."
100
101 #. type: Plain text
102 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
103 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
104 msgid ""
105 "This page provides background and tutorial information on the use of these "
106 "system calls. For details of the arguments and semantics of B<select>() "
107 "and B<pselect>(), see B<select>(2)."
108 msgstr ""
109 "توفر هذه الصفحة معلومات أساسية وتعليمية حول استخدام استدعاءات النظام هذه. "
110 "للحصول على تفاصيل الوسائط ودلالات B<select>() وB<pselect>()، انظر "
111 "B<select>(2)."
112
113 #. type: SS
114 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
115 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
116 #, no-wrap
117 msgid "Combining signal and data events"
118 msgstr "دمج أحداث الإشارات والبيانات"
119
120 #. type: Plain text
121 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
122 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
123 msgid ""
124 "B<pselect>() is useful if you are waiting for a signal as well as for file "
125 "descriptor(s) to become ready for I/O. Programs that receive signals "
126 "normally use the signal handler only to raise a global flag. The global "
127 "flag will indicate that the event must be processed in the main loop of the "
128 "program. A signal will cause the B<select>() (or B<pselect>()) call to "
129 "return with I<errno> set to B<EINTR>. This behavior is essential so that "
130 "signals can be processed in the main loop of the program, otherwise "
131 "B<select>() would block indefinitely."
132 msgstr ""
133 "يكون B<pselect>() مفيدًا إذا كنت تنتظر إشارة بالإضافة إلى واصف (واصفات) ملف "
134 "لتصبح جاهزة للإدخال/الإخراج. تستخدم البرامج التي تستقبل الإشارات عادةً معالج "
135 "الإشارة فقط لرفع علامة عامة. ستشير العلامة العامة إلى أن الحدث يجب معالجته "
136 "في الحلقة الرئيسة للبرنامج. ستتسبب الإشارة في عودة استدعاء B<select>() (أو "
137 "B<pselect>()) مع تعيين I<errno> إلى B<EINTR>. هذا السلوك ضروري حتى يمكن "
138 "معالجة الإشارات في الحلقة الرئيسة للبرنامج، وإلا فسيحظر B<select>() إلى أجل "
139 "غير مسمى."
140
141 #. type: Plain text
142 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
143 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
144 msgid ""
145 "Now, somewhere in the main loop will be a conditional to check the global "
146 "flag. So we must ask: what if a signal arrives after the conditional, but "
147 "before the B<select>() call? The answer is that B<select>() would block "
148 "indefinitely, even though an event is actually pending. This race condition "
149 "is solved by the B<pselect>() call. This call can be used to set the "
150 "signal mask to a set of signals that are to be received only within the "
151 "B<pselect>() call. For instance, let us say that the event in question was "
152 "the exit of a child process. Before the start of the main loop, we would "
153 "block B<SIGCHLD> using B<sigprocmask>(2). Our B<pselect>() call would "
154 "enable B<SIGCHLD> by using an empty signal mask. Our program would look "
155 "like:"
156 msgstr ""
157 "الآن، سيكون هناك شرط في مكان ما في الحلقة الرئيسة للتحقق من العلامة العامة. "
158 "لذا يجب أن نسأل: ماذا لو وصلت إشارة بعد الشرط، ولكن قبل استدعاء B<select>()؟ "
159 "الإجابة هي أن B<select>() سيحظر إلى أجل غير مسمى، على الرغم من أن هناك حدثًا "
160 "معلقًا بالفعل. تُحل حالة السباق هذه بواسطة استدعاء B<pselect>(). يمكن استخدام "
161 "هذا الاستدعاء لضبط قناع الإشارة إلى مجموعة من الإشارات التي ستُستقبل فقط داخل "
162 "استدعاء B<pselect>(). على سبيل المثال، لنفترض أن الحدث المعني هو خروج عملية "
163 "تابعة. قبل بدء الحلقة الرئيسة، سنحظر B<SIGCHLD> باستخدام B<sigprocmask>(2). "
164 "سيمكّن استدعاء B<pselect>() الخاص بنا B<SIGCHLD> باستخدام قناع إشارة فارغ. "
165 "سيبدو برنامجنا كالتالي:"
166
167 #. type: Plain text
168 #: archlinux debian-unstable mageia-cauldron opensuse-leap-16-1
169 #: opensuse-tumbleweed
170 #, no-wrap
171 msgid ""
172 "static volatile sig_atomic_t got_SIGCHLD = 0;\n"
173 "\\&\n"
174 "static void\n"
175 "child_sig_handler(int sig)\n"
176 "{\n"
177 " got_SIGCHLD = 1;\n"
178 "}\n"
179 "\\&\n"
180 "int\n"
181 "main(int argc, char *argv[])\n"
182 "{\n"
183 " sigset_t sigmask, empty_mask;\n"
184 " struct sigaction sa;\n"
185 " fd_set readfds, writefds, exceptfds;\n"
186 " int r;\n"
187 "\\&\n"
188 " sigemptyset(&sigmask);\n"
189 " sigaddset(&sigmask, SIGCHLD);\n"
190 " if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) {\n"
191 " perror(\"sigprocmask\");\n"
192 " exit(EXIT_FAILURE);\n"
193 " }\n"
194 "\\&\n"
195 " sa.sa_flags = 0;\n"
196 " sa.sa_handler = child_sig_handler;\n"
197 " sigemptyset(&sa.sa_mask);\n"
198 " if (sigaction(SIGCHLD, &sa, NULL) == -1) {\n"
199 " perror(\"sigaction\");\n"
200 " exit(EXIT_FAILURE);\n"
201 " }\n"
202 "\\&\n"
203 " sigemptyset(&empty_mask);\n"
204 "\\&\n"
205 " for (;;) { /* main loop */\n"
206 " /* Initialize readfds, writefds, and exceptfds\n"
207 " before the pselect() call. (Code omitted.) */\n"
208 "\\&\n"
209 " r = pselect(nfds, &readfds, &writefds, &exceptfds,\n"
210 " NULL, &empty_mask);\n"
211 " if (r == -1 && errno != EINTR) {\n"
212 " /* Handle error */\n"
213 " }\n"
214 "\\&\n"
215 " if (got_SIGCHLD) {\n"
216 " got_SIGCHLD = 0;\n"
217 "\\&\n"
218 " /* Handle signalled event here; e.g., wait() for all\n"
219 " terminated children. (Code omitted.) */\n"
220 " }\n"
221 "\\&\n"
222 " /* main body of program */\n"
223 " }\n"
224 "}\n"
225 msgstr ""
226 "static volatile sig_atomic_t got_SIGCHLD = 0;\n"
227 "\\&\n"
228 "static void\n"
229 "child_sig_handler(int sig)\n"
230 "{\n"
231 " got_SIGCHLD = 1;\n"
232 "}\n"
233 "\\&\n"
234 "int\n"
235 "main(int argc, char *argv[])\n"
236 "{\n"
237 " sigset_t sigmask, empty_mask;\n"
238 " struct sigaction sa;\n"
239 " fd_set readfds, writefds, exceptfds;\n"
240 " int r;\n"
241 "\\&\n"
242 " sigemptyset(&sigmask);\n"
243 " sigaddset(&sigmask, SIGCHLD);\n"
244 " if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) {\n"
245 " perror(\"sigprocmask\");\n"
246 " exit(EXIT_FAILURE);\n"
247 " }\n"
248 "\\&\n"
249 " sa.sa_flags = 0;\n"
250 " sa.sa_handler = child_sig_handler;\n"
251 " sigemptyset(&sa.sa_mask);\n"
252 " if (sigaction(SIGCHLD, &sa, NULL) == -1) {\n"
253 " perror(\"sigaction\");\n"
254 " exit(EXIT_FAILURE);\n"
255 " }\n"
256 "\\&\n"
257 " sigemptyset(&empty_mask);\n"
258 "\\&\n"
259 " for (;;) { /* الحلقة الرئيسة */\n"
260 " /* هيئ readfds و writefds و exceptfds\n"
261 " قبل استدعاء pselect()‎. (الكود محذوف). */\n"
262 "\\&\n"
263 " r = pselect(nfds, &readfds, &writefds, &exceptfds,\n"
264 " NULL, &empty_mask);\n"
265 " if (r == -1 && errno != EINTR) {\n"
266 " /* عالج الخطأ */\n"
267 " }\n"
268 "\\&\n"
269 " if (got_SIGCHLD) {\n"
270 " got_SIGCHLD = 0;\n"
271 "\\&\n"
272 " /* عالج الحدث المؤشر عليه هنا؛ مثلاً wait()‎ لكل\n"
273 " الأبناء المنتهين. (الكود محذوف). */\n"
274 " }\n"
275 "\\&\n"
276 " /* المتن الرئيس للبرنامج */\n"
277 " }\n"
278 "}\n"
279
280 #. type: SS
281 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
282 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
283 #, no-wrap
284 msgid "Practical"
285 msgstr "عملي"
286
287 #. type: Plain text
288 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
289 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
290 msgid ""
291 "So what is the point of B<select>()? Can't I just read and write to my file "
292 "descriptors whenever I want? The point of B<select>() is that it watches "
293 "multiple descriptors at the same time and properly puts the process to sleep "
294 "if there is no activity. UNIX programmers often find themselves in a "
295 "position where they have to handle I/O from more than one file descriptor "
296 "where the data flow may be intermittent. If you were to merely create a "
297 "sequence of B<read>(2) and B<write>(2) calls, you would find that one of "
298 "your calls may block waiting for data from/to a file descriptor, while "
299 "another file descriptor is unused though ready for I/O. B<select>() "
300 "efficiently copes with this situation."
301 msgstr ""
302 "إذن ما الغاية من B<select>()؟ ألا يمكنني ببساطة القراءة والكتابة إلى واصفات "
303 "الملفات الخاصة بي متى شئت؟ الغاية من B<select>() هي أنه يراقب واصفات متعددة "
304 "في الوقت نفسه ويضع العملية في حالة سكون بشكل صحيح إذا لم يكن هناك نشاط. "
305 "غالبًا ما يجد مبرمجو يونكس أنفسهم في موقف يضطرون فيه إلى التعامل مع إدخال/"
306 "إخراج من أكثر من واصف ملف واحد حيث قد يكون تدفق البيانات متقطعًا. إذا قمت "
307 "ببساطة بإنشاء تسلسل من استدعاءات B<read>(2) وB<write>(2)، فستجد أن أحد "
308 "استدعاءاتك قد يحظر انتظار البيانات من/إلى واصف ملف، بينما واصف ملف آخر غير "
309 "مستخدم على الرغم من أنه جاهز للإدخال/الإخراج. يتعامل B<select>() بكفاءة مع "
310 "هذا الموقف."
311
312 #. type: SS
313 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
314 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
315 #, no-wrap
316 msgid "Select law"
317 msgstr "قانون select"
318
319 #. type: Plain text
320 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
321 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
322 msgid ""
323 "Many people who try to use B<select>() come across behavior that is "
324 "difficult to understand and produces nonportable or borderline results. For "
325 "instance, the above program is carefully written not to block at any point, "
326 "even though it does not set its file descriptors to nonblocking mode. It is "
327 "easy to introduce subtle errors that will remove the advantage of using "
328 "B<select>(), so here is a list of essentials to watch for when using "
329 "B<select>()."
330 msgstr ""
331 "يواجه الكثيرون ممن يحاولون استخدام B<select>() سلوكًا يصعب فهمه وينتج نتائج "
332 "غير قابلة للنقل أو نتائج حدية. على سبيل المثال، البرنامج أعلاه مكتوب بعناية "
333 "لعدم الحظر في أي نقطة، على الرغم من أنه لا يضبط واصفات الملفات الخاصة به على "
334 "وضع عدم الحظر. من السهل إدخال أخطاء دقيقة تزيل ميزة استخدام B<select>()، لذا "
335 "إليك قائمة بالأساسيات التي يجب مراقبتها عند استخدام B<select>()."
336
337 #. type: TP
338 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
339 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
340 #, no-wrap
341 msgid "1."
342 msgstr "1."
343
344 #. type: Plain text
345 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
346 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
347 msgid ""
348 "You should always try to use B<select>() without a timeout. Your program "
349 "should have nothing to do if there is no data available. Code that depends "
350 "on timeouts is not usually portable and is difficult to debug."
351 msgstr ""
352 "يجب أن تحاول دائمًا استخدام B<select>() بدون مهلة زمنية. يجب ألا يكون "
353 "لبرنامجك ما يفعله إذا لم تكن هناك بيانات متاحة. الكود الذي يعتمد على المهل "
354 "الزمنية ليس عادةً قابلاً للنقل ويصعب تنقيحه."
355
356 #. type: TP
357 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
358 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
359 #, no-wrap
360 msgid "2."
361 msgstr "2."
362
363 #. type: Plain text
364 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
365 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
366 msgid ""
367 "The value I<nfds> must be properly calculated for efficiency as explained "
368 "above."
369 msgstr "يجب حساب قيمة I<nfds> بشكل صحيح لتحقيق الكفاءة كما هو موضح أعلاه."
370
371 #. type: TP
372 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
373 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
374 #, no-wrap
375 msgid "3."
376 msgstr "3."
377
378 #. type: Plain text
379 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
380 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
381 msgid ""
382 "No file descriptor must be added to any set if you do not intend to check "
383 "its result after the B<select>() call, and respond appropriately. See next "
384 "rule."
385 msgstr ""
386 "يجب عدم إضافة أي واصف ملف إلى أي مجموعة إذا كنت لا تنوي التحقق من نتيجته بعد "
387 "استدعاء B<select>()، والاستجابة بشكل مناسب. انظر القاعدة التالية."
388
389 #. type: TP
390 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
391 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
392 #, no-wrap
393 msgid "4."
394 msgstr "4."
395
396 #. type: Plain text
397 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
398 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
399 msgid ""
400 "After B<select>() returns, all file descriptors in all sets should be "
401 "checked to see if they are ready."
402 msgstr ""
403 "بعد عودة B<select>()، يجب فحص جميع واصفات الملفات في جميع المجموعات لمعرفة "
404 "ما إذا كانت جاهزة."
405
406 #. type: TP
407 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
408 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
409 #, no-wrap
410 msgid "5."
411 msgstr "5."
412
413 #. type: Plain text
414 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
415 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
416 msgid ""
417 "The functions B<read>(2), B<recv>(2), B<write>(2), and B<send>(2) do I<not> "
418 "necessarily read/write the full amount of data that you have requested. If "
419 "they do read/write the full amount, it's because you have a low traffic load "
420 "and a fast stream. This is not always going to be the case. You should "
421 "cope with the case of your functions managing to send or receive only a "
422 "single byte."
423 msgstr ""
424 "الدوال B<read>(2) وB<recv>(2) وB<write>(2) وB<send>(2) I<لا> تقرأ/تكتب "
425 "بالضرورة كامل كمية البيانات التي طلبتها. إذا قرأت/كتبت كامل الكمية، فذلك لأن "
426 "لديك حمولة مرور منخفضة وتدفقًا سريعًا. لن يكون هذا هو الحال دائمًا. يجب أن "
427 "تتعامل مع حالة تمكن دوالك من إرسال أو استقبال بايت واحد فقط."
428
429 #. type: TP
430 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
431 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
432 #, no-wrap
433 msgid "6."
434 msgstr "6."
435
436 #. type: Plain text
437 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
438 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
439 msgid ""
440 "Never read/write only in single bytes at a time unless you are really sure "
441 "that you have a small amount of data to process. It is extremely "
442 "inefficient not to read/write as much data as you can buffer each time. The "
443 "buffers in the example below are 1024 bytes although they could easily be "
444 "made larger."
445 msgstr ""
446 "لا تقرأ/تكتب أبدًا بايتات مفردة فقط في المرة الواحدة إلا إذا كنت متأكدًا حقًا "
447 "من أن لديك كمية صغيرة من البيانات لمعالجتها. من غير الفعال للغاية عدم قراءة/"
448 "كتابة أكبر قدر ممكن من البيانات التي يمكنك تخزينها مؤقتًا في كل مرة. المخازن "
449 "المؤقتة في المثال أدناه هي 1024 بايت على الرغم من أنه يمكن جعلها أكبر بسهولة."
450
451 #. type: TP
452 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
453 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
454 #, no-wrap
455 msgid "7."
456 msgstr "7."
457
458 #. Nonetheless, you should still cope with these errors for completeness.
459 #. type: Plain text
460 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
461 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
462 msgid ""
463 "Calls to B<read>(2), B<recv>(2), B<write>(2), B<send>(2), and B<select>() "
464 "can fail with the error B<EINTR>, and calls to B<read>(2), B<recv>(2), "
465 "B<write>(2), and B<send>(2) can fail with I<errno> set to B<EAGAIN> "
466 "(B<EWOULDBLOCK>). These results must be properly managed (not done properly "
467 "above). If your program is not going to receive any signals, then it is "
468 "unlikely you will get B<EINTR>. If your program does not set nonblocking I/"
469 "O, you will not get B<EAGAIN>."
470 msgstr ""
471 "استدعاءات B<read>(2) وB<recv>(2) وB<write>(2) وB<send>(2) وB<select>() يمكن "
472 "أن تفشل مع الخطأ B<EINTR>، واستدعاءات B<read>(2) وB<recv>(2) وB<write>(2) "
473 "وB<send>(2) يمكن أن تفشل مع ضبط I<errno> على B<EAGAIN> (B<EWOULDBLOCK>). يجب "
474 "إدارة هذه النتائج بشكل صحيح (لم يُفعل ذلك بشكل صحيح أعلاه). إذا كان برنامجك "
475 "لن يستقبل أي إشارات، فمن غير المرجح أن تحصل على B<EINTR>. إذا كان برنامجك لا "
476 "يضبط الإدخال/الإخراج غير المحظور، فلن تحصل على B<EAGAIN>."
477
478 #. type: TP
479 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
480 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
481 #, no-wrap
482 msgid "8."
483 msgstr "8."
484
485 #. type: Plain text
486 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
487 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
488 msgid ""
489 "Never call B<read>(2), B<recv>(2), B<write>(2), or B<send>(2) with a buffer "
490 "length of zero."
491 msgstr ""
492 "لا تستدعِ أبدًا B<read>(2) أو B<recv>(2) أو B<write>(2) أو B<send>(2) بطول "
493 "مخزن مؤقت صفري."
494
495 #. type: TP
496 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
497 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
498 #, no-wrap
499 msgid "9."
500 msgstr "9."
501
502 #. type: Plain text
503 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
504 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
505 msgid ""
506 "If the functions B<read>(2), B<recv>(2), B<write>(2), and B<send>(2) fail "
507 "with errors other than those listed in B<7.>, or one of the input functions "
508 "returns 0, indicating end of file, then you should I<not> pass that file "
509 "descriptor to B<select>() again. In the example below, I close the file "
510 "descriptor immediately, and then set it to -1 to prevent it being included "
511 "in a set."
512 msgstr ""
513 "إذا فشلت الدوال B<read>(2) أو B<recv>(2) أو B<write>(2) أو B<send>(2) مع "
514 "أخطاء غير تلك المذكورة في B<7.‎>، أو أعادت إحدى دوال الإدخال 0، مما يشير إلى "
515 "نهاية الملف، فعندئذٍ يجب I<ألا> تمرر واصف الملف هذا إلى B<select>() مجددًا. في "
516 "المثال أدناه، أغلقتُ واصف الملف فورًا، ثم ضبطتُه على -1 لمنع إدراجه في مجموعة."
517
518 #. type: TP
519 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
520 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
521 #, no-wrap
522 msgid "10."
523 msgstr "10."
524
525 #. type: Plain text
526 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
527 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
528 msgid ""
529 "The timeout value must be initialized with each new call to B<select>(), "
530 "since some operating systems modify the structure. B<pselect>() however "
531 "does not modify its timeout structure."
532 msgstr ""
533 "يجب تهيئة قيمة المهلة الزمنية مع كل استدعاء جديد لـ B<select>()، لأن بعض "
534 "أنظمة التشغيل تعدل البنية. ومع ذلك، فإن B<pselect>() لا يعدل بنية المهلة "
535 "الخاصة به."
536
537 #. type: TP
538 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
539 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
540 #, no-wrap
541 msgid "11."
542 msgstr "11."
543
544 #. "I have heard" does not fill me with confidence, and doesn't
545 #. belong in a man page, so I've commented this point out.
546 #. .TP
547 #. 11.
548 #. I have heard that the Windows socket layer does not cope with OOB data
549 #. properly.
550 #. It also does not cope with
551 #. .BR select ()
552 #. calls when no file descriptors are set at all.
553 #. Having no file descriptors set is a useful
554 #. way to sleep the process with subsecond precision by using the timeout.
555 #. (See further on.)
556 #. type: Plain text
557 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
558 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
559 msgid ""
560 "Since B<select>() modifies its file descriptor sets, if the call is being "
561 "used in a loop, then the sets must be reinitialized before each call."
562 msgstr ""
563 "بما أن B<select>() يعدل مجموعات واصفات الملفات الخاصة به، فإذا كان الاستدعاء "
564 "يُستخدم في حلقة، فيجب إعادة تهيئة المجموعات قبل كل استدعاء."
565
566 #. type: SH
567 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
568 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
569 #, no-wrap
570 msgid "RETURN VALUE"
571 msgstr "قيمة الإرجاع"
572
573 #. type: Plain text
574 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
575 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
576 msgid "See B<select>(2)."
577 msgstr "انظر B<select>(2)."
578
579 #. type: SH
580 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
581 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
582 #, no-wrap
583 msgid "NOTES"
584 msgstr "ملاحظات"
585
586 #. type: Plain text
587 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
588 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
589 msgid ""
590 "Generally speaking, all operating systems that support sockets also support "
591 "B<select>(). B<select>() can be used to solve many problems in a portable "
592 "and efficient way that naive programmers try to solve in a more complicated "
593 "manner using threads, forking, IPCs, signals, memory sharing, and so on."
594 msgstr ""
595 "بشكل عام، تدعم جميع أنظمة التشغيل التي تدعم المقابس أيضًا B<select>(). يمكن "
596 "استخدام B<select>() لحل العديد من المشكلات بطريقة قابلة للنقل وفعالة يحاول "
597 "المبرمجون المبتدئون حلها بطريقة أكثر تعقيدًا باستخدام الخيوط، والتفريع "
598 "(forking)، والتواصل بين العمليات (IPCs)، والإشارات، ومشاركة الذاكرة، وما إلى "
599 "ذلك."
600
601 #. type: Plain text
602 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
603 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
604 msgid ""
605 "The B<poll>(2) system call has the same functionality as B<select>(), and "
606 "is somewhat more efficient when monitoring sparse file descriptor sets. It "
607 "is nowadays widely available, but historically was less portable than "
608 "B<select>()."
609 msgstr ""
610 "استدعاء النظام B<poll>(2) له نفس وظيفة B<select>()، وهو أكثر كفاءة إلى حد ما "
611 "عند مراقبة مجموعات واصفات الملفات المتناثرة. وهو متاح الآن على نطاق واسع، "
612 "ولكن تاريخيًا كان أقل قابلية للنقل من B<select>()."
613
614 #. type: Plain text
615 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
616 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
617 msgid ""
618 "The Linux-specific B<epoll>(7) API provides an interface that is more "
619 "efficient than B<select>(2) and B<poll>(2) when monitoring large numbers "
620 "of file descriptors."
621 msgstr ""
622 "توفر واجهة برمجة تطبيقات B<epoll>(7) الخاصة بلينكس واجهة أكثر كفاءة من "
623 "B<select>(2) وB<poll>(2) عند مراقبة أعداد كبيرة من واصفات الملفات."
624
625 #. type: SH
626 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
627 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
628 #, no-wrap
629 msgid "EXAMPLES"
630 msgstr "أمثلة"
631
632 #. type: Plain text
633 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
634 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
635 msgid ""
636 "Here is an example that better demonstrates the true utility of "
637 "B<select>(). The listing below is a TCP forwarding program that forwards "
638 "from one TCP port to another."
639 msgstr ""
640 "إليك مثال يوضح بشكل أفضل الفائدة الحقيقية لـ B<select>(). القائمة أدناه هي "
641 "برنامج توجيه TCP يوجه من منفذ TCP إلى آخر."
642
643 #. type: Plain text
644 #: archlinux debian-unstable mageia-cauldron opensuse-leap-16-1
645 #: opensuse-tumbleweed
646 #, no-wrap
647 msgid ""
648 "#include E<lt>arpa/inet.hE<gt>\n"
649 "#include E<lt>errno.hE<gt>\n"
650 "#include E<lt>netinet/in.hE<gt>\n"
651 "#include E<lt>signal.hE<gt>\n"
652 "#include E<lt>stdio.hE<gt>\n"
653 "#include E<lt>stdlib.hE<gt>\n"
654 "#include E<lt>string.hE<gt>\n"
655 "#include E<lt>sys/select.hE<gt>\n"
656 "#include E<lt>sys/socket.hE<gt>\n"
657 "#include E<lt>sys/types.hE<gt>\n"
658 "#include E<lt>unistd.hE<gt>\n"
659 "\\&\n"
660 "static int forward_port;\n"
661 "\\&\n"
662 "#undef max\n"
663 "#define max(x, y) ((x) E<gt> (y) ? (x) : (y))\n"
664 "\\&\n"
665 "static int\n"
666 "listen_socket(int listen_port)\n"
667 "{\n"
668 " int lfd;\n"
669 " int yes;\n"
670 " struct sockaddr_in addr;\n"
671 "\\&\n"
672 " lfd = socket(AF_INET, SOCK_STREAM, 0);\n"
673 " if (lfd == -1) {\n"
674 " perror(\"socket\");\n"
675 " return -1;\n"
676 " }\n"
677 "\\&\n"
678 " yes = 1;\n"
679 " if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR,\n"
680 " &yes, sizeof(yes)) == -1)\n"
681 " {\n"
682 " perror(\"setsockopt\");\n"
683 " close(lfd);\n"
684 " return -1;\n"
685 " }\n"
686 "\\&\n"
687 " memset(&addr, 0, sizeof(addr));\n"
688 " addr.sin_port = htons(listen_port);\n"
689 " addr.sin_family = AF_INET;\n"
690 " if (bind(lfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n"
691 " perror(\"bind\");\n"
692 " close(lfd);\n"
693 " return -1;\n"
694 " }\n"
695 "\\&\n"
696 " printf(\"accepting connections on port %d\\[rs]n\", listen_port);\n"
697 " listen(lfd, 10);\n"
698 " return lfd;\n"
699 "}\n"
700 "\\&\n"
701 "static int\n"
702 "connect_socket(int connect_port, char *address)\n"
703 "{\n"
704 " int cfd;\n"
705 " struct sockaddr_in addr;\n"
706 "\\&\n"
707 " cfd = socket(AF_INET, SOCK_STREAM, 0);\n"
708 " if (cfd == -1) {\n"
709 " perror(\"socket\");\n"
710 " return -1;\n"
711 " }\n"
712 "\\&\n"
713 " memset(&addr, 0, sizeof(addr));\n"
714 " addr.sin_port = htons(connect_port);\n"
715 " addr.sin_family = AF_INET;\n"
716 "\\&\n"
717 " if (!inet_aton(address, (struct in_addr *) &addr.sin_addr.s_addr)) {\n"
718 " fprintf(stderr, \"inet_aton(): bad IP address format\\[rs]n\");\n"
719 " close(cfd);\n"
720 " return -1;\n"
721 " }\n"
722 "\\&\n"
723 " if (connect(cfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n"
724 " perror(\"connect()\");\n"
725 " shutdown(cfd, SHUT_RDWR);\n"
726 " close(cfd);\n"
727 " return -1;\n"
728 " }\n"
729 " return cfd;\n"
730 "}\n"
731 "\\&\n"
732 "#define SHUT_FD1 do { \\[rs]\n"
733 " if (fd1 E<gt>= 0) { \\[rs]\n"
734 " shutdown(fd1, SHUT_RDWR); \\[rs]\n"
735 " close(fd1); \\[rs]\n"
736 " fd1 = -1; \\[rs]\n"
737 " } \\[rs]\n"
738 " } while (0)\n"
739 "\\&\n"
740 "#define SHUT_FD2 do { \\[rs]\n"
741 " if (fd2 E<gt>= 0) { \\[rs]\n"
742 " shutdown(fd2, SHUT_RDWR); \\[rs]\n"
743 " close(fd2); \\[rs]\n"
744 " fd2 = -1; \\[rs]\n"
745 " } \\[rs]\n"
746 " } while (0)\n"
747 "\\&\n"
748 "#define BUF_SIZE 1024\n"
749 "\\&\n"
750 "int\n"
751 "main(int argc, char *argv[])\n"
752 "{\n"
753 " int h;\n"
754 " int ready, nfds;\n"
755 " int fd1 = -1, fd2 = -1;\n"
756 " int buf1_avail = 0, buf1_written = 0;\n"
757 " int buf2_avail = 0, buf2_written = 0;\n"
758 " char buf1[BUF_SIZE], buf2[BUF_SIZE];\n"
759 " fd_set readfds, writefds, exceptfds;\n"
760 " ssize_t nbytes;\n"
761 "\\&\n"
762 " if (argc != 4) {\n"
763 " fprintf(stderr, \"Usage\\[rs]n\\[rs]tfwd E<lt>listen-portE<gt> \"\n"
764 " \"E<lt>forward-to-portE<gt> E<lt>forward-to-ip-addressE<gt>\\[rs]n\");\n"
765 " exit(EXIT_FAILURE);\n"
766 " }\n"
767 "\\&\n"
768 " signal(SIGPIPE, SIG_IGN);\n"
769 "\\&\n"
770 " forward_port = atoi(argv[2]);\n"
771 "\\&\n"
772 " h = listen_socket(atoi(argv[1]));\n"
773 " if (h == -1)\n"
774 " exit(EXIT_FAILURE);\n"
775 "\\&\n"
776 " for (;;) {\n"
777 " nfds = 0;\n"
778 "\\&\n"
779 " FD_ZERO(&readfds);\n"
780 " FD_ZERO(&writefds);\n"
781 " FD_ZERO(&exceptfds);\n"
782 " FD_SET(h, &readfds);\n"
783 " nfds = max(nfds, h);\n"
784 "\\&\n"
785 " if (fd1 E<gt> 0 && buf1_avail E<lt> BUF_SIZE)\n"
786 " FD_SET(fd1, &readfds);\n"
787 " /* Note: nfds is updated below, when fd1 is added to\n"
788 " exceptfds. */\n"
789 " if (fd2 E<gt> 0 && buf2_avail E<lt> BUF_SIZE)\n"
790 " FD_SET(fd2, &readfds);\n"
791 "\\&\n"
792 " if (fd1 E<gt> 0 && buf2_avail - buf2_written E<gt> 0)\n"
793 " FD_SET(fd1, &writefds);\n"
794 " if (fd2 E<gt> 0 && buf1_avail - buf1_written E<gt> 0)\n"
795 " FD_SET(fd2, &writefds);\n"
796 "\\&\n"
797 " if (fd1 E<gt> 0) {\n"
798 " FD_SET(fd1, &exceptfds);\n"
799 " nfds = max(nfds, fd1);\n"
800 " }\n"
801 " if (fd2 E<gt> 0) {\n"
802 " FD_SET(fd2, &exceptfds);\n"
803 " nfds = max(nfds, fd2);\n"
804 " }\n"
805 "\\&\n"
806 " ready = select(nfds + 1, &readfds, &writefds, &exceptfds, NULL);\n"
807 "\\&\n"
808 " if (ready == -1 && errno == EINTR)\n"
809 " continue;\n"
810 "\\&\n"
811 " if (ready == -1) {\n"
812 " perror(\"select()\");\n"
813 " exit(EXIT_FAILURE);\n"
814 " }\n"
815 "\\&\n"
816 " if (FD_ISSET(h, &readfds)) {\n"
817 " socklen_t addrlen;\n"
818 " struct sockaddr_in client_addr;\n"
819 " int fd;\n"
820 "\\&\n"
821 " addrlen = sizeof(client_addr);\n"
822 " memset(&client_addr, 0, addrlen);\n"
823 " fd = accept(h, (struct sockaddr *) &client_addr, &addrlen);\n"
824 " if (fd == -1) {\n"
825 " perror(\"accept()\");\n"
826 " } else {\n"
827 " SHUT_FD1;\n"
828 " SHUT_FD2;\n"
829 " buf1_avail = buf1_written = 0;\n"
830 " buf2_avail = buf2_written = 0;\n"
831 " fd1 = fd;\n"
832 " fd2 = connect_socket(forward_port, argv[3]);\n"
833 " if (fd2 == -1)\n"
834 " SHUT_FD1;\n"
835 " else\n"
836 " printf(\"connect from %s\\[rs]n\",\n"
837 " inet_ntoa(client_addr.sin_addr));\n"
838 "\\&\n"
839 " /* Skip any events on the old, closed file\n"
840 " descriptors. */\n"
841 "\\&\n"
842 " continue;\n"
843 " }\n"
844 " }\n"
845 "\\&\n"
846 " /* NB: read OOB data before normal reads. */\n"
847 "\\&\n"
848 " if (fd1 E<gt> 0 && FD_ISSET(fd1, &exceptfds)) {\n"
849 " char c;\n"
850 "\\&\n"
851 " nbytes = recv(fd1, &c, 1, MSG_OOB);\n"
852 " if (nbytes E<lt> 1)\n"
853 " SHUT_FD1;\n"
854 " else\n"
855 " send(fd2, &c, 1, MSG_OOB);\n"
856 " }\n"
857 " if (fd2 E<gt> 0 && FD_ISSET(fd2, &exceptfds)) {\n"
858 " char c;\n"
859 "\\&\n"
860 " nbytes = recv(fd2, &c, 1, MSG_OOB);\n"
861 " if (nbytes E<lt> 1)\n"
862 " SHUT_FD2;\n"
863 " else\n"
864 " send(fd1, &c, 1, MSG_OOB);\n"
865 " }\n"
866 " if (fd1 E<gt> 0 && FD_ISSET(fd1, &readfds)) {\n"
867 " nbytes = read(fd1, buf1 + buf1_avail,\n"
868 " BUF_SIZE - buf1_avail);\n"
869 " if (nbytes E<lt> 1)\n"
870 " SHUT_FD1;\n"
871 " else\n"
872 " buf1_avail += nbytes;\n"
873 " }\n"
874 " if (fd2 E<gt> 0 && FD_ISSET(fd2, &readfds)) {\n"
875 " nbytes = read(fd2, buf2 + buf2_avail,\n"
876 " BUF_SIZE - buf2_avail);\n"
877 " if (nbytes E<lt> 1)\n"
878 " SHUT_FD2;\n"
879 " else\n"
880 " buf2_avail += nbytes;\n"
881 " }\n"
882 " if (fd1 E<gt> 0 && FD_ISSET(fd1, &writefds) && buf2_avail E<gt> 0) {\n"
883 " nbytes = write(fd1, buf2 + buf2_written,\n"
884 " buf2_avail - buf2_written);\n"
885 " if (nbytes E<lt> 1)\n"
886 " SHUT_FD1;\n"
887 " else\n"
888 " buf2_written += nbytes;\n"
889 " }\n"
890 " if (fd2 E<gt> 0 && FD_ISSET(fd2, &writefds) && buf1_avail E<gt> 0) {\n"
891 " nbytes = write(fd2, buf1 + buf1_written,\n"
892 " buf1_avail - buf1_written);\n"
893 " if (nbytes E<lt> 1)\n"
894 " SHUT_FD2;\n"
895 " else\n"
896 " buf1_written += nbytes;\n"
897 " }\n"
898 "\\&\n"
899 " /* Check if write data has caught read data. */\n"
900 "\\&\n"
901 " if (buf1_written == buf1_avail)\n"
902 " buf1_written = buf1_avail = 0;\n"
903 " if (buf2_written == buf2_avail)\n"
904 " buf2_written = buf2_avail = 0;\n"
905 "\\&\n"
906 " /* One side has closed the connection, keep\n"
907 " writing to the other side until empty. */\n"
908 "\\&\n"
909 " if (fd1 E<lt> 0 && buf1_avail - buf1_written == 0)\n"
910 " SHUT_FD2;\n"
911 " if (fd2 E<lt> 0 && buf2_avail - buf2_written == 0)\n"
912 " SHUT_FD1;\n"
913 " }\n"
914 " exit(EXIT_SUCCESS);\n"
915 "}\n"
916 msgstr ""
917 "#include E<lt>arpa/inet.hE<gt>\n"
918 "#include E<lt>errno.hE<gt>\n"
919 "#include E<lt>netinet/in.hE<gt>\n"
920 "#include E<lt>signal.hE<gt>\n"
921 "#include E<lt>stdio.hE<gt>\n"
922 "#include E<lt>stdlib.hE<gt>\n"
923 "#include E<lt>string.hE<gt>\n"
924 "#include E<lt>sys/select.hE<gt>\n"
925 "#include E<lt>sys/socket.hE<gt>\n"
926 "#include E<lt>sys/types.hE<gt>\n"
927 "#include E<lt>unistd.hE<gt>\n"
928 "\\&\n"
929 "static int forward_port;\n"
930 "\\&\n"
931 "#undef max\n"
932 "#define max(x, y) ((x) E<gt> (y) ? (x) : (y))\n"
933 "\\&\n"
934 "static int\n"
935 "listen_socket(int listen_port)\n"
936 "{\n"
937 " int lfd;\n"
938 " int yes;\n"
939 " struct sockaddr_in addr;\n"
940 "\\&\n"
941 " lfd = socket(AF_INET, SOCK_STREAM, 0);\n"
942 " if (lfd == -1) {\n"
943 " perror(\"socket\");\n"
944 " return -1;\n"
945 " }\n"
946 "\\&\n"
947 " yes = 1;\n"
948 " if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR,\n"
949 " &yes, sizeof(yes)) == -1)\n"
950 " {\n"
951 " perror(\"setsockopt\");\n"
952 " close(lfd);\n"
953 " return -1;\n"
954 " }\n"
955 "\\&\n"
956 " memset(&addr, 0, sizeof(addr));\n"
957 " addr.sin_port = htons(listen_port);\n"
958 " addr.sin_family = AF_INET;\n"
959 " if (bind(lfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n"
960 " perror(\"bind\");\n"
961 " close(lfd);\n"
962 " return -1;\n"
963 " }\n"
964 "\\&\n"
965 " printf(\"accepting connections on port %d\\[rs]n\", listen_port);\n"
966 " listen(lfd, 10);\n"
967 " return lfd;\n"
968 "}\n"
969 "\\&\n"
970 "static int\n"
971 "connect_socket(int connect_port, char *address)\n"
972 "{\n"
973 " int cfd;\n"
974 " struct sockaddr_in addr;\n"
975 "\\&\n"
976 " cfd = socket(AF_INET, SOCK_STREAM, 0);\n"
977 " if (cfd == -1) {\n"
978 " perror(\"socket\");\n"
979 " return -1;\n"
980 " }\n"
981 "\\&\n"
982 " memset(&addr, 0, sizeof(addr));\n"
983 " addr.sin_port = htons(connect_port);\n"
984 " addr.sin_family = AF_INET;\n"
985 "\\&\n"
986 " if (!inet_aton(address, (struct in_addr *) &addr.sin_addr.s_addr)) {\n"
987 " fprintf(stderr, \"inet_aton(): bad IP address format\\[rs]n\");\n"
988 " close(cfd);\n"
989 " return -1;\n"
990 " }\n"
991 "\\&\n"
992 " if (connect(cfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n"
993 " perror(\"connect()\");\n"
994 " shutdown(cfd, SHUT_RDWR);\n"
995 " close(cfd);\n"
996 " return -1;\n"
997 " }\n"
998 " return cfd;\n"
999 "}\n"
1000 "\\&\n"
1001 "#define SHUT_FD1 do { \\[rs]\n"
1002 " if (fd1 E<gt>= 0) { \\[rs]\n"
1003 " shutdown(fd1, SHUT_RDWR); \\[rs]\n"
1004 " close(fd1); \\[rs]\n"
1005 " fd1 = -1; \\[rs]\n"
1006 " } \\[rs]\n"
1007 " } while (0)\n"
1008 "\\&\n"
1009 "#define SHUT_FD2 do { \\[rs]\n"
1010 " if (fd2 E<gt>= 0) { \\[rs]\n"
1011 " shutdown(fd2, SHUT_RDWR); \\[rs]\n"
1012 " close(fd2); \\[rs]\n"
1013 " fd2 = -1; \\[rs]\n"
1014 " } \\[rs]\n"
1015 " } while (0)\n"
1016 "\\&\n"
1017 "#define BUF_SIZE 1024\n"
1018 "\\&\n"
1019 "int\n"
1020 "main(int argc, char *argv[])\n"
1021 "{\n"
1022 " int h;\n"
1023 " int ready, nfds;\n"
1024 " int fd1 = -1, fd2 = -1;\n"
1025 " int buf1_avail = 0, buf1_written = 0;\n"
1026 " int buf2_avail = 0, buf2_written = 0;\n"
1027 " char buf1[BUF_SIZE], buf2[BUF_SIZE];\n"
1028 " fd_set readfds, writefds, exceptfds;\n"
1029 " ssize_t nbytes;\n"
1030 "\\&\n"
1031 " if (argc != 4) {\n"
1032 " fprintf(stderr, \"Usage\\[rs]n\\[rs]tfwd E<lt>listen-portE<gt> \"\n"
1033 " \"E<lt>forward-to-portE<gt> E<lt>forward-to-ip-addressE<gt>\\[rs]n\");\n"
1034 " exit(EXIT_FAILURE);\n"
1035 " }\n"
1036 "\\&\n"
1037 " signal(SIGPIPE, SIG_IGN);\n"
1038 "\\&\n"
1039 " forward_port = atoi(argv[2]);\n"
1040 "\\&\n"
1041 " h = listen_socket(atoi(argv[1]));\n"
1042 " if (h == -1)\n"
1043 " exit(EXIT_FAILURE);\n"
1044 "\\&\n"
1045 " for (;;) {\n"
1046 " nfds = 0;\n"
1047 "\\&\n"
1048 " FD_ZERO(&readfds);\n"
1049 " FD_ZERO(&writefds);\n"
1050 " FD_ZERO(&exceptfds);\n"
1051 " FD_SET(h, &readfds);\n"
1052 " nfds = max(nfds, h);\n"
1053 "\\&\n"
1054 " if (fd1 E<gt> 0 && buf1_avail E<lt> BUF_SIZE)\n"
1055 " FD_SET(fd1, &readfds);\n"
1056 " /* Note: nfds is updated below, when fd1 is added to\n"
1057 " exceptfds. */\n"
1058 " if (fd2 E<gt> 0 && buf2_avail E<lt> BUF_SIZE)\n"
1059 " FD_SET(fd2, &readfds);\n"
1060 "\\&\n"
1061 " if (fd1 E<gt> 0 && buf2_avail - buf2_written E<gt> 0)\n"
1062 " FD_SET(fd1, &writefds);\n"
1063 " if (fd2 E<gt> 0 && buf1_avail - buf1_written E<gt> 0)\n"
1064 " FD_SET(fd2, &writefds);\n"
1065 "\\&\n"
1066 " if (fd1 E<gt> 0) {\n"
1067 " FD_SET(fd1, &exceptfds);\n"
1068 " nfds = max(nfds, fd1);\n"
1069 " }\n"
1070 " if (fd2 E<gt> 0) {\n"
1071 " FD_SET(fd2, &exceptfds);\n"
1072 " nfds = max(nfds, fd2);\n"
1073 " }\n"
1074 "\\&\n"
1075 " ready = select(nfds + 1, &readfds, &writefds, &exceptfds, NULL);\n"
1076 "\\&\n"
1077 " if (ready == -1 && errno == EINTR)\n"
1078 " continue;\n"
1079 "\\&\n"
1080 " if (ready == -1) {\n"
1081 " perror(\"select()\");\n"
1082 " exit(EXIT_FAILURE);\n"
1083 " }\n"
1084 "\\&\n"
1085 " if (FD_ISSET(h, &readfds)) {\n"
1086 " socklen_t addrlen;\n"
1087 " struct sockaddr_in client_addr;\n"
1088 " int fd;\n"
1089 "\\&\n"
1090 " addrlen = sizeof(client_addr);\n"
1091 " memset(&client_addr, 0, addrlen);\n"
1092 " fd = accept(h, (struct sockaddr *) &client_addr, &addrlen);\n"
1093 " if (fd == -1) {\n"
1094 " perror(\"accept()\");\n"
1095 " } else {\n"
1096 " SHUT_FD1;\n"
1097 " SHUT_FD2;\n"
1098 " buf1_avail = buf1_written = 0;\n"
1099 " buf2_avail = buf2_written = 0;\n"
1100 " fd1 = fd;\n"
1101 " fd2 = connect_socket(forward_port, argv[3]);\n"
1102 " if (fd2 == -1)\n"
1103 " SHUT_FD1;\n"
1104 " else\n"
1105 " printf(\"connect from %s\\[rs]n\",\n"
1106 " inet_ntoa(client_addr.sin_addr));\n"
1107 "\\&\n"
1108 " /* Skip any events on the old, closed file\n"
1109 " descriptors. */\n"
1110 "\\&\n"
1111 " continue;\n"
1112 " }\n"
1113 " }\n"
1114 "\\&\n"
1115 " /* NB: read OOB data before normal reads. */\n"
1116 "\\&\n"
1117 " if (fd1 E<gt> 0 && FD_ISSET(fd1, &exceptfds)) {\n"
1118 " char c;\n"
1119 "\\&\n"
1120 " nbytes = recv(fd1, &c, 1, MSG_OOB);\n"
1121 " if (nbytes E<lt> 1)\n"
1122 " SHUT_FD1;\n"
1123 " else\n"
1124 " send(fd2, &c, 1, MSG_OOB);\n"
1125 " }\n"
1126 " if (fd2 E<gt> 0 && FD_ISSET(fd2, &exceptfds)) {\n"
1127 " char c;\n"
1128 "\\&\n"
1129 " nbytes = recv(fd2, &c, 1, MSG_OOB);\n"
1130 " if (nbytes E<lt> 1)\n"
1131 " SHUT_FD2;\n"
1132 " else\n"
1133 " send(fd1, &c, 1, MSG_OOB);\n"
1134 " }\n"
1135 " if (fd1 E<gt> 0 && FD_ISSET(fd1, &readfds)) {\n"
1136 " nbytes = read(fd1, buf1 + buf1_avail,\n"
1137 " BUF_SIZE - buf1_avail);\n"
1138 " if (nbytes E<lt> 1)\n"
1139 " SHUT_FD1;\n"
1140 " else\n"
1141 " buf1_avail += nbytes;\n"
1142 " }\n"
1143 " if (fd2 E<gt> 0 && FD_ISSET(fd2, &readfds)) {\n"
1144 " nbytes = read(fd2, buf2 + buf2_avail,\n"
1145 " BUF_SIZE - buf2_avail);\n"
1146 " if (nbytes E<lt> 1)\n"
1147 " SHUT_FD2;\n"
1148 " else\n"
1149 " buf2_avail += nbytes;\n"
1150 " }\n"
1151 " if (fd1 E<gt> 0 && FD_ISSET(fd1, &writefds) && buf2_avail E<gt> 0) {\n"
1152 " nbytes = write(fd1, buf2 + buf2_written,\n"
1153 " buf2_avail - buf2_written);\n"
1154 " if (nbytes E<lt> 1)\n"
1155 " SHUT_FD1;\n"
1156 " else\n"
1157 " buf2_written += nbytes;\n"
1158 " }\n"
1159 " if (fd2 E<gt> 0 && FD_ISSET(fd2, &writefds) && buf1_avail E<gt> 0) {\n"
1160 " nbytes = write(fd2, buf1 + buf1_written,\n"
1161 " buf1_avail - buf1_written);\n"
1162 " if (nbytes E<lt> 1)\n"
1163 " SHUT_FD2;\n"
1164 " else\n"
1165 " buf1_written += nbytes;\n"
1166 " }\n"
1167 "\\&\n"
1168 " /* Check if write data has caught read data. */\n"
1169 "\\&\n"
1170 " if (buf1_written == buf1_avail)\n"
1171 " buf1_written = buf1_avail = 0;\n"
1172 " if (buf2_written == buf2_avail)\n"
1173 " buf2_written = buf2_avail = 0;\n"
1174 "\\&\n"
1175 " /* One side has closed the connection, keep\n"
1176 " writing to the other side until empty. */\n"
1177 "\\&\n"
1178 " if (fd1 E<lt> 0 && buf1_avail - buf1_written == 0)\n"
1179 " SHUT_FD2;\n"
1180 " if (fd2 E<lt> 0 && buf2_avail - buf2_written == 0)\n"
1181 " SHUT_FD1;\n"
1182 " }\n"
1183 " exit(EXIT_SUCCESS);\n"
1184 "}\n"
1185
1186 #. type: Plain text
1187 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
1188 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
1189 msgid ""
1190 "The above program properly forwards most kinds of TCP connections including "
1191 "OOB signal data transmitted by B<telnet> servers. It handles the tricky "
1192 "problem of having data flow in both directions simultaneously. You might "
1193 "think it more efficient to use a B<fork>(2) call and devote a thread to "
1194 "each stream. This becomes more tricky than you might suspect. Another idea "
1195 "is to set nonblocking I/O using B<fcntl>(2). This also has its problems "
1196 "because you end up using inefficient timeouts."
1197 msgstr ""
1198 "يقوم البرنامج أعلاه بتوجيه معظم أنواع اتصالات TCP بشكل صحيح بما في ذلك "
1199 "بيانات الإشارة خارج النطاق (OOB) التي ترسلها خوادم B<telnet>. يعالج المشكلة "
1200 "الصعبة المتمثلة في تدفق البيانات في كلا الاتجاهين في وقت واحد. قد تظن أنه من "
1201 "الأكثر كفاءة استخدام استدعاء B<fork>(2) وتخصيص خيط لكل تيار. يصبح هذا أكثر "
1202 "تعقيدًا مما قد تظن. فكرة أخرى هي ضبط الإدخال/الإخراج غير المحظور باستخدام "
1203 "B<fcntl>(2). هذا أيضًا له مشاكله لأنك ينتهي بك الأمر باستخدام مهلات غير فعالة."
1204
1205 #. type: Plain text
1206 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
1207 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
1208 msgid ""
1209 "The program does not handle more than one simultaneous connection at a time, "
1210 "although it could easily be extended to do this with a linked list of "
1211 "buffers\\[em]one for each connection. At the moment, new connections cause "
1212 "the current connection to be dropped."
1213 msgstr ""
1214 "لا يعالج البرنامج أكثر من اتصال واحد في وقت واحد، على الرغم من أنه يمكن "
1215 "توسيعه بسهولة للقيام بذلك باستخدام قائمة مرتبطة من المخازن المؤقتة\\[em]واحد "
1216 "لكل اتصال. في الوقت الحالي، تتسبب الاتصالات الجديدة في قطع الاتصال الحالي."
1217
1218 #. type: SH
1219 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
1220 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
1221 #, no-wrap
1222 msgid "SEE ALSO"
1223 msgstr "انظر أيضًا"
1224
1225 #. type: Plain text
1226 #: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide
1227 #: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed
1228 msgid ""
1229 "B<accept>(2), B<connect>(2), B<poll>(2), B<read>(2), B<recv>(2), "
1230 "B<select>(2), B<send>(2), B<sigprocmask>(2), B<write>(2), B<epoll>(7)"
1231 msgstr ""
1232 "B<accept>(2), B<connect>(2), B<poll>(2), B<read>(2), B<recv>(2), "
1233 "B<select>(2), B<send>(2), B<sigprocmask>(2), B<write>(2), B<epoll>(7)"
1234
1235 #. type: TH
1236 #: debian-trixie
1237 #, no-wrap
1238 msgid "2024-06-15"
1239 msgstr "15 يونيو 2024"
1240
1241 #. type: TH
1242 #: debian-trixie
1243 #, no-wrap
1244 msgid "Linux man-pages 6.9.1"
1245 msgstr "صفحات دليل لينكس 6.9.1"
1246
1247 #. type: Plain text
1248 #: debian-trixie
1249 msgid "Standard C library (I<libc>, I<-lc>)"
1250 msgstr "مكتبة سي المعيارية (I<libc>، I<-lc>)"
1251
1252 #. type: Plain text
1253 #: debian-trixie fedora-44 fedora-rawhide
1254 #, no-wrap
1255 msgid ""
1256 "static volatile sig_atomic_t got_SIGCHLD = 0;\n"
1257 "\\&\n"
1258 "static void\n"
1259 "child_sig_handler(int sig)\n"
1260 "{\n"
1261 " got_SIGCHLD = 1;\n"
1262 "}\n"
1263 "\\&\n"
1264 "int\n"
1265 "main(int argc, char *argv[])\n"
1266 "{\n"
1267 " sigset_t sigmask, empty_mask;\n"
1268 " struct sigaction sa;\n"
1269 " fd_set readfds, writefds, exceptfds;\n"
1270 " int r;\n"
1271 "\\&\n"
1272 " sigemptyset(&sigmask);\n"
1273 " sigaddset(&sigmask, SIGCHLD);\n"
1274 " if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) {\n"
1275 " perror(\"sigprocmask\");\n"
1276 " exit(EXIT_FAILURE);\n"
1277 " }\n"
1278 "\\&\n"
1279 " sa.sa_flags = 0;\n"
1280 " sa.sa_handler = child_sig_handler;\n"
1281 " sigemptyset(&sa.sa_mask);\n"
1282 " if (sigaction(SIGCHLD, &sa, NULL) == -1) {\n"
1283 " perror(\"sigaction\");\n"
1284 " exit(EXIT_FAILURE);\n"
1285 " }\n"
1286 "\\&\n"
1287 " sigemptyset(&empty_mask);\n"
1288 "\\&\n"
1289 " for (;;) { /* main loop */\n"
1290 " /* Initialize readfds, writefds, and exceptfds\n"
1291 " before the pselect() call. (Code omitted.) */\n"
1292 "\\&\n"
1293 " r = pselect(nfds, &readfds, &writefds, &exceptfds,\n"
1294 " NULL, &empty_mask);\n"
1295 " if (r == -1 && errno != EINTR) {\n"
1296 " /* Handle error */\n"
1297 " }\n"
1298 "\\&\n"
1299 " if (got_SIGCHLD) {\n"
1300 " got_SIGCHLD = 0;\n"
1301 "\\&\n"
1302 " /* Handle signalled event here; e.g., wait() for all\n"
1303 " terminated children. (Code omitted.) */\n"
1304 " }\n"
1305 "\\&\n"
1306 " /* main body of program */\n"
1307 " }\n"
1308 "}\n"
1309 msgstr ""
1310 "static volatile sig_atomic_t got_SIGCHLD = 0;\n"
1311 "\\&\n"
1312 "static void\n"
1313 "child_sig_handler(int sig)\n"
1314 "{\n"
1315 " got_SIGCHLD = 1;\n"
1316 "}\n"
1317 "\\&\n"
1318 "int\n"
1319 "main(int argc, char *argv[])\n"
1320 "{\n"
1321 " sigset_t sigmask, empty_mask;\n"
1322 " struct sigaction sa;\n"
1323 " fd_set readfds, writefds, exceptfds;\n"
1324 " int r;\n"
1325 "\\&\n"
1326 " sigemptyset(&sigmask);\n"
1327 " sigaddset(&sigmask, SIGCHLD);\n"
1328 " if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) {\n"
1329 " perror(\"sigprocmask\");\n"
1330 " exit(EXIT_FAILURE);\n"
1331 " }\n"
1332 "\\&\n"
1333 " sa.sa_flags = 0;\n"
1334 " sa.sa_handler = child_sig_handler;\n"
1335 " sigemptyset(&sa.sa_mask);\n"
1336 " if (sigaction(SIGCHLD, &sa, NULL) == -1) {\n"
1337 " perror(\"sigaction\");\n"
1338 " exit(EXIT_FAILURE);\n"
1339 " }\n"
1340 "\\&\n"
1341 " sigemptyset(&empty_mask);\n"
1342 "\\&\n"
1343 " for (;;) { /* الحلقة الرئيسية */\n"
1344 " /* تهيئة readfds و writefds و exceptfds\n"
1345 " قبل استدعاء pselect(). (تم حذف الكود.) */\n"
1346 "\\&\n"
1347 " r = pselect(nfds, &readfds, &writefds, &exceptfds,\n"
1348 " NULL, &empty_mask);\n"
1349 " if (r == -1 && errno != EINTR) {\n"
1350 " /* معالجة الخطأ */\n"
1351 " }\n"
1352 "\\&\n"
1353 " if (got_SIGCHLD) {\n"
1354 " got_SIGCHLD = 0;\n"
1355 "\\&\n"
1356 " /* معالجة الحدث المُشار إليه هنا؛ على سبيل المثال، wait() لجميع\n"
1357 " العمليات الفرعية المنتهية. (تم حذف الكود.) */\n"
1358 " }\n"
1359 "\\&\n"
1360 " /* الجزء الرئيسي من البرنامج */\n"
1361 " }\n"
1362 "}\n"
1363
1364 #. type: Plain text
1365 #: debian-trixie fedora-44 fedora-rawhide
1366 #, no-wrap
1367 msgid ""
1368 "#include E<lt>arpa/inet.hE<gt>\n"
1369 "#include E<lt>errno.hE<gt>\n"
1370 "#include E<lt>netinet/in.hE<gt>\n"
1371 "#include E<lt>signal.hE<gt>\n"
1372 "#include E<lt>stdio.hE<gt>\n"
1373 "#include E<lt>stdlib.hE<gt>\n"
1374 "#include E<lt>string.hE<gt>\n"
1375 "#include E<lt>sys/select.hE<gt>\n"
1376 "#include E<lt>sys/socket.hE<gt>\n"
1377 "#include E<lt>sys/types.hE<gt>\n"
1378 "#include E<lt>unistd.hE<gt>\n"
1379 "\\&\n"
1380 "static int forward_port;\n"
1381 "\\&\n"
1382 "#undef max\n"
1383 "#define max(x, y) ((x) E<gt> (y) ? (x) : (y))\n"
1384 "\\&\n"
1385 "static int\n"
1386 "listen_socket(int listen_port)\n"
1387 "{\n"
1388 " int lfd;\n"
1389 " int yes;\n"
1390 " struct sockaddr_in addr;\n"
1391 "\\&\n"
1392 " lfd = socket(AF_INET, SOCK_STREAM, 0);\n"
1393 " if (lfd == -1) {\n"
1394 " perror(\"socket\");\n"
1395 " return -1;\n"
1396 " }\n"
1397 "\\&\n"
1398 " yes = 1;\n"
1399 " if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR,\n"
1400 " &yes, sizeof(yes)) == -1)\n"
1401 " {\n"
1402 " perror(\"setsockopt\");\n"
1403 " close(lfd);\n"
1404 " return -1;\n"
1405 " }\n"
1406 "\\&\n"
1407 " memset(&addr, 0, sizeof(addr));\n"
1408 " addr.sin_port = htons(listen_port);\n"
1409 " addr.sin_family = AF_INET;\n"
1410 " if (bind(lfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n"
1411 " perror(\"bind\");\n"
1412 " close(lfd);\n"
1413 " return -1;\n"
1414 " }\n"
1415 "\\&\n"
1416 " printf(\"accepting connections on port %d\\[rs]n\", listen_port);\n"
1417 " listen(lfd, 10);\n"
1418 " return lfd;\n"
1419 "}\n"
1420 "\\&\n"
1421 "static int\n"
1422 "connect_socket(int connect_port, char *address)\n"
1423 "{\n"
1424 " int cfd;\n"
1425 " struct sockaddr_in addr;\n"
1426 "\\&\n"
1427 " cfd = socket(AF_INET, SOCK_STREAM, 0);\n"
1428 " if (cfd == -1) {\n"
1429 " perror(\"socket\");\n"
1430 " return -1;\n"
1431 " }\n"
1432 "\\&\n"
1433 " memset(&addr, 0, sizeof(addr));\n"
1434 " addr.sin_port = htons(connect_port);\n"
1435 " addr.sin_family = AF_INET;\n"
1436 "\\&\n"
1437 " if (!inet_aton(address, (struct in_addr *) &addr.sin_addr.s_addr)) {\n"
1438 " fprintf(stderr, \"inet_aton(): bad IP address format\\[rs]n\");\n"
1439 " close(cfd);\n"
1440 " return -1;\n"
1441 " }\n"
1442 "\\&\n"
1443 " if (connect(cfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n"
1444 " perror(\"connect()\");\n"
1445 " shutdown(cfd, SHUT_RDWR);\n"
1446 " close(cfd);\n"
1447 " return -1;\n"
1448 " }\n"
1449 " return cfd;\n"
1450 "}\n"
1451 "\\&\n"
1452 "#define SHUT_FD1 do { \\[rs]\n"
1453 " if (fd1 E<gt>= 0) { \\[rs]\n"
1454 " shutdown(fd1, SHUT_RDWR); \\[rs]\n"
1455 " close(fd1); \\[rs]\n"
1456 " fd1 = -1; \\[rs]\n"
1457 " } \\[rs]\n"
1458 " } while (0)\n"
1459 "\\&\n"
1460 "#define SHUT_FD2 do { \\[rs]\n"
1461 " if (fd2 E<gt>= 0) { \\[rs]\n"
1462 " shutdown(fd2, SHUT_RDWR); \\[rs]\n"
1463 " close(fd2); \\[rs]\n"
1464 " fd2 = -1; \\[rs]\n"
1465 " } \\[rs]\n"
1466 " } while (0)\n"
1467 "\\&\n"
1468 "#define BUF_SIZE 1024\n"
1469 "\\&\n"
1470 "int\n"
1471 "main(int argc, char *argv[])\n"
1472 "{\n"
1473 " int h;\n"
1474 " int ready, nfds;\n"
1475 " int fd1 = -1, fd2 = -1;\n"
1476 " int buf1_avail = 0, buf1_written = 0;\n"
1477 " int buf2_avail = 0, buf2_written = 0;\n"
1478 " char buf1[BUF_SIZE], buf2[BUF_SIZE];\n"
1479 " fd_set readfds, writefds, exceptfds;\n"
1480 " ssize_t nbytes;\n"
1481 "\\&\n"
1482 " if (argc != 4) {\n"
1483 " fprintf(stderr, \"Usage\\[rs]n\\[rs]tfwd E<lt>listen-portE<gt> \"\n"
1484 " \"E<lt>forward-to-portE<gt> E<lt>forward-to-ip-addressE<gt>\\[rs]n\");\n"
1485 " exit(EXIT_FAILURE);\n"
1486 " }\n"
1487 "\\&\n"
1488 " signal(SIGPIPE, SIG_IGN);\n"
1489 "\\&\n"
1490 " forward_port = atoi(argv[2]);\n"
1491 "\\&\n"
1492 " h = listen_socket(atoi(argv[1]));\n"
1493 " if (h == -1)\n"
1494 " exit(EXIT_FAILURE);\n"
1495 "\\&\n"
1496 " for (;;) {\n"
1497 " nfds = 0;\n"
1498 "\\&\n"
1499 " FD_ZERO(&readfds);\n"
1500 " FD_ZERO(&writefds);\n"
1501 " FD_ZERO(&exceptfds);\n"
1502 " FD_SET(h, &readfds);\n"
1503 " nfds = max(nfds, h);\n"
1504 "\\&\n"
1505 " if (fd1 E<gt> 0 && buf1_avail E<lt> BUF_SIZE)\n"
1506 " FD_SET(fd1, &readfds);\n"
1507 " /* Note: nfds is updated below, when fd1 is added to\n"
1508 " exceptfds. */\n"
1509 " if (fd2 E<gt> 0 && buf2_avail E<lt> BUF_SIZE)\n"
1510 " FD_SET(fd2, &readfds);\n"
1511 "\\&\n"
1512 " if (fd1 E<gt> 0 && buf2_avail - buf2_written E<gt> 0)\n"
1513 " FD_SET(fd1, &writefds);\n"
1514 " if (fd2 E<gt> 0 && buf1_avail - buf1_written E<gt> 0)\n"
1515 " FD_SET(fd2, &writefds);\n"
1516 "\\&\n"
1517 " if (fd1 E<gt> 0) {\n"
1518 " FD_SET(fd1, &exceptfds);\n"
1519 " nfds = max(nfds, fd1);\n"
1520 " }\n"
1521 " if (fd2 E<gt> 0) {\n"
1522 " FD_SET(fd2, &exceptfds);\n"
1523 " nfds = max(nfds, fd2);\n"
1524 " }\n"
1525 "\\&\n"
1526 " ready = select(nfds + 1, &readfds, &writefds, &exceptfds, NULL);\n"
1527 "\\&\n"
1528 " if (ready == -1 && errno == EINTR)\n"
1529 " continue;\n"
1530 "\\&\n"
1531 " if (ready == -1) {\n"
1532 " perror(\"select()\");\n"
1533 " exit(EXIT_FAILURE);\n"
1534 " }\n"
1535 "\\&\n"
1536 " if (FD_ISSET(h, &readfds)) {\n"
1537 " socklen_t addrlen;\n"
1538 " struct sockaddr_in client_addr;\n"
1539 " int fd;\n"
1540 "\\&\n"
1541 " addrlen = sizeof(client_addr);\n"
1542 " memset(&client_addr, 0, addrlen);\n"
1543 " fd = accept(h, (struct sockaddr *) &client_addr, &addrlen);\n"
1544 " if (fd == -1) {\n"
1545 " perror(\"accept()\");\n"
1546 " } else {\n"
1547 " SHUT_FD1;\n"
1548 " SHUT_FD2;\n"
1549 " buf1_avail = buf1_written = 0;\n"
1550 " buf2_avail = buf2_written = 0;\n"
1551 " fd1 = fd;\n"
1552 " fd2 = connect_socket(forward_port, argv[3]);\n"
1553 " if (fd2 == -1)\n"
1554 " SHUT_FD1;\n"
1555 " else\n"
1556 " printf(\"connect from %s\\[rs]n\",\n"
1557 " inet_ntoa(client_addr.sin_addr));\n"
1558 "\\&\n"
1559 " /* Skip any events on the old, closed file\n"
1560 " descriptors. */\n"
1561 "\\&\n"
1562 " continue;\n"
1563 " }\n"
1564 " }\n"
1565 "\\&\n"
1566 " /* NB: read OOB data before normal reads. */\n"
1567 "\\&\n"
1568 " if (fd1 E<gt> 0 && FD_ISSET(fd1, &exceptfds)) {\n"
1569 " char c;\n"
1570 "\\&\n"
1571 " nbytes = recv(fd1, &c, 1, MSG_OOB);\n"
1572 " if (nbytes E<lt> 1)\n"
1573 " SHUT_FD1;\n"
1574 " else\n"
1575 " send(fd2, &c, 1, MSG_OOB);\n"
1576 " }\n"
1577 " if (fd2 E<gt> 0 && FD_ISSET(fd2, &exceptfds)) {\n"
1578 " char c;\n"
1579 "\\&\n"
1580 " nbytes = recv(fd2, &c, 1, MSG_OOB);\n"
1581 " if (nbytes E<lt> 1)\n"
1582 " SHUT_FD2;\n"
1583 " else\n"
1584 " send(fd1, &c, 1, MSG_OOB);\n"
1585 " }\n"
1586 " if (fd1 E<gt> 0 && FD_ISSET(fd1, &readfds)) {\n"
1587 " nbytes = read(fd1, buf1 + buf1_avail,\n"
1588 " BUF_SIZE - buf1_avail);\n"
1589 " if (nbytes E<lt> 1)\n"
1590 " SHUT_FD1;\n"
1591 " else\n"
1592 " buf1_avail += nbytes;\n"
1593 " }\n"
1594 " if (fd2 E<gt> 0 && FD_ISSET(fd2, &readfds)) {\n"
1595 " nbytes = read(fd2, buf2 + buf2_avail,\n"
1596 " BUF_SIZE - buf2_avail);\n"
1597 " if (nbytes E<lt> 1)\n"
1598 " SHUT_FD2;\n"
1599 " else\n"
1600 " buf2_avail += nbytes;\n"
1601 " }\n"
1602 " if (fd1 E<gt> 0 && FD_ISSET(fd1, &writefds) && buf2_avail E<gt> 0) {\n"
1603 " nbytes = write(fd1, buf2 + buf2_written,\n"
1604 " buf2_avail - buf2_written);\n"
1605 " if (nbytes E<lt> 1)\n"
1606 " SHUT_FD1;\n"
1607 " else\n"
1608 " buf2_written += nbytes;\n"
1609 " }\n"
1610 " if (fd2 E<gt> 0 && FD_ISSET(fd2, &writefds) && buf1_avail E<gt> 0) {\n"
1611 " nbytes = write(fd2, buf1 + buf1_written,\n"
1612 " buf1_avail - buf1_written);\n"
1613 " if (nbytes E<lt> 1)\n"
1614 " SHUT_FD2;\n"
1615 " else\n"
1616 " buf1_written += nbytes;\n"
1617 " }\n"
1618 "\\&\n"
1619 " /* Check if write data has caught read data. */\n"
1620 "\\&\n"
1621 " if (buf1_written == buf1_avail)\n"
1622 " buf1_written = buf1_avail = 0;\n"
1623 " if (buf2_written == buf2_avail)\n"
1624 " buf2_written = buf2_avail = 0;\n"
1625 "\\&\n"
1626 " /* One side has closed the connection, keep\n"
1627 " writing to the other side until empty. */\n"
1628 "\\&\n"
1629 " if (fd1 E<lt> 0 && buf1_avail - buf1_written == 0)\n"
1630 " SHUT_FD2;\n"
1631 " if (fd2 E<lt> 0 && buf2_avail - buf2_written == 0)\n"
1632 " SHUT_FD1;\n"
1633 " }\n"
1634 " exit(EXIT_SUCCESS);\n"
1635 "}\n"
1636 msgstr ""
1637 "#include E<lt>arpa/inet.hE<gt>\n"
1638 "#include E<lt>errno.hE<gt>\n"
1639 "#include E<lt>netinet/in.hE<gt>\n"
1640 "#include E<lt>signal.hE<gt>\n"
1641 "#include E<lt>stdio.hE<gt>\n"
1642 "#include E<lt>stdlib.hE<gt>\n"
1643 "#include E<lt>string.hE<gt>\n"
1644 "#include E<lt>sys/select.hE<gt>\n"
1645 "#include E<lt>sys/socket.hE<gt>\n"
1646 "#include E<lt>sys/types.hE<gt>\n"
1647 "#include E<lt>unistd.hE<gt>\n"
1648 "\\&\n"
1649 "static int forward_port;\n"
1650 "\\&\n"
1651 "#undef max\n"
1652 "#define max(x, y) ((x) E<gt> (y) ? (x) : (y))\n"
1653 "\\&\n"
1654 "static int\n"
1655 "listen_socket(int listen_port)\n"
1656 "{\n"
1657 " int lfd;\n"
1658 " int yes;\n"
1659 " struct sockaddr_in addr;\n"
1660 "\\&\n"
1661 " lfd = socket(AF_INET, SOCK_STREAM, 0);\n"
1662 " if (lfd == -1) {\n"
1663 " perror(\"socket\");\n"
1664 " return -1;\n"
1665 " }\n"
1666 "\\&\n"
1667 " yes = 1;\n"
1668 " if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR,\n"
1669 " &yes, sizeof(yes)) == -1)\n"
1670 " {\n"
1671 " perror(\"setsockopt\");\n"
1672 " close(lfd);\n"
1673 " return -1;\n"
1674 " }\n"
1675 "\\&\n"
1676 " memset(&addr, 0, sizeof(addr));\n"
1677 " addr.sin_port = htons(listen_port);\n"
1678 " addr.sin_family = AF_INET;\n"
1679 " if (bind(lfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n"
1680 " perror(\"bind\");\n"
1681 " close(lfd);\n"
1682 " return -1;\n"
1683 " }\n"
1684 "\\&\n"
1685 " printf(\"accepting connections on port %d\\[rs]n\", listen_port);\n"
1686 " listen(lfd, 10);\n"
1687 " return lfd;\n"
1688 "}\n"
1689 "\\&\n"
1690 "static int\n"
1691 "connect_socket(int connect_port, char *address)\n"
1692 "{\n"
1693 " int cfd;\n"
1694 " struct sockaddr_in addr;\n"
1695 "\\&\n"
1696 " cfd = socket(AF_INET, SOCK_STREAM, 0);\n"
1697 " if (cfd == -1) {\n"
1698 " perror(\"socket\");\n"
1699 " return -1;\n"
1700 " }\n"
1701 "\\&\n"
1702 " memset(&addr, 0, sizeof(addr));\n"
1703 " addr.sin_port = htons(connect_port);\n"
1704 " addr.sin_family = AF_INET;\n"
1705 "\\&\n"
1706 " if (!inet_aton(address, (struct in_addr *) &addr.sin_addr.s_addr)) {\n"
1707 " fprintf(stderr, \"inet_aton(): bad IP address format\\[rs]n\");\n"
1708 " close(cfd);\n"
1709 " return -1;\n"
1710 " }\n"
1711 "\\&\n"
1712 " if (connect(cfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n"
1713 " perror(\"connect()\");\n"
1714 " shutdown(cfd, SHUT_RDWR);\n"
1715 " close(cfd);\n"
1716 " return -1;\n"
1717 " }\n"
1718 " return cfd;\n"
1719 "}\n"
1720 "\\&\n"
1721 "#define SHUT_FD1 do { \\[rs]\n"
1722 " if (fd1 E<gt>= 0) { \\[rs]\n"
1723 " shutdown(fd1, SHUT_RDWR); \\[rs]\n"
1724 " close(fd1); \\[rs]\n"
1725 " fd1 = -1; \\[rs]\n"
1726 " } \\[rs]\n"
1727 " } while (0)\n"
1728 "\\&\n"
1729 "#define SHUT_FD2 do { \\[rs]\n"
1730 " if (fd2 E<gt>= 0) { \\[rs]\n"
1731 " shutdown(fd2, SHUT_RDWR); \\[rs]\n"
1732 " close(fd2); \\[rs]\n"
1733 " fd2 = -1; \\[rs]\n"
1734 " } \\[rs]\n"
1735 " } while (0)\n"
1736 "\\&\n"
1737 "#define BUF_SIZE 1024\n"
1738 "\\&\n"
1739 "int\n"
1740 "main(int argc, char *argv[])\n"
1741 "{\n"
1742 " int h;\n"
1743 " int ready, nfds;\n"
1744 " int fd1 = -1, fd2 = -1;\n"
1745 " int buf1_avail = 0, buf1_written = 0;\n"
1746 " int buf2_avail = 0, buf2_written = 0;\n"
1747 " char buf1[BUF_SIZE], buf2[BUF_SIZE];\n"
1748 " fd_set readfds, writefds, exceptfds;\n"
1749 " ssize_t nbytes;\n"
1750 "\\&\n"
1751 " if (argc != 4) {\n"
1752 " fprintf(stderr, \"Usage\\[rs]n\\[rs]tfwd E<lt>listen-portE<gt> \"\n"
1753 " \"E<lt>forward-to-portE<gt> E<lt>forward-to-ip-addressE<gt>\\[rs]n\");\n"
1754 " exit(EXIT_FAILURE);\n"
1755 " }\n"
1756 "\\&\n"
1757 " signal(SIGPIPE, SIG_IGN);\n"
1758 "\\&\n"
1759 " forward_port = atoi(argv[2]);\n"
1760 "\\&\n"
1761 " h = listen_socket(atoi(argv[1]));\n"
1762 " if (h == -1)\n"
1763 " exit(EXIT_FAILURE);\n"
1764 "\\&\n"
1765 " for (;;) {\n"
1766 " nfds = 0;\n"
1767 "\\&\n"
1768 " FD_ZERO(&readfds);\n"
1769 " FD_ZERO(&writefds);\n"
1770 " FD_ZERO(&exceptfds);\n"
1771 " FD_SET(h, &readfds);\n"
1772 " nfds = max(nfds, h);\n"
1773 "\\&\n"
1774 " if (fd1 E<gt> 0 && buf1_avail E<lt> BUF_SIZE)\n"
1775 " FD_SET(fd1, &readfds);\n"
1776 " /* Note: nfds is updated below, when fd1 is added to\n"
1777 " exceptfds. */\n"
1778 " if (fd2 E<gt> 0 && buf2_avail E<lt> BUF_SIZE)\n"
1779 " FD_SET(fd2, &readfds);\n"
1780 "\\&\n"
1781 " if (fd1 E<gt> 0 && buf2_avail - buf2_written E<gt> 0)\n"
1782 " FD_SET(fd1, &writefds);\n"
1783 " if (fd2 E<gt> 0 && buf1_avail - buf1_written E<gt> 0)\n"
1784 " FD_SET(fd2, &writefds);\n"
1785 "\\&\n"
1786 " if (fd1 E<gt> 0) {\n"
1787 " FD_SET(fd1, &exceptfds);\n"
1788 " nfds = max(nfds, fd1);\n"
1789 " }\n"
1790 " if (fd2 E<gt> 0) {\n"
1791 " FD_SET(fd2, &exceptfds);\n"
1792 " nfds = max(nfds, fd2);\n"
1793 " }\n"
1794 "\\&\n"
1795 " ready = select(nfds + 1, &readfds, &writefds, &exceptfds, NULL);\n"
1796 "\\&\n"
1797 " if (ready == -1 && errno == EINTR)\n"
1798 " continue;\n"
1799 "\\&\n"
1800 " if (ready == -1) {\n"
1801 " perror(\"select()\");\n"
1802 " exit(EXIT_FAILURE);\n"
1803 " }\n"
1804 "\\&\n"
1805 " if (FD_ISSET(h, &readfds)) {\n"
1806 " socklen_t addrlen;\n"
1807 " struct sockaddr_in client_addr;\n"
1808 " int fd;\n"
1809 "\\&\n"
1810 " addrlen = sizeof(client_addr);\n"
1811 " memset(&client_addr, 0, addrlen);\n"
1812 " fd = accept(h, (struct sockaddr *) &client_addr, &addrlen);\n"
1813 " if (fd == -1) {\n"
1814 " perror(\"accept()\");\n"
1815 " } else {\n"
1816 " SHUT_FD1;\n"
1817 " SHUT_FD2;\n"
1818 " buf1_avail = buf1_written = 0;\n"
1819 " buf2_avail = buf2_written = 0;\n"
1820 " fd1 = fd;\n"
1821 " fd2 = connect_socket(forward_port, argv[3]);\n"
1822 " if (fd2 == -1)\n"
1823 " SHUT_FD1;\n"
1824 " else\n"
1825 " printf(\"connect from %s\\[rs]n\",\n"
1826 " inet_ntoa(client_addr.sin_addr));\n"
1827 "\\&\n"
1828 " /* Skip any events on the old, closed file\n"
1829 " descriptors. */\n"
1830 "\\&\n"
1831 " continue;\n"
1832 " }\n"
1833 " }\n"
1834 "\\&\n"
1835 " /* NB: read OOB data before normal reads. */\n"
1836 "\\&\n"
1837 " if (fd1 E<gt> 0 && FD_ISSET(fd1, &exceptfds)) {\n"
1838 " char c;\n"
1839 "\\&\n"
1840 " nbytes = recv(fd1, &c, 1, MSG_OOB);\n"
1841 " if (nbytes E<lt> 1)\n"
1842 " SHUT_FD1;\n"
1843 " else\n"
1844 " send(fd2, &c, 1, MSG_OOB);\n"
1845 " }\n"
1846 " if (fd2 E<gt> 0 && FD_ISSET(fd2, &exceptfds)) {\n"
1847 " char c;\n"
1848 "\\&\n"
1849 " nbytes = recv(fd2, &c, 1, MSG_OOB);\n"
1850 " if (nbytes E<lt> 1)\n"
1851 " SHUT_FD2;\n"
1852 " else\n"
1853 " send(fd1, &c, 1, MSG_OOB);\n"
1854 " }\n"
1855 " if (fd1 E<gt> 0 && FD_ISSET(fd1, &readfds)) {\n"
1856 " nbytes = read(fd1, buf1 + buf1_avail,\n"
1857 " BUF_SIZE - buf1_avail);\n"
1858 " if (nbytes E<lt> 1)\n"
1859 " SHUT_FD1;\n"
1860 " else\n"
1861 " buf1_avail += nbytes;\n"
1862 " }\n"
1863 " if (fd2 E<gt> 0 && FD_ISSET(fd2, &readfds)) {\n"
1864 " nbytes = read(fd2, buf2 + buf2_avail,\n"
1865 " BUF_SIZE - buf2_avail);\n"
1866 " if (nbytes E<lt> 1)\n"
1867 " SHUT_FD2;\n"
1868 " else\n"
1869 " buf2_avail += nbytes;\n"
1870 " }\n"
1871 " if (fd1 E<gt> 0 && FD_ISSET(fd1, &writefds) && buf2_avail E<gt> 0) {\n"
1872 " nbytes = write(fd1, buf2 + buf2_written,\n"
1873 " buf2_avail - buf2_written);\n"
1874 " if (nbytes E<lt> 1)\n"
1875 " SHUT_FD1;\n"
1876 " else\n"
1877 " buf2_written += nbytes;\n"
1878 " }\n"
1879 " if (fd2 E<gt> 0 && FD_ISSET(fd2, &writefds) && buf1_avail E<gt> 0) {\n"
1880 " nbytes = write(fd2, buf1 + buf1_written,\n"
1881 " buf1_avail - buf1_written);\n"
1882 " if (nbytes E<lt> 1)\n"
1883 " SHUT_FD2;\n"
1884 " else\n"
1885 " buf1_written += nbytes;\n"
1886 " }\n"
1887 "\\&\n"
1888 " /* Check if write data has caught read data. */\n"
1889 "\\&\n"
1890 " if (buf1_written == buf1_avail)\n"
1891 " buf1_written = buf1_avail = 0;\n"
1892 " if (buf2_written == buf2_avail)\n"
1893 " buf2_written = buf2_avail = 0;\n"
1894 "\\&\n"
1895 " /* One side has closed the connection, keep\n"
1896 " writing to the other side until empty. */\n"
1897 "\\&\n"
1898 " if (fd1 E<lt> 0 && buf1_avail - buf1_written == 0)\n"
1899 " SHUT_FD2;\n"
1900 " if (fd2 E<lt> 0 && buf2_avail - buf2_written == 0)\n"
1901 " SHUT_FD1;\n"
1902 " }\n"
1903 " exit(EXIT_SUCCESS);\n"
1904 "}\n"
1905
1906 #. type: TH
1907 #: fedora-44 fedora-rawhide
1908 #, no-wrap
1909 msgid "2024-07-23"
1910 msgstr "23 يوليو 2024"
1911
1912 #. type: TH
1913 #: fedora-44 fedora-rawhide
1914 #, no-wrap
1915 msgid "Linux man-pages 6.13"
1916 msgstr "صفحات دليل لينكس 6.13"
1917
1918 #. type: TH
1919 #: mageia-cauldron
1920 #, no-wrap
1921 msgid "Linux man-pages 6.17"
1922 msgstr "صفحات دليل لينكس 6.17"
1923
1924 #. type: TH
1925 #: opensuse-leap-16-1 opensuse-tumbleweed
1926 #, no-wrap
1927 msgid "Linux man-pages (unreleased)"
1928 msgstr "صفحات دليل لينكس (لم تصدر بعد)"