1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
|
\input texinfo @c -*-texinfo-*-
@c %**start of header
@setfilename socket++.info
@settitle C++ socket classes
@c %**end of header
@syncodeindex fn cp
@iftex
@finalout
@end iftex
@ifinfo
@dircategory Programming & development tools
@direntry
* socket++: (socket++.info). C++ family of socket classes.
@end direntry
This info file describes the C++ family of socket classes.
Copyright (C) 1992,1993,1994 Gnanasekaran Swaminathan <gs4t@@virginia.edu>
Permission is granted to make and distribute verbatim copies of this
document provided the copyright notice and this permission notice
are preserved on all copies.
@end ifinfo
@titlepage
@title C++ Socket Classes
@subtitle Version: 12Jan97 1.11
@author Gnanasekaran Swaminathan
@page
@vskip 0pt plus 1filll
Copyright @copyright{} 1992,1993,1994 Gnanasekaran Swaminathan
@sp 2
This is Version: 12Jan97 1.11 of the C++ family of socket classes.
@sp 2
Permission is granted to make and distribute verbatim copies of this
document provided the copyright notice and this permission notice
are preserved on all copies.
@end titlepage
@ifinfo
@node Top, Copying, (dir), (dir)
@top Socket++ Library
Socket++ is a family of C++ classes that gives the same
interface as the iostream classes for input and output
for communication between processes.@refill
This documentation describes Version: 12Jan97 1.11 of
socket++ library.@refill
@end ifinfo
@menu
* Copying:: Copyright information.
* Acknowledgments:: Thanks!
* Overview of Socket++:: Overview of socket++ library.
* sockbuf Class:: Socket streambuf class.
* sockAddr Class:: Base class for socket addresses.
* sockinetbuf Class:: Socket class for INET address family.
* sockinetaddr Class:: Address class for INET address family of
sockets.
* sockunixbuf Class:: Socket class for UNIX address family.
* sockunixaddr Class:: Address class for UNIX address family of
sockets.
* sockstream Classes:: I/O socket stream classes and some examples.
* pipestream Classes:: I/O stream classes that provides pipe,
socketpair, and popen facilities.
* Fork Class:: Use the Fork class to fork a child
process.
* protocol Class:: Protocol base class.
* echo Class:: Class implementing the Echo protocol.
* smtp Class:: Class implementing the SMTP protocol.
* Error Handling:: Describes the default error handling in
the socket++ library.
* Pitfalls:: Common mistakes that socket++ library
users make.
* Index:: Index to concepts and program names
@end menu
@node Copying
@unnumbered Socket++ Library Copyright Notice
@cindex copyright notice
@cindex Copyright
Copyright (C) 1992,1993,1994 Gnanasekaran Swaminathan
Permission is granted to use at your own risk and distribute this
software in source and binary forms provided the above copyright
notice and this paragraph are preserved on all copies. This software
is provided "as is" with no express or implied warranty.@refill
@node Acknowledgments
@unnumbered Acknowledgments
@cindex acknowledgments
Gordon Joly <G.Joly@@cs.ucl.ac.uk> for reporting bugs in
pipestream class implementation and providing an ftp site
for the socket++ library at
cs.ucl.ac.uk:~ftp/coside/gnu/socket++-1.x.tar.gz
He also knows how to make the socket++ library a shared
library.
Jim Anderson for reporting a bug in sockinet.C
Carl Gay <cgay@@skinner.cs.uoregon.edu> for reporting a bug
and a fix in sockinet.C
Oliver Imbusch <flabes@@parystec.de> for reporting a bug
in Makefile.in and suggesting several enhancements for sockbuf class.
Dierk Wendt <wendt@@lambda.hella.de> for reporting errors
in the socket++ documentation.
Per Bothner <bothner@@cygnus.com> for configure, config.sub,
config.shared and move-if-change files that are used
to generate Makefile. These files are taken from his libg++-2.4
and hence, these files are governed by the Copyright Notice found
in the file LICENCE in libg++.
@node Overview of Socket++
@chapter Overview of Socket++ Library
@cindex overview of socket++
Socket++ library defines a family of C++ classes that can be used
more effectively than directly calling the underlying low-level
system functions. One distinct advantage of the socket++ is that
it has the same interface as that of the iostream so that
the users can perform type-safe input output. See your local
IOStream library documentation for more information on iostreams.@refill
@code{streambuf} counterpart of the socket++ is @code{sockbuf}.
@code{sockbuf} is an endpoint for communication with yet another
@code{sockbuf} or simply a @code{socket} descriptor. @code{sockbuf}
has also methods that act as interfaces for most of the commonly
used system calls that involve sockets. @xref{sockbuf Class}, for more
information on the socket buffer class.
For each communication domain, we derive a new class from @code{sockbuf}
that has some additional methods that are specific to that domain. At
present, only @var{unix} and @var{inet} domains are supported.
@code{sockunixbuf} class and @code{sockinetbuf} class define the @var{unix}
and @var{inet} domain of sockets respectively. @xref{sockunixbuf Class}, for
@var{unix} sockets and @xref{sockinetbuf Class}, for @var{inet} sockets.
We also have domain specific socket address classes that are
derived from a common base class called @code{sockAddr}.
@code{sockunixaddr} class is used for @var{unix} domain addresses
and @code{sockinetaddr} class is used for @var{inet} domain
addresses. For more information on address classes see @ref{sockAddr Class},
@ref{sockunixaddr Class}, and @ref{sockinetaddr Class}.
@quotation
@emph{Note}: @code{sockAddr} is not spelled @code{sockaddr} in
order to prevent name clash with the @code{struct sockaddr} declared
in @file{<sys/socket.h>}.
@end quotation
We noted earlier that socket++ provides the same interface as the
iostream library. For example, in the internet domain, we have
@code{isockinet}, @code{osockinet}, and @code{iosockinet} classes
that are counterparts to @code{istream}, @code{ostream}, and
@code{iostream} classes of IOStream library.
For more details on @code{iosockstream} classes see @xref{sockstream Classes}.
The services of @code{pipe()}, @code{socketpair()}, and @code{popen()}
system calls are provided by the @code{pipestream} class.
@xref{pipestream Classes}.
@node sockbuf Class
@chapter @code{sockbuf} Class
@cindex sockbuf class
@cindex class sockbuf
@code{sockbuf} class is derived from @code{streambuf} class of the
iostream library. You can simultaneously read and write into a
@code{sockbuf} just like you can listen and talk through a telephone. To
accomplish the above goal, we maintain two independent buffers for
reading and writing.
@menu
* Constructors:: How to construct a @code{sockbuf} object
and how to open a socket?
* Destructor:: How to destruct a @code{sockbuf} object
and how to close a socket?
* Reading and Writing:: How to use @code{sockbuf} as @code{streambuf}?
* Connection Establishment:: How to bind an address and establish a
connection?
* Socket Options:: How to set and get socket options?
* Timeouts:: How to gracefully handle connection inactivity?
@end menu
@node Constructors
@section Constructors
@cindex sockbuf constructors
@findex sockbuf::type
@code{sockbuf} constructors sets up an endpoint for communication. A
@code{sockbuf} object so created can be read from and written to in
linebuffered mode. To change mode, refer to @code{streambuf} class
in your IOStream library.
@quotation
@cindex flushing buffers
@emph{Note}: If you are using AT&T IOStream library, then the
linebuffered mode is permanently turned off. Thus, you need to
explicitly flush a socket stream. You can flush a socket stream buffer
in one of the following four ways:
@example
// os is a socket ostream
os << "this is a test" << endl;
os << "this is a test\n" << flush;
os << "this is a test\n"; os.flush ();
os << "this is a test\n"; os->sync ();
@end example
@end quotation
@code{sockbuf} objects are created as follows where
@itemize @minus
@item
@code{s} and @code{so} are @code{sockbuf} objects
@item
@code{sd} is an integer which is a socket descriptor
@item
@code{af} and @code{proto} are integers which denote domain number and
protocol number respectively
@item
@code{ty} is a @code{sockbuf::type} and must be one of
@code{sockbuf::sock_stream}, @code{sockbuf::sock_dgram},
@code{sockbuf::sock_raw}, @code{sockbuf::sock_rdm}, and
@code{sockbuf::sock_seqpacket}
@end itemize
@table @code
@item sockbuf s(sd);
@itemx sockbuf s;
@findex sockbuf::sockbuf
Set socket descriptor of @code{s} to @code{sd} (defaults to -1).
@code{sockbuf} destructor will close @code{sd}.
@item sockbuf s(af, ty, proto);
Set socket descriptor of @code{s} to @code{::socket(af, int(ty), proto);}
@item sockbuf so(s);
Set socket descriptor of @code{so} to the socket descriptor of @code{s}.
@item s.open(ty, proto)
@findex sockbuf::open
does nothing and returns simply @code{0}, the null pointer
to @code{sockbuf}.
@item s.is_open()
@findex sockbuf::is_open
returns a non-zero number if the socket descriptor is open else return
0.
@item s = so;
@findex sockbuf::operator=
return a reference @code{s} after assigning @code{s} with @code{so}.
@end table
@node Destructor
@section Destructor
@cindex sockbuf destructor
@findex sockbuf::shuthow
@code{sockbuf::~sockbuf()} flushes output and closes its socket if no other
sockbuf is referencing it and _S_DELETE_DONT_CLOSE flag is not set. It
also deletes its read and write buffers.
In what follows,
@itemize @minus
@item
@code{s} is a @code{sockbuf} object
@item
@code{how} is of type @code{sockbuf::shuthow} and must be one of
@code{sockbuf::shut_read}, @code{sockbuf::shut_write}, and
@code{sockbuf::shut_readwrite}
@end itemize
@table @code
@item sockbuf::~sockbuf()
@findex sockbuf::~sockbuf
flushes output and closes its socket if no other
@code{sockbuf} object is referencing it before deleting its read and
write buffers. If the _S_DELETE_DONT_CLOSE flag is set, then the socket
is not closed.
@item s.close()
@findex sockbuf::close
closes the socket even if it is referenced by other @code{sockbuf}
objects and _S_DELETE_DONT_CLOSE flag is set.
@item s.shutdown(how)
@findex sockbuf::shutdown
shuts down read if @code{how} is @code{sockbuf::shut_read}, shuts down
write if @code{how} is @code{sockbuf::shut_write}, and shuts down both
read and write if @code{how} is @code{sockbuf::shut_readwrite}.
@end table
@node Reading and Writing
@section Reading and Writing
@cindex sockbuf reading
@cindex sockbuf writing
@code{sockbuf} class offers several ways to read and write and tailors
the behavior of several virtual functions of @code{streambuf} for socket
communication.
In case of error, @code{sockbuf::error(const char*)} is called.
In what follows,
@itemize @minus
@item
@code{s} is a @code{sockbuf} object
@item
@code{buf} is buffer of type @code{char*}
@item
@code{bufsz} is an integer and is less than @code{sizeof(buf)}
@item
@code{msgf} is an integer and denotes the message flag
@item
@code{sa} is of type @code{sockAddr}
@item
@code{msgh} is a pointer to @code{struct msghdr}
@item
@code{wp} is an integer and denotes time in seconds
@item
@code{c} is a char
@end itemize
@table @code
@item s.is_open()
@findex sockbuf::is_open
returns a non-zero number if the socket descriptor is open else return
0.
@item s.is_eof()
@findex sockbuf::is_eof
returns a non-zero number if the socket has seen EOF while reading else
return 0.
@item s.write(buf, bufsz)
@findex sockbuf::write
returns an int which must be equal to @code{bufsz} if @code{bufsz} chars in
the @code{buf} are written successfully. It returns 0 if there is
nothing to write or if, in case of timeouts, the socket is not ready
for write @ref{Timeouts}.
@item s.send(buf, bufsz, msgf)
@findex sockbuf::send
@findex sockbuf::msgflag
same as @code{sockbuf::write} described above but allows the user to
control the transmission of messages using the message flag @code{msgf}.
If @code{msgf} is @code{sockbuf::msg_oob} and the socket type of
@code{s} is @code{sockbuf::sock_stream}, @code{s} sends the message in
@var{out-of-band} mode. If @code{msgf} is @code{sockbuf::msg_dontroute},
@code{s} sends the outgoing packets without routing. If @code{msgf} is
0, which is the default case, @code{sockbuf::send} behaves exactly like
@code{sockbuf::write}.
@item s.sendto(sa, buf, bufsz, msgf)
@findex sockbuf::sendto
same as @code{sockbuf::send} but works on unconnected sockets. @code{sa}
specifies the @var{to} address for the message.
@item s.sendmsg(msgh, msgf)
@findex sockbuf::sendmsg
same as @code{sockbuf::send} but sends a @code{struct msghdr} object
instead.
@item s.sys_write(buf, bufsz)
@findex sockbuf::sys_write
calls @code{sockbuf::write} and returns the result. Unlike
@code{sockbuf::write} @code{sockbuf::sys_write} is declared as a virtual
function.
@item s.read(buf, bufsz)
@findex sockbuf::read
returns an int which is the number of chars read into the @code{buf}. In
case of EOF, return EOF. Here, @code{bufsz} indicates the size of the
@code{buf}. In case of timeouts, return 0 @ref{Timeouts}.
@item s.recv(buf, bufsz, msgf)
@findex sockbuf::recv
@findex sockbuf::msgflag
same as @code{sockbuf::read} described above but allows the user to receive
@var{out-of-band} data if @code{msgf} is @code{sockbuf::msg_oob} or to
preview the data waiting to be read if @code{msgf} is
@code{sockbuf::msg_peek}. If @code{msgf} is 0, which is the default
case, @code{sockbuf::recv} behaves exactly like @code{sockbuf::read}.
@item s.recvfrom(sa, buf, bufsz, msgf)
@findex sockbuf::recvfrom
same as @code{sockbuf::recv} but works on unconnected sockets. @code{sa}
specifies the @var{from} address for the message.
@item s.recvmsg(msgh, msgf)
@findex sockbuf::recvmsg
same as @code{sockbuf::recv} but reads a @code{struct msghdr} object
instead.
@item s.sys_read(buf, bufsz)
@findex sockbuf::sys_read
calls @code{sockbuf::read} and returns the result. Unlike
@code{sockbuf::read} @code{sockbuf::sys_read} is declared as a virtual
function.
@item s.is_readready(wp_sec, wp_usec)
@findex sockbuf::is_readready
returns a non-zero int if @code{s} has data waiting to be read from the
communication channel. If @code{wp_sec >= 0}, it waits for
@code{wp_sec 10^6 + wp_usec} microseconds before returning 0 in case
there are no data waiting to be read. If @code{wp_sec < 0}, then it waits until
a datum arrives at the communication channel. @code{wp_usec} defaults to 0.
@quotation
@emph{Please Note}: The data waiting in @code{sockbuf}'s own buffer is
different from the data waiting in the communication channel.
@end quotation
@item s.is_writeready(wp_sec, wp_usec)
@findex sockbuf::is_writeready
returns a non-zero int if data can be written onto the communication
channel of @code{s}. If @code{wp_sec >= 0}, it waits for
@code{wp_sec 10^6 + wp_usec} microseconds before returning 0 in case no
data can be written. If @code{wp_sec < 0}, then it waits until
the communication channel is ready to accept data. @code{wp_usec}
defaults to 0.
@quotation
@emph{Please Note}: The buffer of the @code{sockbuf} class is different
from the buffer of the communication channel buffer.
@end quotation
@item s.is_exceptionpending(wp_sec, wp_usec)
@findex sockbuf::is_exceptionpending
returns non-zero int if @code{s} has any exception events pending.
If @code{wp_sec >= 0}, it waits for @code{wp_sec 10^6 + wp_usec}
microseconds before returning 0 in case @code{s} does
not have any exception events pending. If @code{wp_sec < 0},
then it waits until an expception event occurs. @code{wp_usec} defaults
to 0.
@quotation
@emph{Please Note}: The exceptions that
@code{sockbuf::is_exceptionpending} is looking for are different from
the C++ exceptions.
@end quotation
@item s.flush_output()
@findex sockbuf::flush_output
flushes the output buffer and returns the number of chars flushed.
In case of error, return EOF. @code{sockbuf::flush_output} is a
protected member function and it is not available for general public.
@item s.doallocate()
@findex sockbuf::doallocate
allocates free store for read and write buffers of @code{s} and returns
1 if allocation is done and returns 0 if there is no need.
@code{sockbuf::doallocate} is a protected virtual member function and it
is not available for general public.
@item s.underflow()
@findex sockbuf::underflow
returns the unread char in the buffer as an unsigned char if there is
any. Else returns EOF if @code{s} cannot allocate space for the buffers,
cannot read or peer is closed. @code{sockbuf::underflow} is a protected
virtual member function and it is not available for general public.
@item s.overflow(c)
@findex sockbuf::overflow
if @code{c==EOF}, call and return the result of @code{flush_output()},
else if @code{c=='\n'} and @code{s} is linebuffered, call
@code{flush_output()} and return @code{c} unless @code{flush_output()}
returns EOF, in which case return EOF. In any other case, insert char
@code{c} into the buffer and return @code{c} as an unsigned char.
@code{sockbuf::overflow} is a protected member virtual function and it is not
available for general public.
@quotation
@emph{Node:} linebuffered mode does not work with AT&T IOStream library.
Use explicit flushing to flush @code{sockbuf}.
@end quotation
@item s.sync()
@findex sockbuf::sync
@cindex flushing output
calls @code{flush_output()} and returns the result. Useful if the user needs
to flush the output without writing newline char into the write buffer.
@item s.xsputn(buf, bufsz)
@findex sockbuf::xsputn
write @code{bufsz} chars into the buffer and returns the number of chars
successfully written. Output is flushed if any char in
@code{buf[0..bufsz-1]} is @code{'\n'}.
@item s.recvtimeout(wp)
@findex sockbuf::recvtimeout
sets the recv timeout to @code{wp} seconds. If @code{wp} is -1, it is a
block and if @code{wp} is 0, it is a poll.
It affects all read functions. If the socket is not read ready within
@code{wp} seconds, the read call will return 0. It also affects
@code{sockbuf::underflow}. @code{sockbuf::underflow} will not set the
@code{_S_EOF_SEEN} flag if it is returning EOF because of timeout.
@code{sockbuf::recvtimeout} returns the old recv timeout value.
@item s.sendtimeout(wp)
@findex sockbuf::sendtimeout
sets the send timeout to @code{wp} seconds. If @code{wp} is -1, it is a
block and if @code{wp} is 0, it is a poll.
It affects all write functions. If the socket is not write ready within
@code{wp} seconds, the write call will return 0.
@code{sockbuf::sendtimeout} returns the old send timeout value.
@end table
@node Connection Establishment
@section Establishing connections
@cindex connection establishment
@cindex names
A name must be bound to a @code{sockbuf} if processes want to refer to
it and use it for communication. Names must be unique. A @var{unix} name
is a 3-tuple, @var{<protocol, local path, peer path>}. An @var{inet} name
is a 5-tuple, @var{<protocol, local addr, local port, peer addr, peer
port>}. @code{sockbuf::bind} is used to specify the local half of the
name---@var{<local path>} for @var{unix} and @var{<local addr, local
port>} for @var{inet}. @code{sockbuf::connect} and
@code{sockbuf::accept} are used to specify the peer half of the
name---@var{<peer path>} for @var{unix} and @var{<peer addr, peer port>}
for @var{inet}.
In what follows,
@itemize @minus
@item
@code{s} and @code{so} are @code{sockbuf} objects
@item
@code{sa} is a @code{sockAddr} object
@item
@code{nc} is an integer denoting the number of connections to allow
@end itemize
@table @code
@item s.bind(sa)
@findex sockbuf::bind
@cindex binding addresses
binds @code{sockAddr} @code{sa} as the local half of the name for
@code{s}. It returns 0 on success and returns the errno on failure.
@item s.connect(sa)
@findex sockbuf::connect
@cindex connect
@code{sockbuf::connect} uses @code{sa} to provide the peer half of the
name for @code{s} and to establish the connection itself.
@code{sockbuf::connect} also provides the local half of the name
automatically and hence, the user should not use @code{sockbuf::bind} to
bind any local half of the name. It returns 0 on success and returns
the errno on failure.
@item s.listen(nc)
@findex sockbuf::listen
@cindex listening
makes @code{s} ready to accept connections. @code{nc} specifies the
maximum number of outstanding connections that may be queued and must be
at least 1 and less than or equal to @code{sockbuf::somaxconn} which
is usually 5 on most systems.
@item sockbuf so = s.accept(sa)
@itemx sockbuf so = s.accept()
@findex sockbuf::accept
@cindex accepting connections
accepts connections and returns the peer address in @code{sa}. @code{s}
must be a listening @code{sockbuf}. See @code{sockbuf::listen} above.
@end table
@node Socket Options
@section Getting and Setting Socket Options
@cindex socket options
@cindex option setting
@cindex option getting
Socket options are used to control a socket communication. New options
can be set and old value of the options can be retrived at the protocol
level or at the socket level by using @code{setopt} and
@code{getopt} member functions. In addition, you can also use special
member functions to get and set specific options.
In what follows,
@itemize @minus
@item
@code{s} is a @code{sockbuf} object
@item
@code{opval} is an integer and denotes the option value
@item
@code{op} is of type @code{sockbuf::option} and must be one of
@itemize @bullet
@item
@code{sockbuf::so_error} used to retrieve and clear error status
@item
@code{sockbuf::so_type} used to retrieve type of the socket
@item
@code{sockbuf::so_debug} is used to specify recording of debugging
information
@item
@code{sockbuf::so_reuseaddr} is used to specify the reuse of local address
@item
@code{sockbuf::so_keepalive} is used to specify whether to keep connections
alive or not
@item
@code{sockbuf::so_dontroute} is used to specify whether to route messages
or not
@item
@code{sockbuf::so_broadcast} is used to specify whether to broadcast
@code{sockbuf::sock_dgram} messages or not
@item
@code{sockbuf::so_oobinline} is used to specify whether to inline
@var{out-of-band} data or not.
@item
@code{sockbuf::so_linger} is used to specify for how long to linger before
shutting down
@item
@code{sockbuf::so_sndbuf} is used to retrieve and to set the size of the
send buffer (communication channel buffer not @code{sockbuf}'s internal
buffer)
@item
@code{sockbuf::so_rcvbuf} is used to retrieve and to set the size of the
recv buffer (communication channel buffer not @code{sockbuf}'s internal
buffer)
@end itemize
@end itemize
@table @code
@item s.getopt(op, &opval, sizeof(opval), oplevel)
@findex sockbuf::getopt
@cindex getsockopt
gets the option value of the @code{sockbuf::option} @code{op} at the
option level @code{oplevel} in @code{opval}. It returns the actual size
of the buffer @code{opval} used. The default value of the @code{oplevel}
is @code{sockbuf::sol_socket}.
@item s.setopt(op, &opval, sizeof(opval), oplevel)
@findex sockbuf::setopt
@cindex setsockopt
sets the option value of the @code{sockbuf::option} @code{op} at the
option level @code{oplevel} to @code{opval}. The default value of the
@code{oplevel} is @code{sockbuf::sol_socket}.
@item s.gettype()
@findex sockbuf::gettype
gets the socket type of @code{s}. The return type is
@code{sockbuf::type}.
@item s.clearerror()
@findex sockbuf::clearerror
gets and clears the error status of the socket.
@item s.debug(opval)
@findex sockbuf::debug
if @code{opval} is not -1, set the @code{sockbuf::so_debug} option value
to @code{opval}. In any case, return the old option value of
@code{sockbuf::so_debug} option. The default value of @code{opval} is
-1.
@item s.reuseaddr(opval)
@findex sockbuf::reuseaddr
if @code{opval} is not -1, set the @code{sockbuf::so_reuseaddr} option value
to @code{opval}. In any case, return the old option value of
@code{sockbuf::so_reuseaddr} option. The default value of @code{opval}
is -1.
@item s.dontroute(opval)
@findex sockbuf::dontroute
if @code{opval} is not -1, set the @code{sockbuf::so_dontroute} option value
to @code{opval}. In any case, return the old option value of
@code{sockbuf::so_dontroute} option. The default value of @code{opval}
is -1.
@item s.oobinline(opval)
@findex sockbuf::oobinline
if @code{opval} is not -1, set the @code{sockbuf::so_oobinline} option value
to @code{opval}. In any case, return the old option value of
@code{sockbuf::so_oobinline} option. The default value of @code{opval}
is -1.
@item s.broadcast(opval)
@findex sockbuf::broadcast
if @code{opval} is not -1, set the @code{sockbuf::so_broadcast} option value
to @code{opval}. In any case, return the old option value of
@code{sockbuf::so_broadcast} option. The default value of @code{opval}
is -1.
@item s.keepalive(opval)
@findex sockbuf::keepalive
if @code{opval} is not -1, set the @code{sockbuf::so_keepalive} option value
to @code{opval}. In any case, return the old option value of
@code{sockbuf::so_keepalive} option. The default value of @code{opval}
is -1.
@item s.sendbufsz(opval)
@findex sockbuf::sndbuf
if @code{opval} is not -1, set the new send buffer size to @code{opval}.
In any case, return the old buffer size of the send buffer. The default
value of @code{opval} is -1.
@item s.recvbufsz(opval)
@findex sockbuf::rcvbuf
if @code{opval} is not -1, set the new recv buffer size to @code{opval}.
In any case, return the old buffer size of the recv buffer. The default
value of @code{opval} is -1.
@item s.linger(tim)
@findex sockbuf::linger
if @code{tim} is positive, set the linger time to tim seconds.
If @code{tim} is 0, set the linger off.
In any case, return the old linger time if it was set earlier.
Otherwise return -1. The default value of @code{tim} is -1.
@end table
@node Timeouts
@section Time Outs While Reading and Writing
@cindex timeouts
@cindex read timeouts
@cindex write timeouts
Time outs are very useful in handling data of unknown sizes
and formats while reading and writing. For example, how does
one communicate with a socket that sends chunks of data
of unknown size and format? If only @code{sockbuf::read} is used
without time out, it will block indefinitely.
In such cases, time out facility is the only answer.
The following idiom is recommended. @xref{Pitfalls} for a complete
example.
@example
int old_tmo = s.recvtimeout (2) // set time out (2 seconds here)
for (;;) @{ // read or write
char buf[256];
int rval = s.read (buf, 256);
if (rval == 0 || rval == EOF) break;
// process buf here
@}
s.recvtimeout (old_tmo); // reset time out
@end example
In what follows,
@itemize @minus
@item
@code{s} is a @code{sockbuf} object
@item
@code{wp} is waiting period in seconds
@end itemize
@table @code
@item s.recvtimeout(wp)
@findex sockbuf::recvtimeout
sets the recv timeout to @code{wp} seconds. If @code{wp} is -1, it is a
block and if @code{wp} is 0, it is a poll.
It affects all read functions. If the socket is not read ready within
@code{wp} seconds, the read call will return 0. It also affects
@code{sockbuf::underflow}. @code{sockbuf::underflow} will not set the
@code{_S_EOF_SEEN} flag if it is returning EOF because of timeout.
@code{sockbuf::recvtimeout} returns the old recv timeout value.
@item s.sendtimeout(wp)
@findex sockbuf::sendtimeout
sets the send timeout to @code{wp} seconds. If @code{wp} is -1, it is a
block and if @code{wp} is 0, it is a poll.
It affects all write functions. If the socket is not write ready within
@code{wp} seconds, the write call will return 0.
@code{sockbuf::sendtimeout} returns the old send timeout value.
@end table
@node sockAddr Class
@chapter sockAddr Class
@cindex sockAddr class
@cindex base address class
Class @code{sockAddr} is an abstract base class for all socket address
classes. That is, domain specific socket address classes are all derived
from @code{sockAddr} class.
@quotation
@emph{Note}: @code{sockAddr} is not spelled @code{sockaddr} in
order to prevent name clash with @code{struct sockaddr} declared
in @file{<sys/socket.h>}.
@end quotation
Non-abstract derived classes must have definitions for the following
functions.
@table @code
@item sockAddr::operator void* ()
@findex sockAddr::operator void*
should simply return @code{this}.
@item sockAddr::size()
@findex sockAddr::size
should return @code{sizeof(*this)}. The return type is @code{int}.
@item sockAddr::family()
@findex sockAddr::family
should return address family (domain name) of the socket address. The
return type is @code{int}
@end table
@node sockinetbuf Class
@chapter sockinetbuf Class
@cindex sockinetbuf class
@cindex inet domain
@code{sockinetbuf} class is derived from @code{sockbuf} class and inherits
most of the public functions of @code{sockbuf}. @xref{sockbuf Class},
for more information on @code{sockbuf}. In addition, it provides methods
for getting @code{sockinetaddr} of local and peer connections.
@xref{sockinetaddr Class}, for more information on @code{sockinetaddr}.
@menu
* Methods sockinetbuf:: Describes sockinetbuf member functions.
* Datagram INET:: A pair of example programs demonstrating
datagram connection in inet domain.
* Stream INET:: A pair of example programs demonstrating
stream connection in inet domain.
@end menu
@node Methods sockinetbuf
@section Methods
In what follows,
@itemize @minus
@item
@code{ty} denotes the type of the socket connection and is of type
@code{sockbuf::type}
@item
@code{proto} denotes the protocol and is of type int
@item
@code{si, ins} are @code{sockbuf} objects and are in @var{inet} domain
@item
@code{adr} denotes an @var{inet} address in host byte order and is of
type unsigned long
@item
@code{serv} denotes a service like "nntp" and is of type char*
@item
@code{proto} denotes a protocol like "tcp" and is of type char*
@item
@code{thostname} is of type char* and denotes the name of a host like
@code{"kelvin.acc.virginia.edu"} or @code{"128.143.24.31"}.
@item
@code{portno} denotes a port in host byte order and is of type int
@end itemize
@table @code
@item sockinetbuf ins(ty, proto)
@findex sockinetbuf::sockinetbuf
Constructs a @code{sockinetbuf} object @code{ins} whose socket
communication type is @code{ty} and protocol is @code{proto}.
@code{proto} defaults to 0.
@item sockinetbuf ins(si)
Constructs a @code{sockinetbuf} object @code{ins} which uses the same
socket as @code{si} uses.
@item ins = si
@findex sockinetbuf::operator =
performs the same function as @code{sockbuf::operator=}.
@xref{sockbuf Class}, for more details.
@item ins.open(ty, proto)
@findex sockinetbuf::open
create a new @code{sockinetbuf} whose type and protocol are
@code{ty} and @code{proto} respectively and assign it to @code{ins}.
@item sockinetaddr sina = ins.localaddr()
@findex sockinetbuf::localaddr
@findex getsockname (see sockinetbuf::localaddr)
returns the local @var{inet} address of the @code{sockinetbuf} object
@code{ins}. The call will make sense only after a call to either
@code{sockbuf::bind} or @code{sockbuf::connect}.
@item sockinetaddr sina = ins.peeraddr()
@findex sockinetbuf::peeraddr
@findex getpeername (see sockinetbuf::peeraddr)
returns the peer @var{inet} address of the @code{sockinetbuf} object
@code{ins}. The call will make sense only after a call to
@code{sockbuf::connect}.
@item const char* hn = ins.localhost()
@findex sockinetbuf::localhost
returns the local @var{inet} thostname of the @code{sockinetbuf} object
@code{ins}. The call will make sense only after a call to either
@code{sockbuf::bind} or @code{sockbuf::connect}.
@item const char* hn = ins.peerhost()
@findex sockinetbuf::peerhost
returns the peer @var{inet} thostname of the @code{sockinetbuf} object
@code{ins}. The call will make sense only after a call to
@code{sockbuf::connect}.
@item int pn = ins.localport()
@findex sockinetbuf::localport
returns the local @var{inet} port number of the @code{sockinetbuf} object
@code{ins} in host byte order. The call will make sense only after a
call to either @code{sockbuf::bind} or @code{sockbuf::connect}.
@item int pn = ins.peerport()
@findex sockinetbuf::peerport
returns the peer @var{inet} port number of the @code{sockinetbuf} object
@code{ins} in local host byte order. The call will make sense only after a
call to @code{sockbuf::connect}.
@item ins.bind ()
@findex sockinetbuf::bind
binds @code{ins} to the default address @var{INADDR_ANY} and the default
port. It returns 0 on success and returns the errno on failure.
@item ins.bind (adr, portno)
binds @code{ins} to the address @code{adr} and the port @code{portno}.
It returns 0 on success and returns the errno on failure.
@item ins.bind (adr, serv, proto)
binds @code{ins} to the address, @code{adr} and the port corresponding to
the service @code{serv} and the protocol @code{proto}>.
It returns 0 on success and returns the errno on failure.
@item ins.bind (thostname, portno)
binds @code{ins} to the address corresponding to the hostname
@code{thostname} and the port @code{portno}.
It returns 0 on success and returns the errno on failure.
@item ins.bind (thostname, serv, proto)
binds @code{ins} to the address corresponding to the hostname
@code{thostname} and the port corresponding to the service @code{serv}
and the protocol @code{proto}>. It returns 0 on success and
returns the errno on failure.
@item ins.connect (adr, portno)
@findex sockinetbuf::connect
connects @code{ins} to the address @code{adr} and the port @code{portno}.
It returns 0 on success and returns the errno on failure.
@item ins.connect (adr, serv, proto)
connects @code{ins} to the address, @code{adr} and the port corresponding to
the service @code{serv} and the protocol @code{proto}>.
It returns 0 on success and returns the errno on failure.
@item ins.connect (thostname, portno)
connects @code{ins} to the address corresponding to the hostname
@code{thostname} and the port @code{portno}.
It returns 0 on success and returns the errno on failure.
@item ins.connect (thostname, serv, proto)
connects @code{ins} to the address corresponding to the hostname
@code{thostname} and the port corresponding to the service @code{serv}
and the protocol @code{proto}>.
It returns 0 on success and returns the errno on failure.
@end table
@node Datagram INET
@section @var{inet} Datagram Sockets
@cindex sockinetbuf dgram example
@cindex datagram inet
@cindex isockinet example
@cindex osockinet example
The following two programs illustrates how to use @code{sockinetbuf} class
for datagram connection in @var{inet} domain. @code{tdinread.cc} also
shows how to use @code{isockinet} class and @code{tdinwrite.cc} shows
how to use @code{osockinet} class.
@subheading tdinread.cc
@example
// reads data sent by tdinwrite.cc
#include <sockinet.h>
int main(int ac, char** av)
@{
isockinet is (sockbuf::sock_dgram);
is->bind();
cout << "localhost = " << so.localhost() << endl
<< "localport = " << so.localport() << endl;
char buf[256];
int n;
is >> n;
cout << av[0] << ": ";
while(n--) @{
is >> buf;
cout << buf << ' ';
@}
cout << endl;
return 0;
@}
@end example
@subheading tdinwrite.cc
@example
// sends data to tdinread.cc
#include <sockinetbuf.h>
#include <stdlib.h>
int main(int ac, char** av)
@{
if (ac < 3) @{
cerr << "USAGE: " << av[0] << " thostname port-number "
<< "data ... " << endl;
return 1;
@}
osockinet os (sockbuf::sock_dgram);
os->connect (av[1], atoi(av[2]));
cout << "local: " << so.localport() << ' '
<< so.localhost() << endl
<< "peer: " << so.peerport() << ' '
<< so.peerhost() << endl;
os << ac-3; av += 3;
while(*av) os << *av++ << ' ';
os << endl;
return 0;
@}
@end example
@node Stream INET
@section @var{inet} Stream Sockets
@cindex stream inet
@cindex sockinetbuf stream example
@cindex iosockinet example
The following two programs illustrates the use of @code{sockinetbuf} class
for stream connection in @var{inet} domain. It also shows how to use
@code{iosockinet} class.
@subheading tsinread.cc
@example
// receives strings from tsinwrite.cc and sends the strlen
// of each string back to tsinwrite.cc
#include <sockinet.h>
int main()
@{
sockinetbuf si(sockbuf::sock_stream);
si.bind();
cout << si.localhost() << ' ' << si.localport() << endl;
si.listen();
iosockinet s = si.accept();
char buf[1024];
while (s >> buf) @{
cout << buf << ' ';
s << ::strlen(buf) << endl;
@}
cout << endl;
return 0;
@}
@end example
@subheading tsinwrite.cc
@example
// sends strings to tsinread.cc and gets back their length
// usage: tsinwrite hostname portno
// see the output of tsinread for what hostname and portno to use
#include <sockinet.h>
#include <stdlib.h>
int main(int ac, char** av)
@{
iosockinet sio (sockbuf::sock_stream);
sio->connect (av[1], atoi (av[2]));
sio << "Hello! This is a test\n" << flush;
// terminate the while loop in tsinread.cc
si.shutdown(sockbuf::shut_write);
int len;
while (s >> len) cout << len << ' ';
cout << endl;
return 0;
@}
@end example
@node sockinetaddr Class
@chapter sockinetaddr Class
@cindex sockinetaddr class
@cindex inet address class
Class @code{sockinetaddr} is derived from @code{sockAddr} declared in
@code{<sockstream.h>} and from @code{sockaddr_in} declared in
@code{<netinet/in.h>}. Always use a @code{sockinetaddr} object for an
address with @var{inet} domain of sockets. @xref{Connection
Establishment}.
In what follows,
@itemize @minus
@item
@code{adr} denotes an @var{inet} address in host byte order and is of
type unsigned long
@item
@code{serv} denotes a service like "nntp" and is of type char*
@item
@code{proto} denotes a protocol like "tcp" and is of type char*
@item
@code{thostname} is of type char* and denotes the name of a host like
@code{"kelvin.acc.virginia.edu"} or @code{"128.143.24.31"}.
@item
@code{portno} denotes a port in host byte order and is of type int
@end itemize
@table @code
@item sockinetaddr sina
@findex sockinetaddr::sockinetaddr
Constructs a @code{sockinetaddr} object @code{sina} with default address
@var{INADDR_ANY} and default port number 0.
@item sockinetaddr sina(adr, portno)
Constructs a @code{sockinetaddr} object @code{sina} setting inet address
to @code{adr} and the port number to @code{portno}. @code{portno}
defaults to 0.
@item sockinetaddr sina(adr, serv, proto)
Constructs a @code{sockinetaddr} object @code{sina} setting inet address
to @code{adr} and the port number corresponding to the service
@code{serv} and the protocol @code{proto}. The protocol defaults to "tcp".
@item sockinetaddr sina(thostname, portno)
Constructs a @code{sockinetaddr} object @code{sina} setting inet address
to the address of @code{thostname} and the port number to @code{portno}.
@code{portno} defaults to 0.
@item sockinetaddr sina(thostname, serv, proto)
Constructs a @code{sockinetaddr} object @code{sina} setting inet address
to the address of @code{thostname} and the port number corresponding to
the service @code{serv} and the protocol @code{proto}. The protocol
defaults to "tcp".
@item void* a = sina
@findex sockinetaddr::operator void*
returns the address of the @code{sockaddr_in} part of
@code{sockinetaddr} object @code{sina} as void*.
@item int sz = sina.size()
@findex sockinetaddr::size
returns the sizeof @code{sockaddr_in} part of @code{sockinetaddr} object
@code{sina}.
@item int af = sina.family()
@findex sockinetaddr::family
returns @code{sockinetbuf::af_inet} if all is well.
@item int pn = sina.getport()
@findex sockinetaddr::getport
returns the port number of the @code{sockinetaddr} object @code{sina} in
host byte order.
@item const char* hn = getthostname()
@findex sockinetaddr::getthostname
returns the host name of the @code{sockinetaddr} object @code{sina}.
@end table
@node sockunixbuf Class
@chapter sockunixbuf Class
@cindex sockunixbuf class
@cindex unix domain
@code{sockunixbuf} class is derived from @code{sockbuf} class declared in
@code{<sockstream.h>} and hence, inherits most of the public member
functions of @code{sockbuf}. @xref{sockbuf Class}, for more information
on @code{sockbuf}.
@menu
* Methods sockunixbuf:: Describes sockunixbuf member functions
* Datagram UNIX:: A pair of example programs demonstrating
datagram connection in @var{unix} domain
* Stream UNIX:: A pair of example programs demonstrating
stream connection in @var{unix} domain
@end menu
@node Methods sockunixbuf
@section Methods
In what follows,
@itemize @minus
@item
@code{ty} denotes the socket type and is of type @code{sockbuf::type}
@item
@code{proto} denotes the protocol number and is of type int
@item
@code{su} is a @code{sockbuf} and must be in @var{unix} domain
@item
@code{path} is the @var{unix} path name like "/tmp/unix_socket"
@end itemize
@table @code
@item sockunixbuf uns(ty, proto)
@findex sockunixbuf::sockunixbuf
Constructs a @code{sockunixbuf} object @code{uns} with @code{ty} as its
type and @code{proto} as its protocol number. @code{proto} defaults to
0.
@item sockunixbuf uns = su
Constructs a @code{sockunixbuf} object @code{uns} which uses the same
socket as is used by @code{su}.
@item uns = su
@findex sockunixbuf::operator =
@code{sockunixbuf} object @code{uns} closes its current socket if no other
@code{sockbuf} is referring to it and uses the socket that @code{sockbuf}
object @code{su} is using.
@item uns.open(ty, proto)
@findex sockunixbuf::open
create a @code{sockunixbuf} object with @code{ty} as its type and
@code{proto} as its protocol and assign the @code{sockunixbuf} object so
created to @code{*this}. It returns @code{this}. @code{proto} defaults
to 0.
@item uns.bind(path)
@findex sockunixbuf::bind
binds @code{uns} to the @var{unix} pathname @code{path}.
It returns 0 on success and returns the errno on failure.
@item uns.connect(path)
@findex sockunixbuf::connect
connects @code{uns} to the @var{unix} pathname @code{path}.
It returns 0 on success and returns the errno on failure.
@end table
@node Datagram UNIX
@section @var{unix} Datagram Sockets
@cindex datagram unix
@cindex isockunix example
@cindex osockunix example
The following two programs illustrates how to use @code{sockunixbuf} class
for datagram connection in @var{unix} domain. @code{tdunread.cc} also
shows how to use @code{isockunix} class and @code{tdunwrite.cc} shows
how to use @code{osockunix} class.
@subheading tdunread.cc
@example
// reads data sent by tdunwrite.cc
#include <sockunix.h>
#include <unistd.h>
#include <errno.h>
int main(int ac, char** av)
@{
if (ac != 2) @{
cerr << "USAGE: " << av[0] << " socket_path_name\n";
return 1;
@}
// isockunix builds the sockunixbuf object
isockunix su (sockbuf::sock_dgram);
su->bind(av[1]);
cout << "Socket name = " << av[1] << endl;
if (chmod(av[1], 0777) == -1) @{
perror("chmod");
return 1;
@}
char buf[1024];
int i;
su >> i;
cout << av[0] << ": " << i << " strings: ";
while (i--) @{
su >> buf;
cout << buf << ' ';
@}
cout << endl;
unlink(av[1]);
return 0;
@}
@end example
@subheading tdunwrite.cc
@example
// sends data to tdunread.cc
#include <sockunix.h>
int main(int ac, char** av)
@{
if (ac < 2) @{
cerr << "USAGE: " << av[0]
<< " socket_path_name data...\n";
return 1;
@}
osockunix su (sockbuf::sock_dgram);
su->connect (av[1]);
su << ac << ' ';
while (*av) @{ su << av[i] << ' '; av++; @}
su << endl;
return 0;
@}
@end example
@node Stream UNIX
@section @var{unix} Stream Sockets
@cindex stream unix
@cindex sockunixbuf example
@cindex iosockunix example
The following two programs illustrates how to use @code{sockunixbuf} class
for stream connection in @var{unix} domain. It also shows how to use
@code{iosockunix} class.
@subheading tsunread.cc
@example
// exchanges char strings with tsunwrite.cc
#include <sockunix.h>
#include <unistd.h>
#include <errno.h>
int main(int ac, char** av)
@{
if (ac != 2) @{
cerr << "USAGE: " << av[0] << " socket_path_name\n";
return 1;
@}
sockunixbuf su(sockbuf::sock_stream);
su.bind(av [1]);
cout << "Socket name = " << av[1] << endl;
if (chmod(av[1], 0777) == -1) @{
perror("chmod");
return 1;
@}
su.listen(3);
iosockunix ioput = su.accept ();
char buf[1024];
ioput << av[0] << ' ' << av[1] << endl;
while ( ioput >> buf ) cout << av[0] << ": " << buf << endl;
unlink(av[1]);
return 0;
@}
@end example
@subheading tsunwrite.cc
@example
// exchanges char strings with tsunread.cc
#include <sockunix.h>
int main(int ac, char** av)
@{
if (ac < 2) @{
cerr << "USAGE: " << av[0]
<< " socket_path_name data...\n";
return 1;
@}
iosockunix oput (sockbuf::sock_stream);
oput->connect (av [1]);
char buf[128];
oput >> buf;
cout << buf << ' ';
oput >> buf;
cout << buf << endl;
while (*av) oput << *av++ << ' ';
oput << endl;
return 0;
@}
@end example
@node sockunixaddr Class
@chapter sockunixaddr Class
@cindex sockunixaddr class
@cindex unix address class
Class @code{sockunixaddr} is derived from class @code{sockAddr} declared in
@code{<sockstream.h>} and from struct @code{sockaddr_un} declared in
@code{<sys/un.h>}. Always use @code{sockunixaddr} objects for addresses
with @var{unix} domain of sockets. @xref{Connection Establishment}.
In what follows,
@itemize @minus
@item
@code{path} is the @var{unix} path name like "/tmp/unix_socket"
@end itemize
@table @code
@item sockunixaddr suna(path)
@findex sockunixaddr::sockunixaddr
Constructs a @code{sockunixaddr} object @code{suna} with @code{path} as
the @var{unix} path name.
@item void* a = suna
@findex sockunixaddr::operator void*
returns the address of the @code{sockaddr_un} part of
@code{sockunixaddr} object @code{suna} as void*.
@item int sz = suna.size()
@findex sockunixaddr::size
returns the sizeof @code{sockaddr_un} part of @code{sockunixaddr} object
@code{suna}.
@item int af = suna.family()
@findex sockunixaddr::family
returns @code{sockunixbuf::af_unix} if all is well.
@end table
@node sockstream Classes
@chapter sockstream Classes
@cindex sockstream classes
@cindex iosockstream classes
sockstream classes are designed in such a way that they provide the same
interface as their stream counterparts do. We have @code{isockstream}
derived from @code{istream} and @code{osockstream} derived from
@code{ostream}. We also have @code{iosockstream} which is derived from
@code{iostream}.
Each domain also has its own set of @code{stream} classes. For example,
@code{unix} domain has @code{isockunix}, @code{osockunix}, and
@code{iosockunix} derived from @code{isockstream}, @code{osockstream},
and @code{iosockstream} respectively. Similarly, @code{inet} domain has
@code{isockinet}, @code{osockinet}, and @code{iosockinet}.
@menu
* iosockstream:: Generic IOStream classes for sockbuf
buffers.
* iosockinet:: IOStream classes for @var{inet} domain of
sockets.
* iosockunix:: IOStream classes for @var{unix} domain of
sockets.
@end menu
@node iosockstream
@section iosockstreams
@subsection isockstream Class
@cindex isockstream class
Since @code{isockstream} is publicly derived from @code{istream}, most
of the public functions of @code{istream} are also available in
@code{isockstream}.
@code{isockstream} redefines @code{rdbuf()} defined in its virtual base
class @code{ios}. Since, @code{ios::rdbuf()} is not virtual, care must
be taken to call the correct @code{rdbuf()} through a reference or a
pointer to an object of class @code{isockstream}.
In what follows,
@itemize @minus
@item
@code{sb} is a @code{sockbuf} object
@item
@code{sbp} is a pointer to a @code{sockbuf} object
@end itemize
@table @code
@item isockstream is(sb)
@findex isockstream::isockstream
Constructs an @code{isockstream} object @code{is} with @code{sb} as its
@code{sockbuf}.
@item isockstream is(sbp)
Constructs an @code{isockstream} object @code{is} with @code{*sbp} as its
@code{sockbuf}.
@item sbp = is.rdbuf()
@findex isockstream::rdbuf
returns a pointer to the @code{sockbuf} of the @code{isockstream} object
@code{is}.
@item isockstream::operator -> ()
@findex isockstream::operator->
returns a pointer to the @code{isockstream}'s @code{sockbuf} so that
the user can use @code{isockstream} object as a @code{sockbuf} object.
@example
is->connect (sa); // same as is.rdbuf()->connect (sa);
@end example
@end table
@subsection osockstream Class
@cindex osockstream class
Since @code{osockstream} is publicly derived from @code{ostream}, most
of the public functions of @code{ostream} are also available in
@code{osockstream}.
@code{osockstream} redefines @code{rdbuf()} defined in its virtual base
class @code{ios}. Since, @code{ios::rdbuf()} is not virtual, care must
be taken to call the correct @code{rdbuf()} through a reference or a
pointer to an object of class @code{osockstream}.
In what follows,
@itemize @minus
@item
@code{sb} is a @code{sockbuf} object
@item
@code{sbp} is a pointer to a @code{sockbuf} object
@end itemize
@table @code
@item osockstream os(sb)
@findex osockstream::osockstream
Constructs an @code{osockstream} object @code{os} with @code{sb} as its
@code{sockbuf}.
@item osockstream os(sbp)
Constructs an @code{osockstream} object @code{os} with @code{*sbp} as its
@code{sockbuf}.
@item sbp = os.rdbuf()
@findex osockstream::rdbuf
returns a pointer to the @code{sockbuf} of the @code{osockstream} object
@code{os}.
@item osockstream::operator -> ()
@findex osockstream::operator->
returns a pointer to the @code{osockstream}'s @code{sockbuf} so that
the user can use @code{osockstream} object as a @code{sockbuf} object.
@example
os->connect (sa); // same as os.rdbuf()->connect (sa);
@end example
@end table
@subsection iosockstream Class
@cindex iosockstream class
Since @code{iosockstream} is publicly derived from @code{iostream}, most
of the public functions of @code{iostream} are also available in
@code{iosockstream}.
@code{iosockstream} redefines @code{rdbuf()} defined in its virtual base
class @code{ios}. Since, @code{ios::rdbuf()} is not virtual, care must
be taken to call the correct @code{rdbuf()} through a reference or a
pointer to an object of class @code{iosockstream}.
In what follows,
@itemize @minus
@item
@code{sb} is a @code{sockbuf} object
@item
@code{sbp} is a pointer to a @code{sockbuf} object
@end itemize
@table @code
@item iosockstream io(sb)
@findex iosockstream::iosockstream
Constructs an @code{iosockstream} object @code{io} with @code{sb} as its
@code{sockbuf}.
@item iosockstream io(sbp)
Constructs an @code{iosockstream} object @code{io} with @code{*sbp} as its
@code{sockbuf}.
@item sbp = io.rdbuf()
@findex iosockstream::rdbuf
returns a pointer to the @code{sockbuf} of the @code{iosockstream} object
@code{io}.
@item iosockstream::operator -> ()
@findex iosockstream::operator->
returns a pointer to the @code{iosockstream}'s @code{sockbuf} so that
the user can use @code{iosockstream} object as a @code{sockbuf} object.
@example
io->connect (sa); // same as io.rdbuf()->connect (sa);
@end example
@end table
@node iosockinet
@section iosockinet Stream Classes
We discus only @code{isockinet} class here. @code{osockinet} and
@code{iosockinet} are similar and are left out. However, they are
covered in the examples that follow.
@subsection isockinet
@cindex isockinet class
@cindex class isockinet
@code{isockinet} is used to handle interprocess communication in
@var{inet} domain. It is derived from @code{isockstream} class and it
uses a @code{sockinetbuf} as its stream buffer. @xref{iosockstream}, for
more details on @code{isockstream}. @xref{sockinetbuf Class}, for
information on @code{sockinetbuf}.
In what follows,
@itemize @minus
@item
@code{ty} is a @code{sockbuf::type} and must be one of
@code{sockbuf::sock_stream}, @code{sockbuf::sock_dgram},
@code{sockbuf::sock_raw}, @code{sockbuf::sock_rdm}, and
@code{sockbuf::sock_seqpacket}
@item
@code{proto} denotes the protocol number and is of type int
@item
@code{sb} is a @code{sockbuf} object and must be in @var{inet} domain
@item
@code{sinp} is a pointer to an object of @code{sockinetbuf}
@end itemize
@table @code
@item isockinet is (ty, proto)
@findex isockinet::isockinet
constructs an @code{isockinet} object @code{is} whose @code{sockinetbuf}
buffer is of the type @code{ty} and has the protocol number
@code{proto}. The default protocol number is 0.
@item isockinet is (sb)
constructs a @code{isockinet} object @code{is} whose @code{sockinetbuf}
is @code{sb}. @code{sb} must be in @var{inet} domain.
@item isockinet is (sinp)
constructs a @code{isockinet} object @code{is} whose @code{sockinetbuf}
is @code{sinp}.
@item sinp = is.rdbuf ()
@findex isockinet::rdbuf
returns a pointer to the @code{sockinetbuf} of @code{isockinet} object
@code{is}.
@item isockinet::operator ->
@findex isockinet::operator->
returns @code{sockinetbuf} of @code{sockinet} so that the @code{sockinet}
object acts as a smart pointer to @code{sockinetbuf}.
@example
is->localhost (); // same as is.rdbuf ()->localhost ();
@end example
@end table
@subsection iosockinet examples
@cindex iosockinet examples
The first pair of examples demonstrates datagram socket connections in the
@var{inet} domain. First, @code{tdinread} prints its local host and
local port on stdout and waits for input in the connection.
@code{tdinwrite} is started with the local host and local port of
@code{tdinread} as arguments. It sends the string "How do ye do!" to
@code{tdinread} which in turn reads the string and prints on its stdout.
@example
// tdinread.cc
#include <sockinet.h>
int main ()
@{
char buf[256];
isockinet is (sockbuf::sock_dgram);
is->bind ();
cout << is->localhost() << ' ' << is->localport() << endl;
is.getline (buf);
cout << buf << endl;
return 0;
@}
@end example
@example
// tdinwrite.cc--tdinwrite hostname portno
#include <sockinet.h>
#include <stdlib.h>
int main (int ac, char** av)
@{
osockinet os (sockbuf::sock_dgram);
os->connect (av[1], atoi(av[2]));
os << "How do ye do!" << endl;
return 0;
@}
@end example
The next example communicates with an nntp server through a
@code{sockbuf::sock_stream} socket connection in @var{inet} domain.
After establishing a connection to the nntp server, it sends a "HELP"
command and gets back the HELP message before sending the "QUIT"
command.
@example
// tnntp.cc
#include <sockinet.h>
int main ()
@{
char buf[1024];
iosockinet io (sockbuf::sock_stream);
io->connect ("murdoch.acc.virginia.edu", "nntp", "tcp");
io.getline (buf, 1024); cout << buf << endl;
io << "HELP\r\n" << flush;
io.getline (buf, 1024); cout << buf << endl;
while (io.getline (buf, 1024))
if (buf[0] == '.' && buf[1] == '\r') break;
else if (buf[0] == '.' && buf[1] == '.') cout << buf+1 << endl;
else cout << buf << endl;
io << "QUIT\r\n" << flush;
io.getline (buf, 1024); cout << buf << endl;
return 0;
@}
@end example
@node iosockunix
@section iosockunix Classes
@cindex iosockunix class
We discuss only @code{isockunix} here. @code{osockunix} and
@code{iosockunix} are similar.
@subsection isockunix class
@cindex isockunix class
@cindex class isockunix
@code{isockunix} is used to handle interprocess communication in
@var{unix} domain. It is derived from @code{isockstream} class and it
uses a @code{sockunixbuf} as its stream buffer. @xref{iosockstream}, for
more details on @code{isockstream}. @xref{sockunixbuf Class}, for
information on @code{sockunixbuf}.
In what follows,
@itemize @minus
@item
@code{ty} is a @code{sockbuf::type} and must be one of
@code{sockbuf::sock_stream}, @code{sockbuf::sock_dgram},
@code{sockbuf::sock_raw}, @code{sockbuf::sock_rdm}, and
@code{sockbuf::sock_seqpacket}
@item
@code{proto} denotes the protocol number and is of type int
@item
@code{sb} is a @code{sockbuf} object and must be in @var{unix} domain
@item
@code{sinp} is a pointer to an object of @code{sockunixbuf}
@end itemize
@table @code
@item isockunix is (ty, proto)
@findex isockunix::isockunix
constructs an @code{isockunix} object @code{is} whose @code{sockunixbuf}
buffer is of the type @code{ty} and has the protocol number
@code{proto}. The default protocol number is 0.
@item isockunix is (sb)
constructs a @code{isockunix} object @code{is} whose @code{sockunixbuf}
is @code{sb}. @code{sb} must be in @var{unix} domain.
@item isockunix is (sinp)
constructs a @code{isockunix} object @code{is} whose @code{sockunixbuf}
is @code{sinp}.
@item sinp = is.rdbuf ()
@findex isockunix::rdbuf
returns a pointer to the @code{sockunixbuf} of @code{isockunix} object
@code{is}.
@item isockunix::operator ->
@findex isockunix::operator->
returns @code{sockunixbuf} of @code{sockunix} so that the @code{sockunix}
object acts as a smart pointer to @code{sockunixbuf}.
@example
is->localhost (); // same as is.rdbuf ()->localhost ();
@end example
@end table
@subsection iosockunix examples
@cindex iosockunix examples
@code{tsunread} listens for connections. When @code{tsunwrite} requests
connection, @code{tsunread} accepts it and waits for input.
@code{tsunwrite} sends the string "Hello!!!" to @code{tsunread}.
@code{tsunread} reads the string sent by @code{tsunwrite} and prints on
its stdout.
@example
// tsunread.cc
#include <sockunix.h>
#include <unistd.h>
int main ()
@{
sockunixbuf sunb (sockbuf::sock_stream);
sunb.bind ("/tmp/socket+-");
sunb.listen (2);
isockunix is = sunb.accept ();
char buf[32];
is >> buf; cout << buf << endl;
unlink ("/tmp/socket+-");
return 0;
@}
@end example
@example
// tsunwrite.cc
#include <sockunix.h>
int main ()
@{
osockunix os (sockbuf::sock_stream);
os->connect ("/tmp/socket++");
os << "Hello!!!\n" << flush;
return 0;
@}
@end example
@node pipestream Classes
@chapter pipestream Classes
@cindex pipestream classes
@cindex pipestream examples
@findex popen
@findex pipe
@findex socketpair
@code{pipestream} stream classes provide the services of the @var{UNIX}
system calls @code{pipe} and @code{socketpair} and the C library
function @code{popen}. @code{ipipestream}, @code{opipestream}, and
@code{iopipestream} are obtained by simply deriving from
@code{isockstream}, @code{osockstream} and @code{iosockstream}
respectively. @xref{sockstream Classes} for details.
In what follows,
@itemize @minus
@item
@code{ip} is an @code{ipipestream} object
@item
@code{op} is an @code{opipestream} object
@item
@code{iop} is an @code{iopipestream} object
@item
@code{cmd} is a char* denoting an executable like "wc"
@item
@code{ty} is of type @code{sockbuf::type} indicating the type of the
connection
@item
@code{proto} is an @code{int} denoting a protocol number
@end itemize
@table @code
@item ipipestream ip(cmd)
@findex ipipestream::ipipestream
construct an @code{ipipestream} object @code{ip} such that the output of
the command @code{cmd} is available as input through @code{ip}.
@item opipestream op(cmd)
@findex opipestream::opipestream
construct an @code{opipestream} object @code{op} such that the input for
the command @code{cmd} can be send through @code{op}.
@item iopipestream iop(cmd)
@findex iopipestream::iopipestream
construct an @code{iopipestream} object @code{iop} such that the input
and the output to the command @code{cmd} can be sent and received
through @code{iop}.
@item iopipestream iop(ty, proto)
construct a @code{iopipestream} object @code{iop} whose socket is a
socketpair of type @code{ty} with protocol number @code{proto}.
@code{ty} defaults to @code{sockbuf::sock_stream} and @code{proto}
defaults to 0. Object @code{iop} can be used either as a @code{pipe} or
as a @code{socketpair}.
@item iop.pid ()
@findex iopipestream::pid
return the process id of the child if the current process is the parent
or return 0. If the process has not forked yet, return -1.
@item iopipestream::fork ()
@findex iopipestream::fork
@code{fork()} is a static function of class @code{iopipestream}.
@code{fork()} forks the current process and appropriately sets the
@code{cpid} field of the @code{iopipestream} objects that have not
forked yet.
@end table
@menu
* pipe Example:: How to use pipestream as pipe?
* socketpair Example:: How to use pipestream as socketpair?
* popen Example:: How to use pipestream as popen?
@end menu
@node pipe Example
@section pipestream as pipe
@cindex pipe example
@code{pipe} is used to communicate between parent and child processes in
the @var{unix} domain.
The following example illustrates how to use @code{iopipestream} class as
a @code{pipe}. The parent sends the string "I am the parent" to the
child and receives the string "I am the child" from child. The child, in
turn, receives the string "I am the parent" from parent and sends the
string "I am the child" to the parent. Note the same @code{iopipestream}
object is used for input and output in each process.
@example
#include <pipestream.h>
int main()
@{
iopipestream p;
if ( p.fork() ) @{
char buf[128];
p << "I am the parent\n" << flush;
cout << "parent: ";
while(p >> buf)
cout << buf << ' ';
cout << endl;
@}else @{
char buf[128];
p.getline(buf, 127);
cout << "child: " << buf << endl;
p << "I am the child\n" << flush;
@}
return 0;
@}
@end example
@node socketpair Example
@section pipestream as socketpair
@cindex socketpair example
Like pipes, socketpairs also allow communication between parent and
child processes. But socketpairs are more flexible than pipes in the
sense that they let the users choose the socket type and protocol.
The following example illustrates the use of @code{iopipestream} class as
a @code{socketpair} whose type is @code{sockbuf::sock_dgram}. The parent
sends the string "I am the parent" to the child and receives the string
"I am the child" from the child. The child, in turn, receives and sends
the strings "I am the parent" and "I am the child" respectively from and
to the parent. Note in the following example that the same
@code{iopipestream} object is used for both the input and the output in
each process.
@example
#include <pipestream.h>
int main()
@{
iopipestream p(sockbuf::sock_dgram);
if ( iopipestream::fork() ) @{
char buf[128];
p << "I am the parent\n" << flush;
p.getline(buf, 127);
cout << "parent: " << buf << endl;
@}else @{
char buf[128];
p.getline(buf, 127);
cout << "child: " << buf << endl;
p << "I am the child\n" << flush;
@}
return 0;
@}
@end example
@node popen Example
@section pipestream as popen
@cindex popen example
@code{popen} is used to call an executable and send inputs and
outputs to that executable. For example, the following example
executes "/bin/date", gets its output, and prints it to stdout.
@example
#include <pipestream.h>
int main ()
@{
char buf[128];
ipipestream p("/bin/date");
p.getline (buf, 127);
cout << buf << endl;
return 0;
@}
@end example
Here is an example that prints "Hello World!!" on stdout. It uses
@code{opipestream} object.
@example
#include <pipestream.h>
int main ()
@{
opipestream p("/bin/cat");
p << "Hello World!!\n" << endl;
return 0;
@}
@end example
The following example illustrates the use of @code{iopipestream} for
both input and output.
@example
#include <pipestream.h>
int main()
@{
char buf[128];
iopipestream p("lpc");
p << "help\nquit\n" << flush;
while ( p.getline(buf, 127) ) cout << buf << endl;
return 0;
@}
@end example
@node Fork Class
@chapter Fork Class
@cindex fork class
You can effectively use the @code{Fork} wrapper class to create child
processes. You can use the @code{Fork} class, instead of directly using the
system call fork (), if you desire the following:
@itemize @bullet
@item
Avoid zombie processes
@item
Optionally kill child processes when the parent process terminates.
@item
Want to know the reason for abnormal termination of child processes.
@end itemize
In what follows,
@itemize @minus
@item
@code{killchild} is an integer.
@item
@code{reason} is an integer.
@item
@code{signo} is a valid signal.
@item
@code{f} is a @code{Fork} object.
@end itemize
@table @code
@item Fork f(killchild, reason)
@findex Fork::Fork
constructs a @code{Fork} object @code{f}. The constructor creates a child
process. When the parent process terminates, it will kill the child
process if @code{killchild} is not 0. Otherwise, the parent process will
wait until all its child processes die. If @code{reason} is not 0, then
it gives the reason for a child process's death on the stderr.
@item f.is_child ()
@findex Fork::is_child
returns 1 if the current process is the child process following the
fork in constructing the @code{Fork} object @code{f}. Otherwise, return 0.
@item f.is_parent ()
@findex Fork::is_parent
returns 1 if the current process is the parent process following the
fork in constructing the @code{Fork} object @code{f}. Otherwise, return 0.
@item f.process_id ()
@findex Fork::process_id
returns the process id of the child process, if the current process
is the parent process. Returns 0, if the current process is the child
process. Returns -1, if fork failed.
@item Fork::suicide_signal (signo)
@findex Fork::suicide_signal
is a static function. Upon the reciept of the signal
@code{signo}, the current process will kill all its child processes
created through @code{Fork::Fork(int, int)} irrespective of the value of
the @code{killchild} flag used in the construction of the @code{Fork}
objects. @code{signo} defaults to SIGTERM signal.
@end table
@section Fork Example
@cindex fork example
The following example illustrates the use of the @code{Fork} class to
create child processes. First, we set up @var{SIGTERM} signal handler
to kill all the child processes, by callling @code{Fork::suicide_signal
()}. Second, we create several child and grandchild processes.
You can kill the top most parent process and all its children by sending
a @var{SIGTERM} signal to the top most parent process.
@example
// tfork.C
#include <iostream.h>
#include <Fork.h>
static void print (char* name, pid_t child)
@{
if (child)
cerr << "Parent " << getppid () << "; "
<< name << ' ' << getpid () << "; Child " << child << ";\n";
@}
int main (int ac, char** av)
@{
Fork::suicide_signal (SIGTERM);
Fork a(0, 1);
print ("a", a.process_id ());
if (a.is_child ()) @{
sleep (3000);
@} else if (a.is_parent ()) @{
Fork b (1, 1);
print ("b", b.process_id ());
@{
Fork c (b.is_parent (), 1);
if (b.is_child ())
print ("cchild", c.process_id ());
else
print ("cparent", c.process_id ());
if (c.is_child ()) @{
sleep (3000);
return 0;
@}
@}
if (b.is_child ()) @{
sleep (120);
return 0x8;
@}
@}
return 0;
@}
@end example
@node protocol Class
@chapter Class protocol
@cindex protocol class
@code{protocol} class is the base class for all the other application
protocol classes like @code{echo}, @code{smtp}, etc. @code{protocol}
is derived publicly from @code{iosockstream}. It uses @code{protocolbuf}
class, a nested class of @code{protocol}, as its stream buffer.
The @code{protocol} class is an abstract class and thus, you cannot
instantiate an object of @code{protocol}.
@section Class protocol::protocolbuf
@cindex protocolbuf class
@code{protocol::protocolbuf} class is publicly derived from
@code{sockinetbuf} and thus, it inherits all the latter's public member
functions. In addition, the @code{protocolbuf} defines the following
member functions.
In what follows,
@itemize @minus
@item
@code{p} is an object of a non-abstract class derived from
@code{protocolbuf}.
@item
@code{pname} is the transport protocol name which is either
@code{protocol::tcp} or @code{protocol::udp}.
@item
@code{addr} is an unsigned long denoting the valid address of a machine in
host byte order.
@item
@code{host} is a char string denoting the name of a machine like
"kelvin.seas.virginia.edu".
@item
@code{portno} is an int and denotes the port number in host byte order.
@end itemize
@table @code
@item protocol::protocolbuf::protocolbuf (pname)
@findex protocolbuf::protocolbuf
constructs @code{protocolbuf} object with the transport protocol set to
@code{pname}.
@item p.protocol_name ()
@findex protcolbuf::protocol_name
returns the name of the transport protocol of @code{p} as a char string.
@item p.rfc_name ()
@findex protocolbuf::rfc_name
returns the name of the application protocol name of @code{p} as a char
string. @code{protocolbuf::rfc_name ()} is a pure virtual function;
thus, any class derived from @code{protocol::protocolbuf} should provide
a definition for @code{protocolbuf::rfc_name ()}.
@item p.rfc_doc ()
@findex protocolbuf::rfc_doc
returns the RFC document name of the application protocol of
@code{p} as a char string. @code{protocolbuf::rfc_doc ()} is a pure
virtual function; thus, any class derived from
@code{protocol::protocolbuf} should provide a definition for
@code{protocolbuf::rfc_doc ()}.
@item p.serve_clients (portno)
@findex protocolbuf::serve_clients
converts @code{p} into a server. Use the port specified in
@code{/etc/services} for the application if @code{portno} < 0. Use a
default port if @code{0 <= portno <= 1024}. Otherwise, use @code{portno}
as the port to accept clients requesting service.
@code{protocolbuf::serve_clients()} is pure virtual function; thus,
any class derived from @code{protocol::protocolbuf} should provide a
definition for @code{protocolbuf::serve_clients()}.
Please do not change the meaning of @code{portno} when you derive
your own class.
@item p.bind ()
@findex protocolbuf::bind
same as @code{p.serve_clients (-1)}.
It returns 0 on success and returns the errno on failure.
@item p.connect ()
@findex protocolbuf::connect
connects to the local host's server for the application. @code{p} acts as
the client. It returns 0 on success and returns the errno on failure.
@item p.connect (addr)
connects to the server running at the machine with address, @code{addr}.
@code{p} acts as the client.
It returns 0 on success and returns the errno on failure.
@item p.connect (host)
connects to the server running at the machine, @code{host}.
@code{p} acts as the client.
It returns 0 on success and returns the errno on failure.
@item p.connect (host, portno)
connects to the server servicing clients at @code{portno} at the machine,
@code{host}. Unlike this connect call, the other variants of connect
uses the port specified in the @code{/etc/services} file.
It returns 0 on success and returns the errno on failure.
@end table
@node echo Class
@chapter Echo Class
@cindex echo class
The @code{echo} class implements RFC 862. An @code{echo} object, as a
client, will get back what ever data it sends to an @code{echo} server.
Similarly, an @code{echo} object, as a server, will echo back the data
it receives from its client.
The @code{echo} class is derived from @code{protocol} class, and uses
@code{echo::echobuf} as its stream buffer. @code{echo::echobuf} is in
turn is derived from @code{protocol::protcolbuf}.
In what follows,
@itemize @minus
@item
@code{e} is a @code{echo} object.
@item
@code{pname} is a transport protocol name and must be either
@code{protocol::tcp} or @code{protocol::udp}.
@end itemize
@table @code
@item echo e (pname)
@findex echo::echo
constructs the @code{echo} object, @code{e} with @code{pname} as its
transport protocol name.
@item echo::operator -> ()
@findex echo::operator->
an @code{echo} object is a smart pointer for the underlying
@code{echobuf}.
@end table
@section tsecho.C
@example
// echo server. Serves clients at port 4000.
#include <echo.h>
#include <stdlib.h>
int main ()
@{
echo server (protocol::tcp);
server->serve_clients (4000);
return 1;
@}
@end example
@subsection tcecho.C
@example
// echo client. Sends "mary had a litte lamb" to the server
#include <echo.h>
#include <stdlib.h>
int main ()
@{
echo e(protocol::tcp);
e->connect ("kelvin.seas.virginia.edu", 4000);
cout << e->rfc_name () << ' ' << e->rfc_doc () << endl;
e << "mary had a little lamb\r\n" << flush;
char buf [256];
e.getline (buf, 255);
cout << "got back: " << buf << endl;
return 0;
@}
@end example
@node smtp Class
@chapter SMTP Class
@cindex smtp class
The @code{smtp} class, which is derived from @code{protocol} class,
implements RFC 821. It can be used only as a client. Server function
is not yet implemented.
@code{smtp} uses @code{smtp::smtpbuf} as its underlying stream buffer.
Also, like the @code{protocol} class, @code{smtp} is a smart pointer
class for it is @code{smtp::smtpbuf}.
In what follows,
@itemize @minus
@item
@code{s} is an @code{smtp} object.
@item
@code{sb} is an @code{smtp::smtpbuf} object.
@item
@code{io} is a pointer to an @code{ostream}.
@item
@code{buf} is a char buffer of length @code{buflen}.
@item
@code{str, str0, str1, ...} are all char strings.
@end itemize
@table @code
@item smtp s (io)
@findex smtp::smtp
constructs an @code{smtp} client, @code{s}. Any response the client gets
from the server is sent to the ostream, @code{io}.
@item sb.get_response ()
@findex smtpbuf::get_response
gets the server response and sends it to @code{io} of the @code{smtpbuf}.
@item sb.send_cmd (str0, str1, str2)
@findex smtpbuf::send_cmd
concatenates strings @code{str0}, @code{str1}, and @code{str2} and sends the
concatenated string to the server before getting its response.
@item sb.send_buf (buf, buflen)
@findex smtpbuf::send_buf
sends the contents of the @code{buf} to the server.
@item sb.helo ()
@item sb.help (str)
@item sb.quit ()
@item sb.turn ()
@item sb.rset ()
@item sb.noop ()
@item sb.data ()
@item sb.vrfy (str)
@item sb.expn (str)
implements the respective @var{smtp} commands. See RFC 821 for the
meaning of each.
@item sb.mail (str)
sends the mail command to the server. @code{str} is the the reverse path
or the @var{FROM} address.
@item sb.rcpt (str)
sends the recipient command to the server. @code{str} is the forward path
or the @var{TO} address.
@item sb.data (buf, buflen)
sends the contents of the buffer, @code{buf} as the mail data to the recipient
previously established through @code{smtpbuf::rcpt()} calls.
@item sb.data (filename)
sends the contents of the file, @code{filename} as the mail data to the
recipient previously established through @code{smtpbuf::rcpt()} calls.
@end table
@section tcsmtp.C
@example
// smtp client.
// The president sends a message to gs4t@@virginia.edu.
#include <smtp.h>
#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
int main ()
@{
smtp client (&cout);
// establish connection
client->connect ("fulton.seas.virginia.edu");
client->helo ();
// get help
client->help ();
// setup the FROM address
client->mail ("president@@whitehouse.gov");
// setup the TO address
client->rcpt ("gs4t@@virginia.edu");
// send the message
client->data ();
client << "Hi Sekar, I appoint you as the director of NASA\r\n" << flush;
client << " -Bill, Hill, and Chel\r\n" << flush;
cout << client; // get the server response.
// finally quit
client->quit ();
return 0;
@}
@end example
@node Error Handling
@chapter Error Handling
@cindex error handling
Each class in the Socket++ library uses @code{error(const char*)} member
function to report any errors that may occur during a system call. It
first calls @code{perror()} to report the error message for the
@code{errno} set by the system call. It then calls
@code{sock_error (const char* nm, const char* errmsg)}
where @code{nm} is the name of the class.
The @code{sock_error()} function simply prints the @code{nm} and the
@code{errmsg} on the @var{stderr}.
@node Pitfalls
@chapter Pitfalls
@cindex pitfalls
@cindex common mistakes
Deadlocks in datagram sockets are the most common mistakes that novices
make. To alleviate the problem, @code{sockbuf} class provides timeout
facilities that can be used effectively to avoid deadlocks.
Consider the following simple tsmtp example which sends the HELP command to a
smtp server and gets back the help message. Suppose it does not know
the size of the help message nor the format of the message. In such
cases, the timeout facilities of @code{sockbuf} class provides the
required tools.
The example terminates the help message reception if the there is no
input activity from the smtp server for 10 seconds.
@subheading tsmtp.cc
@example
@cindex timeout example
#include <sockinet.h>
int main()
@{
iosockinet sio(sockbuf::sock_stream);
sio->connect("kelvin.seas.virginia.edu", "smtp", "tcp");
char buf[512];
sio.getline(buf, 511); cout << buf << endl;
sio << "HELO kelvin\n" << flush;
sio.getline(buf, 511); cout << buf << endl;
sio << "HELP\n" << flush;
// set the receive timeout to 10 seconds
int tmo = sio->recvtimeout(10);
while ( sio.getline(buf, 511) ) cout << buf << endl;
// if the above while loop terminated due to timeout
// clear the state of sio.
if ( !sio->is_eof() )
sio.clear();
sio->recvtimeout(tmo); // reset the receive timeout time
sio << "QUIT\n" << flush;
sio.getline(buf, 511); cout << buf << endl;
return 0;
@}
@end example
@node Index
@unnumbered Index
@printindex cp
@contents
@bye
|