This file is indexed.

/usr/share/doc/glibc-doc/html/libc_16.html is in glibc-doc 2.15-0ubuntu10.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
<html>
<!-- This file documents the GNU C library.

This is Edition 0.13, last updated 2011-07-19,
of The GNU C Library Reference Manual, for version
2.14 (Ubuntu EGLIBC 2.15-0ubuntu10) .

Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010, 2011 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Free Software Needs Free Documentation"
and "GNU Lesser General Public License", the Front-Cover texts being
"A GNU Manual", and with the Back-Cover Texts as in (a) below.  A
copy of the license is included in the section entitled "GNU Free
Documentation License".

(a) The FSF's Back-Cover Text is: "You have the freedom to
copy and modify this GNU manual.  Buying copies from the FSF
supports it in developing GNU and promoting software freedom."
 -->
<!-- Created on April 20, 2012 by texi2html 1.82
texi2html was written by: 
            Lionel Cons <Lionel.Cons@cern.ch> (original author)
            Karl Berry  <karl@freefriends.org>
            Olaf Bachmann <obachman@mathematik.uni-kl.de>
            and many others.
Maintained by: Many creative people.
Send bugs and suggestions to <texi2html-bug@nongnu.org>
-->
<head>
<title>The GNU C Library: 16. Sockets</title>

<meta name="description" content="The GNU C Library: 16. Sockets">
<meta name="keywords" content="The GNU C Library: 16. Sockets">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2html 1.82">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
pre.display {font-family: serif}
pre.format {font-family: serif}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: serif; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: serif; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.roman {font-family:serif; font-weight:normal;}
span.sansserif {font-family:sans-serif; font-weight:normal;}
ul.toc {list-style: none}
-->
</style>


</head>

<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">

<a name="Sockets"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="libc_15.html#Pipe-Atomicity" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Socket-Concepts" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc_15.html#Pipes-and-FIFOs" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Sockets-1"></a>
<h1 class="chapter">16. Sockets</h1>

<p>This chapter describes the GNU facilities for interprocess
communication using sockets.
</p>
<a name="index-socket"></a>
<a name="index-interprocess-communication_002c-with-sockets"></a>
<p>A <em>socket</em> is a generalized interprocess communication channel.
Like a pipe, a socket is represented as a file descriptor.  Unlike pipes
sockets support communication between unrelated processes, and even
between processes running on different machines that communicate over a
network.  Sockets are the primary means of communicating with other
machines; <code>telnet</code>, <code>rlogin</code>, <code>ftp</code>, <code>talk</code> and the
other familiar network programs use sockets.
</p>
<p>Not all operating systems support sockets.  In the GNU library, the
header file &lsquo;<tt>sys/socket.h</tt>&rsquo; exists regardless of the operating
system, and the socket functions always exist, but if the system does
not really support sockets these functions always fail.
</p>
<p><strong>Incomplete:</strong> We do not currently document the facilities for
broadcast messages or for configuring Internet interfaces.  The
reentrant functions and some newer functions that are related to IPv6
aren&rsquo;t documented either so far.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Socket-Concepts">16.1 Socket Concepts</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">	Basic concepts you need to know about.
</td></tr>
<tr><td align="left" valign="top"><a href="#Communication-Styles">16.2 Communication Styles</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Stream communication, datagrams and other styles.
</td></tr>
<tr><td align="left" valign="top"><a href="#Socket-Addresses">16.3 Socket Addresses</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">	How socket names (&ldquo;addresses&rdquo;) work.
</td></tr>
<tr><td align="left" valign="top"><a href="#Interface-Naming">16.4 Interface Naming</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">	Identifying specific network interfaces.
</td></tr>
<tr><td align="left" valign="top"><a href="#Local-Namespace">16.5 The Local Namespace</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">	Details about the local namespace.
</td></tr>
<tr><td align="left" valign="top"><a href="#Internet-Namespace">16.6 The Internet Namespace</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">	Details about the Internet namespace.
</td></tr>
<tr><td align="left" valign="top"><a href="#Misc-Namespaces">16.7 Other Namespaces</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">	Other namespaces not documented fully here.
</td></tr>
<tr><td align="left" valign="top"><a href="#Open_002fClose-Sockets">16.8 Opening and Closing Sockets</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">  Creating sockets and destroying them.
</td></tr>
<tr><td align="left" valign="top"><a href="#Connections">16.9 Using Sockets with Connections</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">		Operations on sockets with connection state.
</td></tr>
<tr><td align="left" valign="top"><a href="#Datagrams">16.10 Datagram Socket Operations</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">		Operations on datagram sockets.
</td></tr>
<tr><td align="left" valign="top"><a href="#Inetd">16.11 The <code>inetd</code> Daemon</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">		Inetd is a daemon that starts servers on request.
			   The most convenient way to write a server
			   is to make it work with Inetd.
</td></tr>
<tr><td align="left" valign="top"><a href="#Socket-Options">16.12 Socket Options</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">	Miscellaneous low-level socket options.
</td></tr>
<tr><td align="left" valign="top"><a href="#Networks-Database">16.13 Networks Database</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">   Accessing the database of network names.
</td></tr>
</table>

<hr size="6">
<a name="Socket-Concepts"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Sockets" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Communication-Styles" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Sockets" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Socket-Concepts-1"></a>
<h2 class="section">16.1 Socket Concepts</h2>

<a name="index-communication-style-_0028of-a-socket_0029"></a>
<a name="index-style-of-communication-_0028of-a-socket_0029"></a>
<p>When you create a socket, you must specify the style of communication
you want to use and the type of protocol that should implement it.
The <em>communication style</em> of a socket defines the user-level
semantics of sending and receiving data on the socket.  Choosing a
communication style specifies the answers to questions such as these:
</p>
<ul>
<li>
<a name="index-packet"></a>
<a name="index-byte-stream"></a>
<a name="index-stream-_0028sockets_0029"></a>
<strong>What are the units of data transmission?</strong>  Some communication
styles regard the data as a sequence of bytes with no larger
structure; others group the bytes into records (which are known in
this context as <em>packets</em>).

</li><li>
<a name="index-loss-of-data-on-sockets"></a>
<a name="index-data-loss-on-sockets"></a>
<strong>Can data be lost during normal operation?</strong>  Some communication
styles guarantee that all the data sent arrives in the order it was
sent (barring system or network crashes); other styles occasionally
lose data as a normal part of operation, and may sometimes deliver
packets more than once or in the wrong order.

<p>Designing a program to use unreliable communication styles usually
involves taking precautions to detect lost or misordered packets and
to retransmit data as needed.
</p>
</li><li>
<strong>Is communication entirely with one partner?</strong>  Some
communication styles are like a telephone call&mdash;you make a
<em>connection</em> with one remote socket and then exchange data
freely.  Other styles are like mailing letters&mdash;you specify a
destination address for each message you send.
</li></ul>

<a name="index-namespace-_0028of-socket_0029"></a>
<a name="index-domain-_0028of-socket_0029"></a>
<a name="index-socket-namespace"></a>
<a name="index-socket-domain"></a>
<p>You must also choose a <em>namespace</em> for naming the socket.  A socket
name (&ldquo;address&rdquo;) is meaningful only in the context of a particular
namespace.  In fact, even the data type to use for a socket name may
depend on the namespace.  Namespaces are also called &ldquo;domains&rdquo;, but we
avoid that word as it can be confused with other usage of the same
term.  Each namespace has a symbolic name that starts with &lsquo;<samp>PF_</samp>&rsquo;.
A corresponding symbolic name starting with &lsquo;<samp>AF_</samp>&rsquo; designates the
address format for that namespace.
</p>
<a name="index-network-protocol"></a>
<a name="index-protocol-_0028of-socket_0029"></a>
<a name="index-socket-protocol"></a>
<a name="index-protocol-family"></a>
<p>Finally you must choose the <em>protocol</em> to carry out the
communication.  The protocol determines what low-level mechanism is used
to transmit and receive data.  Each protocol is valid for a particular
namespace and communication style; a namespace is sometimes called a
<em>protocol family</em> because of this, which is why the namespace names
start with &lsquo;<samp>PF_</samp>&rsquo;.
</p>
<p>The rules of a protocol apply to the data passing between two programs,
perhaps on different computers; most of these rules are handled by the
operating system and you need not know about them.  What you do need to
know about protocols is this:
</p>
<ul>
<li>
In order to have communication between two sockets, they must specify
the <em>same</em> protocol.

</li><li>
Each protocol is meaningful with particular style/namespace
combinations and cannot be used with inappropriate combinations.  For
example, the TCP protocol fits only the byte stream style of
communication and the Internet namespace.

</li><li>
For each combination of style and namespace there is a <em>default
protocol</em>, which you can request by specifying 0 as the protocol
number.  And that&rsquo;s what you should normally do&mdash;use the default.
</li></ul>

<p>Throughout the following description at various places
variables/parameters to denote sizes are required.  And here the trouble
starts.  In the first implementations the type of these variables was
simply <code>int</code>.  On most machines at that time an <code>int</code> was 32
bits wide, which created a <em>de facto</em> standard requiring 32-bit
variables.  This is important since references to variables of this type
are passed to the kernel.
</p>
<p>Then the POSIX people came and unified the interface with the words &quot;all
size values are of type <code>size_t</code>&quot;.  On 64-bit machines
<code>size_t</code> is 64 bits wide, so pointers to variables were no longer
possible.
</p>
<p>The Unix98 specification provides a solution by introducing a type
<code>socklen_t</code>.  This type is used in all of the cases that POSIX
changed to use <code>size_t</code>.  The only requirement of this type is that
it be an unsigned type of at least 32 bits.  Therefore, implementations
which require that references to 32-bit variables be passed can be as
happy as implementations which use 64-bit values.
</p>

<hr size="6">
<a name="Communication-Styles"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Socket-Concepts" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Socket-Addresses" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Sockets" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Communication-Styles-1"></a>
<h2 class="section">16.2 Communication Styles</h2>

<p>The GNU library includes support for several different kinds of sockets,
each with different characteristics.  This section describes the
supported socket types.  The symbolic constants listed here are
defined in &lsquo;<tt>sys/socket.h</tt>&rsquo;.
<a name="index-sys_002fsocket_002eh"></a>
</p>
<dl>
<dt><a name="index-SOCK_005fSTREAM"></a><u>Macro:</u> int <b>SOCK_STREAM</b></dt>
<dd><p>The <code>SOCK_STREAM</code> style is like a pipe (see section <a href="libc_15.html#Pipes-and-FIFOs">Pipes and FIFOs</a>).
It operates over a connection with a particular remote socket and
transmits data reliably as a stream of bytes.
</p>
<p>Use of this style is covered in detail in <a href="#Connections">Using Sockets with Connections</a>.
</p></dd></dl>

<dl>
<dt><a name="index-SOCK_005fDGRAM"></a><u>Macro:</u> int <b>SOCK_DGRAM</b></dt>
<dd><p>The <code>SOCK_DGRAM</code> style is used for sending
individually-addressed packets unreliably.
It is the diametrical opposite of <code>SOCK_STREAM</code>.
</p>
<p>Each time you write data to a socket of this kind, that data becomes
one packet.  Since <code>SOCK_DGRAM</code> sockets do not have connections,
you must specify the recipient address with each packet.
</p>
<p>The only guarantee that the system makes about your requests to
transmit data is that it will try its best to deliver each packet you
send.  It may succeed with the sixth packet after failing with the
fourth and fifth packets; the seventh packet may arrive before the
sixth, and may arrive a second time after the sixth.
</p>
<p>The typical use for <code>SOCK_DGRAM</code> is in situations where it is
acceptable to simply re-send a packet if no response is seen in a
reasonable amount of time.
</p>
<p>See section <a href="#Datagrams">Datagram Socket Operations</a>, for detailed information about how to use datagram
sockets.
</p></dd></dl>



<dl>
<dt><a name="index-SOCK_005fRAW"></a><u>Macro:</u> int <b>SOCK_RAW</b></dt>
<dd><p>This style provides access to low-level network protocols and
interfaces.  Ordinary user programs usually have no need to use this
style.
</p></dd></dl>

<hr size="6">
<a name="Socket-Addresses"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Communication-Styles" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Address-Formats" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Sockets" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Socket-Addresses-1"></a>
<h2 class="section">16.3 Socket Addresses</h2>

<a name="index-address-of-socket"></a>
<a name="index-name-of-socket"></a>
<a name="index-binding-a-socket-address"></a>
<a name="index-socket-address-_0028name_0029-binding"></a>
<p>The name of a socket is normally called an <em>address</em>.  The
functions and symbols for dealing with socket addresses were named
inconsistently, sometimes using the term &ldquo;name&rdquo; and sometimes using
&ldquo;address&rdquo;.  You can regard these terms as synonymous where sockets
are concerned.
</p>
<p>A socket newly created with the <code>socket</code> function has no
address.  Other processes can find it for communication only if you
give it an address.  We call this <em>binding</em> the address to the
socket, and the way to do it is with the <code>bind</code> function.
</p>
<p>You need be concerned with the address of a socket if other processes
are to find it and start communicating with it.  You can specify an
address for other sockets, but this is usually pointless; the first time
you send data from a socket, or use it to initiate a connection, the
system assigns an address automatically if you have not specified one.
</p>
<p>Occasionally a client needs to specify an address because the server
discriminates based on address; for example, the rsh and rlogin
protocols look at the client&rsquo;s socket address and only bypass password
checking if it is less than <code>IPPORT_RESERVED</code> (see section <a href="#Ports">Internet Ports</a>).
</p>
<p>The details of socket addresses vary depending on what namespace you are
using.  See section <a href="#Local-Namespace">The Local Namespace</a>, or <a href="#Internet-Namespace">The Internet Namespace</a>, for specific
information.
</p>
<p>Regardless of the namespace, you use the same functions <code>bind</code> and
<code>getsockname</code> to set and examine a socket&rsquo;s address.  These
functions use a phony data type, <code>struct sockaddr *</code>, to accept the
address.  In practice, the address lives in a structure of some other
data type appropriate to the address format you are using, but you cast
its address to <code>struct sockaddr *</code> when you pass it to
<code>bind</code>.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Address-Formats">16.3.1 Address Formats</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">		About <code>struct sockaddr</code>.
</td></tr>
<tr><td align="left" valign="top"><a href="#Setting-Address">16.3.2 Setting the Address of a Socket</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">		Binding an address to a socket.
</td></tr>
<tr><td align="left" valign="top"><a href="#Reading-Address">16.3.3 Reading the Address of a Socket</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">		Reading the address of a socket.
</td></tr>
</table>

<hr size="6">
<a name="Address-Formats"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Socket-Addresses" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Setting-Address" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Socket-Addresses" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Address-Formats-1"></a>
<h3 class="subsection">16.3.1 Address Formats</h3>

<p>The functions <code>bind</code> and <code>getsockname</code> use the generic data
type <code>struct sockaddr *</code> to represent a pointer to a socket
address.  You can&rsquo;t use this data type effectively to interpret an
address or construct one; for that, you must use the proper data type
for the socket&rsquo;s namespace.
</p>
<p>Thus, the usual practice is to construct an address of the proper
namespace-specific type, then cast a pointer to <code>struct sockaddr *</code>
when you call <code>bind</code> or <code>getsockname</code>.
</p>
<p>The one piece of information that you can get from the <code>struct
sockaddr</code> data type is the <em>address format designator</em>.  This tells
you which data type to use to understand the address fully.
</p>
<a name="index-sys_002fsocket_002eh-1"></a>
<p>The symbols in this section are defined in the header file
&lsquo;<tt>sys/socket.h</tt>&rsquo;.
</p>
<dl>
<dt><a name="index-struct-sockaddr"></a><u>Data Type:</u> <b>struct sockaddr</b></dt>
<dd><p>The <code>struct sockaddr</code> type itself has the following members:
</p>
<dl compact="compact">
<dt> <code>short int sa_family</code></dt>
<dd><p>This is the code for the address format of this address.  It
identifies the format of the data which follows.
</p>
</dd>
<dt> <code>char sa_data[14]</code></dt>
<dd><p>This is the actual socket address data, which is format-dependent.  Its
length also depends on the format, and may well be more than 14.  The
length 14 of <code>sa_data</code> is essentially arbitrary.
</p></dd>
</dl>
</dd></dl>

<p>Each address format has a symbolic name which starts with &lsquo;<samp>AF_</samp>&rsquo;.
Each of them corresponds to a &lsquo;<samp>PF_</samp>&rsquo; symbol which designates the
corresponding namespace.  Here is a list of address format names:
</p>
<dl compact="compact">
<dt> <code>AF_LOCAL</code></dt>
<dd><a name="index-AF_005fLOCAL"></a>
<p>This designates the address format that goes with the local namespace.
(<code>PF_LOCAL</code> is the name of that namespace.)  See section <a href="#Local-Namespace-Details">Details of Local Namespace</a>, for information about this address format.
</p>
</dd>
<dt> <code>AF_UNIX</code></dt>
<dd><a name="index-AF_005fUNIX"></a>
<p>This is a synonym for <code>AF_LOCAL</code>.  Although <code>AF_LOCAL</code> is
mandated by POSIX.1g, <code>AF_UNIX</code> is portable to more systems.
<code>AF_UNIX</code> was the traditional name stemming from BSD, so even most
POSIX systems support it.  It is also the name of choice in the Unix98
specification. (The same is true for <code>PF_UNIX</code>
vs. <code>PF_LOCAL</code>).
</p>
</dd>
<dt> <code>AF_FILE</code></dt>
<dd><a name="index-AF_005fFILE"></a>
<p>This is another synonym for <code>AF_LOCAL</code>, for compatibility.
(<code>PF_FILE</code> is likewise a synonym for <code>PF_LOCAL</code>.)
</p>
</dd>
<dt> <code>AF_INET</code></dt>
<dd><a name="index-AF_005fINET"></a>
<p>This designates the address format that goes with the Internet
namespace.  (<code>PF_INET</code> is the name of that namespace.)
See section <a href="#Internet-Address-Formats">Internet Socket Address Formats</a>.
</p>
</dd>
<dt> <code>AF_INET6</code></dt>
<dd><p>This is similar to <code>AF_INET</code>, but refers to the IPv6 protocol.
(<code>PF_INET6</code> is the name of the corresponding namespace.)
</p>
</dd>
<dt> <code>AF_UNSPEC</code></dt>
<dd><a name="index-AF_005fUNSPEC"></a>
<p>This designates no particular address format.  It is used only in rare
cases, such as to clear out the default destination address of a
&ldquo;connected&rdquo; datagram socket.  See section <a href="#Sending-Datagrams">Sending Datagrams</a>.
</p>
<p>The corresponding namespace designator symbol <code>PF_UNSPEC</code> exists
for completeness, but there is no reason to use it in a program.
</p></dd>
</dl>

<p>&lsquo;<tt>sys/socket.h</tt>&rsquo; defines symbols starting with &lsquo;<samp>AF_</samp>&rsquo; for many
different kinds of networks, most or all of which are not actually
implemented.  We will document those that really work as we receive
information about how to use them.
</p>
<hr size="6">
<a name="Setting-Address"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Address-Formats" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Reading-Address" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Socket-Addresses" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Setting-the-Address-of-a-Socket"></a>
<h3 class="subsection">16.3.2 Setting the Address of a Socket</h3>

<a name="index-sys_002fsocket_002eh-2"></a>
<p>Use the <code>bind</code> function to assign an address to a socket.  The
prototype for <code>bind</code> is in the header file &lsquo;<tt>sys/socket.h</tt>&rsquo;.
For examples of use, see <a href="#Local-Socket-Example">Example of Local-Namespace Sockets</a>, or see <a href="#Inet-Example">Internet Socket Example</a>.
</p>
<dl>
<dt><a name="index-bind"></a><u>Function:</u> int <b>bind</b><i> (int <var>socket</var>, struct sockaddr *<var>addr</var>, socklen_t <var>length</var>)</i></dt>
<dd><p>The <code>bind</code> function assigns an address to the socket
<var>socket</var>.  The <var>addr</var> and <var>length</var> arguments specify the
address; the detailed format of the address depends on the namespace.
The first part of the address is always the format designator, which
specifies a namespace, and says that the address is in the format of
that namespace.
</p>
<p>The return value is <code>0</code> on success and <code>-1</code> on failure.  The
following <code>errno</code> error conditions are defined for this function:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The <var>socket</var> argument is not a valid file descriptor.
</p>
</dd>
<dt> <code>ENOTSOCK</code></dt>
<dd><p>The descriptor <var>socket</var> is not a socket.
</p>
</dd>
<dt> <code>EADDRNOTAVAIL</code></dt>
<dd><p>The specified address is not available on this machine.
</p>
</dd>
<dt> <code>EADDRINUSE</code></dt>
<dd><p>Some other socket is already using the specified address.
</p>
</dd>
<dt> <code>EINVAL</code></dt>
<dd><p>The socket <var>socket</var> already has an address.
</p>
</dd>
<dt> <code>EACCES</code></dt>
<dd><p>You do not have permission to access the requested address.  (In the
Internet domain, only the super-user is allowed to specify a port number
in the range 0 through <code>IPPORT_RESERVED</code> minus one; see
<a href="#Ports">Internet Ports</a>.)
</p></dd>
</dl>

<p>Additional conditions may be possible depending on the particular namespace
of the socket.
</p></dd></dl>

<hr size="6">
<a name="Reading-Address"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Setting-Address" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Interface-Naming" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Socket-Addresses" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Reading-the-Address-of-a-Socket"></a>
<h3 class="subsection">16.3.3 Reading the Address of a Socket</h3>

<a name="index-sys_002fsocket_002eh-3"></a>
<p>Use the function <code>getsockname</code> to examine the address of an
Internet socket.  The prototype for this function is in the header file
&lsquo;<tt>sys/socket.h</tt>&rsquo;.
</p>
<dl>
<dt><a name="index-getsockname"></a><u>Function:</u> int <b>getsockname</b><i> (int <var>socket</var>, struct sockaddr *<var>addr</var>, socklen_t *<var>length-ptr</var>)</i></dt>
<dd><p>The <code>getsockname</code> function returns information about the
address of the socket <var>socket</var> in the locations specified by the
<var>addr</var> and <var>length-ptr</var> arguments.  Note that the
<var>length-ptr</var> is a pointer; you should initialize it to be the
allocation size of <var>addr</var>, and on return it contains the actual
size of the address data.
</p>
<p>The format of the address data depends on the socket namespace.  The
length of the information is usually fixed for a given namespace, so
normally you can know exactly how much space is needed and can provide
that much.  The usual practice is to allocate a place for the value
using the proper data type for the socket&rsquo;s namespace, then cast its
address to <code>struct sockaddr *</code> to pass it to <code>getsockname</code>.
</p>
<p>The return value is <code>0</code> on success and <code>-1</code> on error.  The
following <code>errno</code> error conditions are defined for this function:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The <var>socket</var> argument is not a valid file descriptor.
</p>
</dd>
<dt> <code>ENOTSOCK</code></dt>
<dd><p>The descriptor <var>socket</var> is not a socket.
</p>
</dd>
<dt> <code>ENOBUFS</code></dt>
<dd><p>There are not enough internal buffers available for the operation.
</p></dd>
</dl>
</dd></dl>

<p>You can&rsquo;t read the address of a socket in the file namespace.  This is
consistent with the rest of the system; in general, there&rsquo;s no way to
find a file&rsquo;s name from a descriptor for that file.
</p>
<hr size="6">
<a name="Interface-Naming"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Reading-Address" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Local-Namespace" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Sockets" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Interface-Naming-1"></a>
<h2 class="section">16.4 Interface Naming</h2>

<p>Each network interface has a name.  This usually consists of a few
letters that relate to the type of interface, which may be followed by a
number if there is more than one interface of that type.  Examples
might be <code>lo</code> (the loopback interface) and <code>eth0</code> (the first
Ethernet interface).
</p>
<p>Although such names are convenient for humans, it would be clumsy to
have to use them whenever a program needs to refer to an interface.  In
such situations an interface is referred to by its <em>index</em>, which is
an arbitrarily-assigned small positive integer.
</p>
<p>The following functions, constants and data types are declared in the
header file &lsquo;<tt>net/if.h</tt>&rsquo;.
</p>
<dl>
<dt><a name="index-IFNAMSIZ"></a><u>Constant:</u> size_t <b>IFNAMSIZ</b></dt>
<dd><p>This constant defines the maximum buffer size needed to hold an
interface name, including its terminating zero byte.
</p></dd></dl>

<dl>
<dt><a name="index-if_005fnametoindex"></a><u>Function:</u> unsigned int <b>if_nametoindex</b><i> (const char *ifname)</i></dt>
<dd><p>This function yields the interface index corresponding to a particular
name.  If no interface exists with the name given, it returns 0.
</p></dd></dl>

<dl>
<dt><a name="index-if_005findextoname"></a><u>Function:</u> char * <b>if_indextoname</b><i> (unsigned int ifindex, char *ifname)</i></dt>
<dd><p>This function maps an interface index to its corresponding name.  The
returned name is placed in the buffer pointed to by <code>ifname</code>, which
must be at least <code>IFNAMSIZ</code> bytes in length.  If the index was
invalid, the function&rsquo;s return value is a null pointer, otherwise it is
<code>ifname</code>.
</p></dd></dl>

<dl>
<dt><a name="index-struct-if_005fnameindex"></a><u>Data Type:</u> <b>struct if_nameindex</b></dt>
<dd><p>This data type is used to hold the information about a single
interface.  It has the following members:
</p>
<dl compact="compact">
<dt> <code>unsigned int if_index;</code></dt>
<dd><p>This is the interface index.
</p>
</dd>
<dt> <code>char *if_name</code></dt>
<dd><p>This is the null-terminated index name.
</p>
</dd>
</dl>
</dd></dl>

<dl>
<dt><a name="index-if_005fnameindex"></a><u>Function:</u> struct if_nameindex * <b>if_nameindex</b><i> (void)</i></dt>
<dd><p>This function returns an array of <code>if_nameindex</code> structures, one
for every interface that is present.  The end of the list is indicated
by a structure with an interface of 0 and a null name pointer.  If an
error occurs, this function returns a null pointer.
</p>
<p>The returned structure must be freed with <code>if_freenameindex</code> after
use.
</p></dd></dl>

<dl>
<dt><a name="index-if_005ffreenameindex"></a><u>Function:</u> void <b>if_freenameindex</b><i> (struct if_nameindex *ptr)</i></dt>
<dd><p>This function frees the structure returned by an earlier call to
<code>if_nameindex</code>.
</p></dd></dl>

<hr size="6">
<a name="Local-Namespace"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Interface-Naming" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Local-Namespace-Concepts" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Sockets" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="The-Local-Namespace"></a>
<h2 class="section">16.5 The Local Namespace</h2>
<a name="index-local-namespace_002c-for-sockets"></a>

<p>This section describes the details of the local namespace, whose
symbolic name (required when you create a socket) is <code>PF_LOCAL</code>.
The local namespace is also known as &ldquo;Unix domain sockets&rdquo;.  Another
name is file namespace since socket addresses are normally implemented
as file names.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Local-Namespace-Concepts">16.5.1 Local Namespace Concepts</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top"> What you need to understand.
</td></tr>
<tr><td align="left" valign="top"><a href="#Local-Namespace-Details">16.5.2 Details of Local Namespace</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">   Address format, symbolic names, etc.
</td></tr>
<tr><td align="left" valign="top"><a href="#Local-Socket-Example">16.5.3 Example of Local-Namespace Sockets</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">      Example of creating a socket.
</td></tr>
</table>

<hr size="6">
<a name="Local-Namespace-Concepts"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Local-Namespace" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Local-Namespace-Details" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Local-Namespace" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Local-Namespace-Concepts-1"></a>
<h3 class="subsection">16.5.1 Local Namespace Concepts</h3>

<p>In the local namespace socket addresses are file names.  You can specify
any file name you want as the address of the socket, but you must have
write permission on the directory containing it.
It&rsquo;s common to put these files in the &lsquo;<tt>/tmp</tt>&rsquo; directory.
</p>
<p>One peculiarity of the local namespace is that the name is only used
when opening the connection; once open the address is not meaningful and
may not exist.
</p>
<p>Another peculiarity is that you cannot connect to such a socket from
another machine&ndash;not even if the other machine shares the file system
which contains the name of the socket.  You can see the socket in a
directory listing, but connecting to it never succeeds.  Some programs
take advantage of this, such as by asking the client to send its own
process ID, and using the process IDs to distinguish between clients.
However, we recommend you not use this method in protocols you design,
as we might someday permit connections from other machines that mount
the same file systems.  Instead, send each new client an identifying
number if you want it to have one.
</p>
<p>After you close a socket in the local namespace, you should delete the
file name from the file system.  Use <code>unlink</code> or <code>remove</code> to
do this; see <a href="libc_14.html#Deleting-Files">Deleting Files</a>.
</p>
<p>The local namespace supports just one protocol for any communication
style; it is protocol number <code>0</code>.
</p>
<hr size="6">
<a name="Local-Namespace-Details"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Local-Namespace-Concepts" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Local-Socket-Example" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Local-Namespace" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Details-of-Local-Namespace"></a>
<h3 class="subsection">16.5.2 Details of Local Namespace</h3>

<a name="index-sys_002fsocket_002eh-4"></a>
<p>To create a socket in the local namespace, use the constant
<code>PF_LOCAL</code> as the <var>namespace</var> argument to <code>socket</code> or
<code>socketpair</code>.  This constant is defined in &lsquo;<tt>sys/socket.h</tt>&rsquo;.
</p>
<dl>
<dt><a name="index-PF_005fLOCAL"></a><u>Macro:</u> int <b>PF_LOCAL</b></dt>
<dd><p>This designates the local namespace, in which socket addresses are local
names, and its associated family of protocols.  <code>PF_Local</code> is the
macro used by Posix.1g.
</p></dd></dl>

<dl>
<dt><a name="index-PF_005fUNIX"></a><u>Macro:</u> int <b>PF_UNIX</b></dt>
<dd><p>This is a synonym for <code>PF_LOCAL</code>, for compatibility&rsquo;s sake.
</p></dd></dl>

<dl>
<dt><a name="index-PF_005fFILE"></a><u>Macro:</u> int <b>PF_FILE</b></dt>
<dd><p>This is a synonym for <code>PF_LOCAL</code>, for compatibility&rsquo;s sake.
</p></dd></dl>

<p>The structure for specifying socket names in the local namespace is
defined in the header file &lsquo;<tt>sys/un.h</tt>&rsquo;:
<a name="index-sys_002fun_002eh"></a>
</p>
<dl>
<dt><a name="index-struct-sockaddr_005fun"></a><u>Data Type:</u> <b>struct sockaddr_un</b></dt>
<dd><p>This structure is used to specify local namespace socket addresses.  It has
the following members:
</p>
<dl compact="compact">
<dt> <code>short int sun_family</code></dt>
<dd><p>This identifies the address family or format of the socket address.
You should store the value <code>AF_LOCAL</code> to designate the local
namespace.  See section <a href="#Socket-Addresses">Socket Addresses</a>.
</p>
</dd>
<dt> <code>char sun_path[108]</code></dt>
<dd><p>This is the file name to use.
</p>
<p><strong>Incomplete:</strong>  Why is 108 a magic number?  RMS suggests making
this a zero-length array and tweaking the following example to use
<code>alloca</code> to allocate an appropriate amount of storage based on
the length of the filename.
</p></dd>
</dl>
</dd></dl>

<p>You should compute the <var>length</var> parameter for a socket address in
the local namespace as the sum of the size of the <code>sun_family</code>
component and the string length (<em>not</em> the allocation size!) of
the file name string.  This can be done using the macro <code>SUN_LEN</code>:
</p>
<dl>
<dt><a name="index-SUN_005fLEN"></a><u>Macro:</u> int <b>SUN_LEN</b><i> (<em>struct sockaddr_un *</em> <var>ptr</var>)</i></dt>
<dd><p>The macro computes the length of socket address in the local namespace.
</p></dd></dl>

<hr size="6">
<a name="Local-Socket-Example"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Local-Namespace-Details" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Internet-Namespace" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Local-Namespace" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Example-of-Local_002dNamespace-Sockets"></a>
<h3 class="subsection">16.5.3 Example of Local-Namespace Sockets</h3>

<p>Here is an example showing how to create and name a socket in the local
namespace.
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">#include &lt;stddef.h&gt;
#include &lt;stdio.h&gt;
#include &lt;errno.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;sys/un.h&gt;

int
make_named_socket (const char *filename)
{
  struct sockaddr_un name;
  int sock;
  size_t size;

  /* <span class="roman">Create the socket.</span> */
  sock = socket (PF_LOCAL, SOCK_DGRAM, 0);
  if (sock &lt; 0)
    {
      perror (&quot;socket&quot;);
      exit (EXIT_FAILURE);
    }

  /* <span class="roman">Bind a name to the socket.</span> */
  name.sun_family = AF_LOCAL;
  strncpy (name.sun_path, filename, sizeof (name.sun_path));
  name.sun_path[sizeof (name.sun_path) - 1] = '\0';

  /* <span class="roman">The size of the address is
     the offset of the start of the filename,
     plus its length,
     plus one for the terminating null byte.
     Alternatively you can just do:
     size = SUN_LEN (&amp;name);
</span> */
  size = (offsetof (struct sockaddr_un, sun_path)
          + strlen (name.sun_path) + 1);

  if (bind (sock, (struct sockaddr *) &amp;name, size) &lt; 0)
    {
      perror (&quot;bind&quot;);
      exit (EXIT_FAILURE);
    }

  return sock;
}
</pre></td></tr></table>

<hr size="6">
<a name="Internet-Namespace"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Local-Socket-Example" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Internet-Address-Formats" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Sockets" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="The-Internet-Namespace"></a>
<h2 class="section">16.6 The Internet Namespace</h2>
<a name="index-Internet-namespace_002c-for-sockets"></a>

<p>This section describes the details of the protocols and socket naming
conventions used in the Internet namespace.
</p>
<p>Originally the Internet namespace used only IP version 4 (IPv4).  With
the growing number of hosts on the Internet, a new protocol with a
larger address space was necessary: IP version 6 (IPv6).  IPv6
introduces 128-bit addresses (IPv4 has 32-bit addresses) and other
features, and will eventually replace IPv4.
</p>
<p>To create a socket in the IPv4 Internet namespace, use the symbolic name
<code>PF_INET</code> of this namespace as the <var>namespace</var> argument to
<code>socket</code> or <code>socketpair</code>.  For IPv6 addresses you need the
macro <code>PF_INET6</code>. These macros are defined in &lsquo;<tt>sys/socket.h</tt>&rsquo;.
<a name="index-sys_002fsocket_002eh-5"></a>
</p>
<dl>
<dt><a name="index-PF_005fINET"></a><u>Macro:</u> int <b>PF_INET</b></dt>
<dd><p>This designates the IPv4 Internet namespace and associated family of
protocols.
</p></dd></dl>

<dl>
<dt><a name="index-PF_005fINET6"></a><u>Macro:</u> int <b>PF_INET6</b></dt>
<dd><p>This designates the IPv6 Internet namespace and associated family of
protocols.
</p></dd></dl>

<p>A socket address for the Internet namespace includes the following components:
</p>
<ul>
<li>
The address of the machine you want to connect to.  Internet addresses
can be specified in several ways; these are discussed in <a href="#Internet-Address-Formats">Internet Socket Address Formats</a>, <a href="#Host-Addresses">Host Addresses</a> and <a href="#Host-Names">Host Names</a>.

</li><li>
A port number for that machine.  See section <a href="#Ports">Internet Ports</a>.
</li></ul>

<p>You must ensure that the address and port number are represented in a
canonical format called <em>network byte order</em>.  See section <a href="#Byte-Order">Byte Order Conversion</a>,
for information about this.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Internet-Address-Formats">16.6.1 Internet Socket Address Formats</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">    How socket addresses are specified in the
                                 Internet namespace.
</td></tr>
<tr><td align="left" valign="top"><a href="#Host-Addresses">16.6.2 Host Addresses</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">	        All about host addresses of Internet host.
</td></tr>
<tr><td align="left" valign="top"><a href="#Protocols-Database">16.6.6 Protocols Database</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">		Referring to protocols by name.
</td></tr>
<tr><td align="left" valign="top"><a href="#Ports">16.6.3 Internet Ports</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">			Internet port numbers.
</td></tr>
<tr><td align="left" valign="top"><a href="#Services-Database">16.6.4 The Services Database</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">           Ports may have symbolic names.
</td></tr>
<tr><td align="left" valign="top"><a href="#Byte-Order">16.6.5 Byte Order Conversion</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">		        Different hosts may use different byte
                                 ordering conventions; you need to
                                 canonicalize host address and port number.
</td></tr>
<tr><td align="left" valign="top"><a href="#Inet-Example">16.6.7 Internet Socket Example</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">	        Putting it all together.
</td></tr>
</table>

<hr size="6">
<a name="Internet-Address-Formats"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Internet-Namespace" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Host-Addresses" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Internet-Namespace" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Internet-Socket-Address-Formats"></a>
<h3 class="subsection">16.6.1 Internet Socket Address Formats</h3>

<p>In the Internet namespace, for both IPv4 (<code>AF_INET</code>) and IPv6
(<code>AF_INET6</code>), a socket address consists of a host address
and a port on that host.  In addition, the protocol you choose serves
effectively as a part of the address because local port numbers are
meaningful only within a particular protocol.
</p>
<p>The data types for representing socket addresses in the Internet namespace
are defined in the header file &lsquo;<tt>netinet/in.h</tt>&rsquo;.
<a name="index-netinet_002fin_002eh"></a>
</p>
<dl>
<dt><a name="index-struct-sockaddr_005fin"></a><u>Data Type:</u> <b>struct sockaddr_in</b></dt>
<dd><p>This is the data type used to represent socket addresses in the
Internet namespace.  It has the following members:
</p>
<dl compact="compact">
<dt> <code>sa_family_t sin_family</code></dt>
<dd><p>This identifies the address family or format of the socket address.
You should store the value <code>AF_INET</code> in this member.
See section <a href="#Socket-Addresses">Socket Addresses</a>.
</p>
</dd>
<dt> <code>struct in_addr sin_addr</code></dt>
<dd><p>This is the Internet address of the host machine.  See section <a href="#Host-Addresses">Host Addresses</a>, and <a href="#Host-Names">Host Names</a>, for how to get a value to store
here.
</p>
</dd>
<dt> <code>unsigned short int sin_port</code></dt>
<dd><p>This is the port number.  See section <a href="#Ports">Internet Ports</a>.
</p></dd>
</dl>
</dd></dl>

<p>When you call <code>bind</code> or <code>getsockname</code>, you should specify
<code>sizeof (struct sockaddr_in)</code> as the <var>length</var> parameter if
you are using an IPv4 Internet namespace socket address.
</p>
<dl>
<dt><a name="index-struct-sockaddr_005fin6"></a><u>Data Type:</u> <b>struct sockaddr_in6</b></dt>
<dd><p>This is the data type used to represent socket addresses in the IPv6
namespace.  It has the following members:
</p>
<dl compact="compact">
<dt> <code>sa_family_t sin6_family</code></dt>
<dd><p>This identifies the address family or format of the socket address.
You should store the value of <code>AF_INET6</code> in this member.
See section <a href="#Socket-Addresses">Socket Addresses</a>.
</p>
</dd>
<dt> <code>struct in6_addr sin6_addr</code></dt>
<dd><p>This is the IPv6 address of the host machine.  See section <a href="#Host-Addresses">Host Addresses</a>, and <a href="#Host-Names">Host Names</a>, for how to get a value to store
here.
</p>
</dd>
<dt> <code>uint32_t sin6_flowinfo</code></dt>
<dd><p>This is a currently unimplemented field.
</p>
</dd>
<dt> <code>uint16_t sin6_port</code></dt>
<dd><p>This is the port number.  See section <a href="#Ports">Internet Ports</a>.
</p>
</dd>
</dl>
</dd></dl>

<hr size="6">
<a name="Host-Addresses"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Internet-Address-Formats" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Abstract-Host-Addresses" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Internet-Namespace" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Host-Addresses-1"></a>
<h3 class="subsection">16.6.2 Host Addresses</h3>

<p>Each computer on the Internet has one or more <em>Internet addresses</em>,
numbers which identify that computer among all those on the Internet.
Users typically write IPv4 numeric host addresses as sequences of four
numbers, separated by periods, as in &lsquo;<samp>128.52.46.32</samp>&rsquo;, and IPv6
numeric host addresses as sequences of up to eight numbers separated by
colons, as in &lsquo;<samp>5f03:1200:836f:c100::1</samp>&rsquo;.
</p>
<p>Each computer also has one or more <em>host names</em>, which are strings
of words separated by periods, as in &lsquo;<samp>mescaline.gnu.org</samp>&rsquo;.
</p>
<p>Programs that let the user specify a host typically accept both numeric
addresses and host names.  To open a connection a program needs a
numeric address, and so must convert a host name to the numeric address
it stands for.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Abstract-Host-Addresses">16.6.2.1 Internet Host Addresses</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">	What a host number consists of.
</td></tr>
<tr><td align="left" valign="top"><a href="#Host-Address-Data-Type">16.6.2.2 Host Address Data Type</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">	Data type for a host number.
</td></tr>
<tr><td align="left" valign="top"><a href="#Host-Address-Functions">16.6.2.3 Host Address Functions</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">	Functions to operate on them.
</td></tr>
<tr><td align="left" valign="top"><a href="#Host-Names">16.6.2.4 Host Names</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">		Translating host names to host numbers.
</td></tr>
</table>

<hr size="6">
<a name="Abstract-Host-Addresses"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Host-Addresses" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Host-Address-Data-Type" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Host-Addresses" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Internet-Host-Addresses"></a>
<h4 class="subsubsection">16.6.2.1 Internet Host Addresses</h4>
<a name="index-host-address_002c-Internet"></a>
<a name="index-Internet-host-address"></a>


<a name="index-network-number"></a>
<a name="index-local-network-address-number"></a>
<p>An IPv4 Internet host address is a number containing four bytes of data.
Historically these are divided into two parts, a <em>network number</em> and a
<em>local network address number</em> within that network.  In the
mid-1990s classless addresses were introduced which changed this
behavior.  Since some functions implicitly expect the old definitions,
we first describe the class-based network and will then describe
classless addresses.  IPv6 uses only classless addresses and therefore
the following paragraphs don&rsquo;t apply.
</p>
<p>The class-based IPv4 network number consists of the first one, two or
three bytes; the rest of the bytes are the local address.
</p>
<p>IPv4 network numbers are registered with the Network Information Center
(NIC), and are divided into three classes&mdash;A, B and C.  The local
network address numbers of individual machines are registered with the
administrator of the particular network.
</p>
<p>Class A networks have single-byte numbers in the range 0 to 127.  There
are only a small number of Class A networks, but they can each support a
very large number of hosts.  Medium-sized Class B networks have two-byte
network numbers, with the first byte in the range 128 to 191.  Class C
networks are the smallest; they have three-byte network numbers, with
the first byte in the range 192-255.  Thus, the first 1, 2, or 3 bytes
of an Internet address specify a network.  The remaining bytes of the
Internet address specify the address within that network.
</p>
<p>The Class A network 0 is reserved for broadcast to all networks.  In
addition, the host number 0 within each network is reserved for broadcast
to all hosts in that network.  These uses are obsolete now but for
compatibility reasons you shouldn&rsquo;t use network 0 and host number 0.
</p>
<p>The Class A network 127 is reserved for loopback; you can always use
the Internet address &lsquo;<samp>127.0.0.1</samp>&rsquo; to refer to the host machine.
</p>
<p>Since a single machine can be a member of multiple networks, it can
have multiple Internet host addresses.  However, there is never
supposed to be more than one machine with the same host address.
</p>

<a name="index-standard-dot-notation_002c-for-Internet-addresses"></a>
<a name="index-dot-notation_002c-for-Internet-addresses"></a>
<p>There are four forms of the <em>standard numbers-and-dots notation</em>
for Internet addresses:
</p>
<dl compact="compact">
<dt> <code><var>a</var>.<var>b</var>.<var>c</var>.<var>d</var></code></dt>
<dd><p>This specifies all four bytes of the address individually and is the
commonly used representation.
</p>
</dd>
<dt> <code><var>a</var>.<var>b</var>.<var>c</var></code></dt>
<dd><p>The last part of the address, <var>c</var>, is interpreted as a 2-byte quantity.
This is useful for specifying host addresses in a Class B network with
network address number <code><var>a</var>.<var>b</var></code>.
</p>
</dd>
<dt> <code><var>a</var>.<var>b</var></code></dt>
<dd><p>The last part of the address, <var>b</var>, is interpreted as a 3-byte quantity.
This is useful for specifying host addresses in a Class A network with
network address number <var>a</var>.
</p>
</dd>
<dt> <code><var>a</var></code></dt>
<dd><p>If only one part is given, this corresponds directly to the host address
number.
</p></dd>
</dl>

<p>Within each part of the address, the usual C conventions for specifying
the radix apply.  In other words, a leading &lsquo;<samp>0x</samp>&rsquo; or &lsquo;<samp>0X</samp>&rsquo; implies
hexadecimal radix; a leading &lsquo;<samp>0</samp>&rsquo; implies octal; and otherwise decimal
radix is assumed.
</p>
<a name="Classless-Addresses"></a>
<h4 class="subsubheading">Classless Addresses</h4>

<p>IPv4 addresses (and IPv6 addresses also) are now considered classless;
the distinction between classes A, B and C can be ignored.  Instead an
IPv4 host address consists of a 32-bit address and a 32-bit mask.  The
mask contains set bits for the network part and cleared bits for the
host part.  The network part is contiguous from the left, with the
remaining bits representing the host.  As a consequence, the netmask can
simply be specified as the number of set bits.  Classes A, B and C are
just special cases of this general rule.  For example, class A addresses
have a netmask of &lsquo;<samp>255.0.0.0</samp>&rsquo; or a prefix length of 8.
</p>
<p>Classless IPv4 network addresses are written in numbers-and-dots
notation with the prefix length appended and a slash as separator.  For
example the class A network 10 is written as &lsquo;<samp>10.0.0.0/8</samp>&rsquo;.
</p>
<a name="IPv6-Addresses"></a>
<h4 class="subsubheading">IPv6 Addresses</h4>

<p>IPv6 addresses contain 128 bits (IPv4 has 32 bits) of data.  A host
address is usually written as eight 16-bit hexadecimal numbers that are
separated by colons.  Two colons are used to abbreviate strings of
consecutive zeros.  For example, the IPv6 loopback address
&lsquo;<samp>0:0:0:0:0:0:0:1</samp>&rsquo; can just be written as &lsquo;<samp>::1</samp>&rsquo;.
</p>
<hr size="6">
<a name="Host-Address-Data-Type"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Abstract-Host-Addresses" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Host-Address-Functions" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Host-Addresses" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Host-Address-Data-Type-1"></a>
<h4 class="subsubsection">16.6.2.2 Host Address Data Type</h4>

<p>IPv4 Internet host addresses are represented in some contexts as integers
(type <code>uint32_t</code>).  In other contexts, the integer is
packaged inside a structure of type <code>struct in_addr</code>.  It would
be better if the usage were made consistent, but it is not hard to extract
the integer from the structure or put the integer into a structure.
</p>
<p>You will find older code that uses <code>unsigned long int</code> for
IPv4 Internet host addresses instead of <code>uint32_t</code> or <code>struct
in_addr</code>.  Historically <code>unsigned long int</code> was a 32-bit number but
with 64-bit machines this has changed.  Using <code>unsigned long int</code>
might break the code if it is used on machines where this type doesn&rsquo;t
have 32 bits.  <code>uint32_t</code> is specified by Unix98 and guaranteed to have
32 bits.
</p>
<p>IPv6 Internet host addresses have 128 bits and are packaged inside a
structure of type <code>struct in6_addr</code>.
</p>
<p>The following basic definitions for Internet addresses are declared in
the header file &lsquo;<tt>netinet/in.h</tt>&rsquo;:
<a name="index-netinet_002fin_002eh-1"></a>
</p>
<dl>
<dt><a name="index-struct-in_005faddr"></a><u>Data Type:</u> <b>struct in_addr</b></dt>
<dd><p>This data type is used in certain contexts to contain an IPv4 Internet
host address.  It has just one field, named <code>s_addr</code>, which records
the host address number as an <code>uint32_t</code>.
</p></dd></dl>

<dl>
<dt><a name="index-INADDR_005fLOOPBACK"></a><u>Macro:</u> uint32_t <b>INADDR_LOOPBACK</b></dt>
<dd><p>You can use this constant to stand for &ldquo;the address of this machine,&rdquo;
instead of finding its actual address.  It is the IPv4 Internet address
&lsquo;<samp>127.0.0.1</samp>&rsquo;, which is usually called &lsquo;<samp>localhost</samp>&rsquo;.  This
special constant saves you the trouble of looking up the address of your
own machine.  Also, the system usually implements <code>INADDR_LOOPBACK</code>
specially, avoiding any network traffic for the case of one machine
talking to itself.
</p></dd></dl>

<dl>
<dt><a name="index-INADDR_005fANY"></a><u>Macro:</u> uint32_t <b>INADDR_ANY</b></dt>
<dd><p>You can use this constant to stand for &ldquo;any incoming address&rdquo; when
binding to an address.  See section <a href="#Setting-Address">Setting the Address of a Socket</a>.  This is the usual
address to give in the <code>sin_addr</code> member of <code>struct
sockaddr_in</code> when you want to accept Internet connections.
</p></dd></dl>

<dl>
<dt><a name="index-INADDR_005fBROADCAST"></a><u>Macro:</u> uint32_t <b>INADDR_BROADCAST</b></dt>
<dd><p>This constant is the address you use to send a broadcast message.
</p></dd></dl>

<dl>
<dt><a name="index-INADDR_005fNONE"></a><u>Macro:</u> uint32_t <b>INADDR_NONE</b></dt>
<dd><p>This constant is returned by some functions to indicate an error.
</p></dd></dl>

<dl>
<dt><a name="index-struct-in6_005faddr"></a><u>Data Type:</u> <b>struct in6_addr</b></dt>
<dd><p>This data type is used to store an IPv6 address.  It stores 128 bits of
data, which can be accessed (via a union) in a variety of ways.
</p></dd></dl>

<dl>
<dt><a name="index-in6addr_005floopback"></a><u>Constant:</u> struct in6_addr <b>in6addr_loopback</b></dt>
<dd><p>This constant is the IPv6 address &lsquo;<samp>::1</samp>&rsquo;, the loopback address.  See
above for a description of what this means.  The macro
<code>IN6ADDR_LOOPBACK_INIT</code> is provided to allow you to initialize your
own variables to this value.
</p></dd></dl>

<dl>
<dt><a name="index-in6addr_005fany"></a><u>Constant:</u> struct in6_addr <b>in6addr_any</b></dt>
<dd><p>This constant is the IPv6 address &lsquo;<samp>::</samp>&rsquo;, the unspecified address.  See
above for a description of what this means.  The macro
<code>IN6ADDR_ANY_INIT</code> is provided to allow you to initialize your
own variables to this value.
</p></dd></dl>

<hr size="6">
<a name="Host-Address-Functions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Host-Address-Data-Type" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Host-Names" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Host-Addresses" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Host-Address-Functions-1"></a>
<h4 class="subsubsection">16.6.2.3 Host Address Functions</h4>

<a name="index-arpa_002finet_002eh"></a>
<p>These additional functions for manipulating Internet addresses are
declared in the header file &lsquo;<tt>arpa/inet.h</tt>&rsquo;.  They represent Internet
addresses in network byte order, and network numbers and
local-address-within-network numbers in host byte order.  See section <a href="#Byte-Order">Byte Order Conversion</a>, for an explanation of network and host byte order.
</p>
<dl>
<dt><a name="index-inet_005faton"></a><u>Function:</u> int <b>inet_aton</b><i> (const char *<var>name</var>, struct in_addr *<var>addr</var>)</i></dt>
<dd><p>This function converts the IPv4 Internet host address <var>name</var>
from the standard numbers-and-dots notation into binary data and stores
it in the <code>struct in_addr</code> that <var>addr</var> points to.
<code>inet_aton</code> returns nonzero if the address is valid, zero if not.
</p></dd></dl>

<dl>
<dt><a name="index-inet_005faddr"></a><u>Function:</u> uint32_t <b>inet_addr</b><i> (const char *<var>name</var>)</i></dt>
<dd><p>This function converts the IPv4 Internet host address <var>name</var> from the
standard numbers-and-dots notation into binary data.  If the input is
not valid, <code>inet_addr</code> returns <code>INADDR_NONE</code>.  This is an
obsolete interface to <code>inet_aton</code>, described immediately above. It
is obsolete because <code>INADDR_NONE</code> is a valid address
(255.255.255.255), and <code>inet_aton</code> provides a cleaner way to
indicate error return.
</p></dd></dl>

<dl>
<dt><a name="index-inet_005fnetwork"></a><u>Function:</u> uint32_t <b>inet_network</b><i> (const char *<var>name</var>)</i></dt>
<dd><p>This function extracts the network number from the address <var>name</var>,
given in the standard numbers-and-dots notation. The returned address is
in host order. If the input is not valid, <code>inet_network</code> returns
<code>-1</code>.
</p>
<p>The function works only with traditional IPv4 class A, B and C network
types.  It doesn&rsquo;t work with classless addresses and shouldn&rsquo;t be used
anymore.
</p></dd></dl>

<dl>
<dt><a name="index-inet_005fntoa"></a><u>Function:</u> char * <b>inet_ntoa</b><i> (struct in_addr <var>addr</var>)</i></dt>
<dd><p>This function converts the IPv4 Internet host address <var>addr</var> to a
string in the standard numbers-and-dots notation.  The return value is
a pointer into a statically-allocated buffer.  Subsequent calls will
overwrite the same buffer, so you should copy the string if you need
to save it.
</p>
<p>In multi-threaded programs each thread has an own statically-allocated
buffer.  But still subsequent calls of <code>inet_ntoa</code> in the same
thread will overwrite the result of the last call.
</p>
<p>Instead of <code>inet_ntoa</code> the newer function <code>inet_ntop</code> which is
described below should be used since it handles both IPv4 and IPv6
addresses.
</p></dd></dl>

<dl>
<dt><a name="index-inet_005fmakeaddr"></a><u>Function:</u> struct in_addr <b>inet_makeaddr</b><i> (uint32_t <var>net</var>, uint32_t <var>local</var>)</i></dt>
<dd><p>This function makes an IPv4 Internet host address by combining the network
number <var>net</var> with the local-address-within-network number
<var>local</var>.
</p></dd></dl>

<dl>
<dt><a name="index-inet_005flnaof"></a><u>Function:</u> uint32_t <b>inet_lnaof</b><i> (struct in_addr <var>addr</var>)</i></dt>
<dd><p>This function returns the local-address-within-network part of the
Internet host address <var>addr</var>.
</p>
<p>The function works only with traditional IPv4 class A, B and C network
types.  It doesn&rsquo;t work with classless addresses and shouldn&rsquo;t be used
anymore.
</p></dd></dl>

<dl>
<dt><a name="index-inet_005fnetof"></a><u>Function:</u> uint32_t <b>inet_netof</b><i> (struct in_addr <var>addr</var>)</i></dt>
<dd><p>This function returns the network number part of the Internet host
address <var>addr</var>.
</p>
<p>The function works only with traditional IPv4 class A, B and C network
types.  It doesn&rsquo;t work with classless addresses and shouldn&rsquo;t be used
anymore.
</p></dd></dl>

<dl>
<dt><a name="index-inet_005fpton"></a><u>Function:</u> int <b>inet_pton</b><i> (int <var>af</var>, const char *<var>cp</var>, void *<var>buf</var>)</i></dt>
<dd><p>This function converts an Internet address (either IPv4 or IPv6) from
presentation (textual) to network (binary) format.  <var>af</var> should be
either <code>AF_INET</code> or <code>AF_INET6</code>, as appropriate for the type of
address being converted.  <var>cp</var> is a pointer to the input string, and
<var>buf</var> is a pointer to a buffer for the result.  It is the caller&rsquo;s
responsibility to make sure the buffer is large enough.
</p></dd></dl>

<dl>
<dt><a name="index-inet_005fntop"></a><u>Function:</u> const char * <b>inet_ntop</b><i> (int <var>af</var>, const void *<var>cp</var>, char *<var>buf</var>, size_t <var>len</var>)</i></dt>
<dd><p>This function converts an Internet address (either IPv4 or IPv6) from
network (binary) to presentation (textual) form.  <var>af</var> should be
either <code>AF_INET</code> or <code>AF_INET6</code>, as appropriate.  <var>cp</var> is a
pointer to the address to be converted.  <var>buf</var> should be a pointer
to a buffer to hold the result, and <var>len</var> is the length of this
buffer.  The return value from the function will be this buffer address.
</p></dd></dl>

<hr size="6">
<a name="Host-Names"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Host-Address-Functions" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Ports" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Host-Addresses" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Host-Names-1"></a>
<h4 class="subsubsection">16.6.2.4 Host Names</h4>
<a name="index-hosts-database"></a>
<a name="index-converting-host-name-to-address"></a>
<a name="index-converting-host-address-to-name"></a>

<p>Besides the standard numbers-and-dots notation for Internet addresses,
you can also refer to a host by a symbolic name.  The advantage of a
symbolic name is that it is usually easier to remember.  For example,
the machine with Internet address &lsquo;<samp>158.121.106.19</samp>&rsquo; is also known as
&lsquo;<samp>alpha.gnu.org</samp>&rsquo;; and other machines in the &lsquo;<samp>gnu.org</samp>&rsquo;
domain can refer to it simply as &lsquo;<samp>alpha</samp>&rsquo;.
</p>
<a name="index-_002fetc_002fhosts"></a>
<a name="index-netdb_002eh"></a>
<p>Internally, the system uses a database to keep track of the mapping
between host names and host numbers.  This database is usually either
the file &lsquo;<tt>/etc/hosts</tt>&rsquo; or an equivalent provided by a name server.
The functions and other symbols for accessing this database are declared
in &lsquo;<tt>netdb.h</tt>&rsquo;.  They are BSD features, defined unconditionally if
you include &lsquo;<tt>netdb.h</tt>&rsquo;.
</p>
<dl>
<dt><a name="index-struct-hostent"></a><u>Data Type:</u> <b>struct hostent</b></dt>
<dd><p>This data type is used to represent an entry in the hosts database.  It
has the following members:
</p>
<dl compact="compact">
<dt> <code>char *h_name</code></dt>
<dd><p>This is the &ldquo;official&rdquo; name of the host.
</p>
</dd>
<dt> <code>char **h_aliases</code></dt>
<dd><p>These are alternative names for the host, represented as a null-terminated
vector of strings.
</p>
</dd>
<dt> <code>int h_addrtype</code></dt>
<dd><p>This is the host address type; in practice, its value is always either
<code>AF_INET</code> or <code>AF_INET6</code>, with the latter being used for IPv6
hosts.  In principle other kinds of addresses could be represented in
the database as well as Internet addresses; if this were done, you
might find a value in this field other than <code>AF_INET</code> or
<code>AF_INET6</code>.  See section <a href="#Socket-Addresses">Socket Addresses</a>.
</p>
</dd>
<dt> <code>int h_length</code></dt>
<dd><p>This is the length, in bytes, of each address.
</p>
</dd>
<dt> <code>char **h_addr_list</code></dt>
<dd><p>This is the vector of addresses for the host.  (Recall that the host
might be connected to multiple networks and have different addresses on
each one.)  The vector is terminated by a null pointer.
</p>
</dd>
<dt> <code>char *h_addr</code></dt>
<dd><p>This is a synonym for <code>h_addr_list[0]</code>; in other words, it is the
first host address.
</p></dd>
</dl>
</dd></dl>

<p>As far as the host database is concerned, each address is just a block
of memory <code>h_length</code> bytes long.  But in other contexts there is an
implicit assumption that you can convert IPv4 addresses to a
<code>struct in_addr</code> or an <code>uint32_t</code>.  Host addresses in
a <code>struct hostent</code> structure are always given in network byte
order; see <a href="#Byte-Order">Byte Order Conversion</a>.
</p>
<p>You can use <code>gethostbyname</code>, <code>gethostbyname2</code> or
<code>gethostbyaddr</code> to search the hosts database for information about
a particular host.  The information is returned in a
statically-allocated structure; you must copy the information if you
need to save it across calls.  You can also use <code>getaddrinfo</code> and
<code>getnameinfo</code> to obtain this information.
</p>
<dl>
<dt><a name="index-gethostbyname"></a><u>Function:</u> struct hostent * <b>gethostbyname</b><i> (const char *<var>name</var>)</i></dt>
<dd><p>The <code>gethostbyname</code> function returns information about the host
named <var>name</var>.  If the lookup fails, it returns a null pointer.
</p></dd></dl>

<dl>
<dt><a name="index-gethostbyname2"></a><u>Function:</u> struct hostent * <b>gethostbyname2</b><i> (const char *<var>name</var>, int <var>af</var>)</i></dt>
<dd><p>The <code>gethostbyname2</code> function is like <code>gethostbyname</code>, but
allows the caller to specify the desired address family (e.g.
<code>AF_INET</code> or <code>AF_INET6</code>) of the result.
</p></dd></dl>

<dl>
<dt><a name="index-gethostbyaddr"></a><u>Function:</u> struct hostent * <b>gethostbyaddr</b><i> (const char *<var>addr</var>, size_t <var>length</var>, int <var>format</var>)</i></dt>
<dd><p>The <code>gethostbyaddr</code> function returns information about the host
with Internet address <var>addr</var>.  The parameter <var>addr</var> is not
really a pointer to char - it can be a pointer to an IPv4 or an IPv6
address. The <var>length</var> argument is the size (in bytes) of the address
at <var>addr</var>.  <var>format</var> specifies the address format; for an IPv4
Internet address, specify a value of <code>AF_INET</code>; for an IPv6
Internet address, use <code>AF_INET6</code>.
</p>
<p>If the lookup fails, <code>gethostbyaddr</code> returns a null pointer.
</p></dd></dl>

<a name="index-h_005ferrno"></a>
<p>If the name lookup by <code>gethostbyname</code> or <code>gethostbyaddr</code>
fails, you can find out the reason by looking at the value of the
variable <code>h_errno</code>.  (It would be cleaner design for these
functions to set <code>errno</code>, but use of <code>h_errno</code> is compatible
with other systems.)
</p>
<p>Here are the error codes that you may find in <code>h_errno</code>:
</p>
<dl compact="compact">
<dt> <code>HOST_NOT_FOUND</code></dt>
<dd><a name="index-HOST_005fNOT_005fFOUND"></a>
<p>No such host is known in the database.
</p>
</dd>
<dt> <code>TRY_AGAIN</code></dt>
<dd><a name="index-TRY_005fAGAIN"></a>
<p>This condition happens when the name server could not be contacted.  If
you try again later, you may succeed then.
</p>
</dd>
<dt> <code>NO_RECOVERY</code></dt>
<dd><a name="index-NO_005fRECOVERY"></a>
<p>A non-recoverable error occurred.
</p>
</dd>
<dt> <code>NO_ADDRESS</code></dt>
<dd><a name="index-NO_005fADDRESS"></a>
<p>The host database contains an entry for the name, but it doesn&rsquo;t have an
associated Internet address.
</p></dd>
</dl>

<p>The lookup functions above all have one in common: they are not
reentrant and therefore unusable in multi-threaded applications.
Therefore provides the GNU C library a new set of functions which can be
used in this context.
</p>
<dl>
<dt><a name="index-gethostbyname_005fr"></a><u>Function:</u> int <b>gethostbyname_r</b><i> (const char *restrict <var>name</var>, struct hostent *restrict <var>result_buf</var>, char *restrict <var>buf</var>, size_t <var>buflen</var>, struct hostent **restrict <var>result</var>, int *restrict <var>h_errnop</var>)</i></dt>
<dd><p>The <code>gethostbyname_r</code> function returns information about the host
named <var>name</var>.  The caller must pass a pointer to an object of type
<code>struct hostent</code> in the <var>result_buf</var> parameter.  In addition
the function may need extra buffer space and the caller must pass an
pointer and the size of the buffer in the <var>buf</var> and <var>buflen</var>
parameters.
</p>
<p>A pointer to the buffer, in which the result is stored, is available in
<code>*<var>result</var></code> after the function call successfully returned.  If
an error occurs or if no entry is found, the pointer <code>*<var>result</var></code>
is a null pointer.  Success is signalled by a zero return value.  If the
function failed the return value is an error number.  In addition to the
errors defined for <code>gethostbyname</code> it can also be <code>ERANGE</code>.
In this case the call should be repeated with a larger buffer.
Additional error information is not stored in the global variable
<code>h_errno</code> but instead in the object pointed to by <var>h_errnop</var>.
</p>
<p>Here&rsquo;s a small example:
</p><table><tr><td>&nbsp;</td><td><pre class="smallexample">struct hostent *
gethostname (char *host)
{
  struct hostent hostbuf, *hp;
  size_t hstbuflen;
  char *tmphstbuf;
  int res;
  int herr;

  hstbuflen = 1024;
  /* Allocate buffer, remember to free it to avoid memory leakage.  */
  tmphstbuf = malloc (hstbuflen);

  while ((res = gethostbyname_r (host, &amp;hostbuf, tmphstbuf, hstbuflen,
                                 &amp;hp, &amp;herr)) == ERANGE)
    {
      /* Enlarge the buffer.  */
      hstbuflen *= 2;
      tmphstbuf = realloc (tmphstbuf, hstbuflen);
    }
  /*  Check for errors.  */
  if (res || hp == NULL)
    return NULL;
  return hp;
}
</pre></td></tr></table>
</dd></dl>

<dl>
<dt><a name="index-gethostbyname2_005fr"></a><u>Function:</u> int <b>gethostbyname2_r</b><i> (const char *<var>name</var>, int <var>af</var>, struct hostent *restrict <var>result_buf</var>, char *restrict <var>buf</var>, size_t <var>buflen</var>, struct hostent **restrict <var>result</var>, int *restrict <var>h_errnop</var>)</i></dt>
<dd><p>The <code>gethostbyname2_r</code> function is like <code>gethostbyname_r</code>, but
allows the caller to specify the desired address family (e.g.
<code>AF_INET</code> or <code>AF_INET6</code>) for the result.
</p></dd></dl>

<dl>
<dt><a name="index-gethostbyaddr_005fr"></a><u>Function:</u> int <b>gethostbyaddr_r</b><i> (const char *<var>addr</var>, size_t <var>length</var>, int <var>format</var>, struct hostent *restrict <var>result_buf</var>, char *restrict <var>buf</var>, size_t <var>buflen</var>, struct hostent **restrict <var>result</var>, int *restrict <var>h_errnop</var>)</i></dt>
<dd><p>The <code>gethostbyaddr_r</code> function returns information about the host
with Internet address <var>addr</var>.  The parameter <var>addr</var> is not
really a pointer to char - it can be a pointer to an IPv4 or an IPv6
address. The <var>length</var> argument is the size (in bytes) of the address
at <var>addr</var>.  <var>format</var> specifies the address format; for an IPv4
Internet address, specify a value of <code>AF_INET</code>; for an IPv6
Internet address, use <code>AF_INET6</code>.
</p>
<p>Similar to the <code>gethostbyname_r</code> function, the caller must provide
buffers for the result and memory used internally.  In case of success
the function returns zero.  Otherwise the value is an error number where
<code>ERANGE</code> has the special meaning that the caller-provided buffer is
too small.
</p></dd></dl>

<p>You can also scan the entire hosts database one entry at a time using
<code>sethostent</code>, <code>gethostent</code> and <code>endhostent</code>.  Be careful
when using these functions because they are not reentrant.
</p>
<dl>
<dt><a name="index-sethostent"></a><u>Function:</u> void <b>sethostent</b><i> (int <var>stayopen</var>)</i></dt>
<dd><p>This function opens the hosts database to begin scanning it.  You can
then call <code>gethostent</code> to read the entries.
</p>
<p>If the <var>stayopen</var> argument is nonzero, this sets a flag so that
subsequent calls to <code>gethostbyname</code> or <code>gethostbyaddr</code> will
not close the database (as they usually would).  This makes for more
efficiency if you call those functions several times, by avoiding
reopening the database for each call.
</p></dd></dl>

<dl>
<dt><a name="index-gethostent"></a><u>Function:</u> struct hostent * <b>gethostent</b><i> (void)</i></dt>
<dd><p>This function returns the next entry in the hosts database.  It
returns a null pointer if there are no more entries.
</p></dd></dl>

<dl>
<dt><a name="index-endhostent"></a><u>Function:</u> void <b>endhostent</b><i> (void)</i></dt>
<dd><p>This function closes the hosts database.
</p></dd></dl>

<hr size="6">
<a name="Ports"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Host-Names" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Services-Database" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Internet-Namespace" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Internet-Ports"></a>
<h3 class="subsection">16.6.3 Internet Ports</h3>
<a name="index-port-number"></a>

<p>A socket address in the Internet namespace consists of a machine&rsquo;s
Internet address plus a <em>port number</em> which distinguishes the
sockets on a given machine (for a given protocol).  Port numbers range
from 0 to 65,535.
</p>
<p>Port numbers less than <code>IPPORT_RESERVED</code> are reserved for standard
servers, such as <code>finger</code> and <code>telnet</code>.  There is a database
that keeps track of these, and you can use the <code>getservbyname</code>
function to map a service name onto a port number; see <a href="#Services-Database">The Services Database</a>.
</p>
<p>If you write a server that is not one of the standard ones defined in
the database, you must choose a port number for it.  Use a number
greater than <code>IPPORT_USERRESERVED</code>; such numbers are reserved for
servers and won&rsquo;t ever be generated automatically by the system.
Avoiding conflicts with servers being run by other users is up to you.
</p>
<p>When you use a socket without specifying its address, the system
generates a port number for it.  This number is between
<code>IPPORT_RESERVED</code> and <code>IPPORT_USERRESERVED</code>.
</p>
<p>On the Internet, it is actually legitimate to have two different
sockets with the same port number, as long as they never both try to
communicate with the same socket address (host address plus port
number).  You shouldn&rsquo;t duplicate a port number except in special
circumstances where a higher-level protocol requires it.  Normally,
the system won&rsquo;t let you do it; <code>bind</code> normally insists on
distinct port numbers.  To reuse a port number, you must set the
socket option <code>SO_REUSEADDR</code>.  See section <a href="#Socket_002dLevel-Options">Socket-Level Options</a>.
</p>
<a name="index-netinet_002fin_002eh-2"></a>
<p>These macros are defined in the header file &lsquo;<tt>netinet/in.h</tt>&rsquo;.
</p>
<dl>
<dt><a name="index-IPPORT_005fRESERVED"></a><u>Macro:</u> int <b>IPPORT_RESERVED</b></dt>
<dd><p>Port numbers less than <code>IPPORT_RESERVED</code> are reserved for
superuser use.
</p></dd></dl>

<dl>
<dt><a name="index-IPPORT_005fUSERRESERVED"></a><u>Macro:</u> int <b>IPPORT_USERRESERVED</b></dt>
<dd><p>Port numbers greater than or equal to <code>IPPORT_USERRESERVED</code> are
reserved for explicit use; they will never be allocated automatically.
</p></dd></dl>

<hr size="6">
<a name="Services-Database"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Ports" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Byte-Order" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Internet-Namespace" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="The-Services-Database"></a>
<h3 class="subsection">16.6.4 The Services Database</h3>
<a name="index-services-database"></a>
<a name="index-converting-service-name-to-port-number"></a>
<a name="index-converting-port-number-to-service-name"></a>

<a name="index-_002fetc_002fservices"></a>
<p>The database that keeps track of &ldquo;well-known&rdquo; services is usually
either the file &lsquo;<tt>/etc/services</tt>&rsquo; or an equivalent from a name server.
You can use these utilities, declared in &lsquo;<tt>netdb.h</tt>&rsquo;, to access
the services database.
<a name="index-netdb_002eh-1"></a>
</p>
<dl>
<dt><a name="index-struct-servent"></a><u>Data Type:</u> <b>struct servent</b></dt>
<dd><p>This data type holds information about entries from the services database.
It has the following members:
</p>
<dl compact="compact">
<dt> <code>char *s_name</code></dt>
<dd><p>This is the &ldquo;official&rdquo; name of the service.
</p>
</dd>
<dt> <code>char **s_aliases</code></dt>
<dd><p>These are alternate names for the service, represented as an array of
strings.  A null pointer terminates the array.
</p>
</dd>
<dt> <code>int s_port</code></dt>
<dd><p>This is the port number for the service.  Port numbers are given in
network byte order; see <a href="#Byte-Order">Byte Order Conversion</a>.
</p>
</dd>
<dt> <code>char *s_proto</code></dt>
<dd><p>This is the name of the protocol to use with this service.
See section <a href="#Protocols-Database">Protocols Database</a>.
</p></dd>
</dl>
</dd></dl>

<p>To get information about a particular service, use the
<code>getservbyname</code> or <code>getservbyport</code> functions.  The information
is returned in a statically-allocated structure; you must copy the
information if you need to save it across calls.
</p>
<dl>
<dt><a name="index-getservbyname"></a><u>Function:</u> struct servent * <b>getservbyname</b><i> (const char *<var>name</var>, const char *<var>proto</var>)</i></dt>
<dd><p>The <code>getservbyname</code> function returns information about the
service named <var>name</var> using protocol <var>proto</var>.  If it can&rsquo;t find
such a service, it returns a null pointer.
</p>
<p>This function is useful for servers as well as for clients; servers
use it to determine which port they should listen on (see section <a href="#Listening">Listening for Connections</a>).
</p></dd></dl>

<dl>
<dt><a name="index-getservbyport"></a><u>Function:</u> struct servent * <b>getservbyport</b><i> (int <var>port</var>, const char *<var>proto</var>)</i></dt>
<dd><p>The <code>getservbyport</code> function returns information about the
service at port <var>port</var> using protocol <var>proto</var>.  If it can&rsquo;t
find such a service, it returns a null pointer.
</p></dd></dl>

<p>You can also scan the services database using <code>setservent</code>,
<code>getservent</code> and <code>endservent</code>.  Be careful when using these
functions because they are not reentrant.
</p>
<dl>
<dt><a name="index-setservent"></a><u>Function:</u> void <b>setservent</b><i> (int <var>stayopen</var>)</i></dt>
<dd><p>This function opens the services database to begin scanning it.
</p>
<p>If the <var>stayopen</var> argument is nonzero, this sets a flag so that
subsequent calls to <code>getservbyname</code> or <code>getservbyport</code> will
not close the database (as they usually would).  This makes for more
efficiency if you call those functions several times, by avoiding
reopening the database for each call.
</p></dd></dl>

<dl>
<dt><a name="index-getservent"></a><u>Function:</u> struct servent * <b>getservent</b><i> (void)</i></dt>
<dd><p>This function returns the next entry in the services database.  If
there are no more entries, it returns a null pointer.
</p></dd></dl>

<dl>
<dt><a name="index-endservent"></a><u>Function:</u> void <b>endservent</b><i> (void)</i></dt>
<dd><p>This function closes the services database.
</p></dd></dl>

<hr size="6">
<a name="Byte-Order"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Services-Database" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Protocols-Database" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Internet-Namespace" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Byte-Order-Conversion"></a>
<h3 class="subsection">16.6.5 Byte Order Conversion</h3>
<a name="index-byte-order-conversion_002c-for-socket"></a>
<a name="index-converting-byte-order"></a>

<a name="index-big_002dendian"></a>
<a name="index-little_002dendian"></a>
<p>Different kinds of computers use different conventions for the
ordering of bytes within a word.  Some computers put the most
significant byte within a word first (this is called &ldquo;big-endian&rdquo;
order), and others put it last (&ldquo;little-endian&rdquo; order).
</p>
<a name="index-network-byte-order"></a>
<p>So that machines with different byte order conventions can
communicate, the Internet protocols specify a canonical byte order
convention for data transmitted over the network.  This is known
as <em>network byte order</em>.
</p>
<p>When establishing an Internet socket connection, you must make sure that
the data in the <code>sin_port</code> and <code>sin_addr</code> members of the
<code>sockaddr_in</code> structure are represented in network byte order.
If you are encoding integer data in the messages sent through the
socket, you should convert this to network byte order too.  If you don&rsquo;t
do this, your program may fail when running on or talking to other kinds
of machines.
</p>
<p>If you use <code>getservbyname</code> and <code>gethostbyname</code> or
<code>inet_addr</code> to get the port number and host address, the values are
already in network byte order, and you can copy them directly into
the <code>sockaddr_in</code> structure.
</p>
<p>Otherwise, you have to convert the values explicitly.  Use <code>htons</code>
and <code>ntohs</code> to convert values for the <code>sin_port</code> member.  Use
<code>htonl</code> and <code>ntohl</code> to convert IPv4 addresses for the
<code>sin_addr</code> member.  (Remember, <code>struct in_addr</code> is equivalent
to <code>uint32_t</code>.)  These functions are declared in
&lsquo;<tt>netinet/in.h</tt>&rsquo;.
<a name="index-netinet_002fin_002eh-3"></a>
</p>
<dl>
<dt><a name="index-htons"></a><u>Function:</u> uint16_t <b>htons</b><i> (uint16_t <var>hostshort</var>)</i></dt>
<dd><p>This function converts the <code>uint16_t</code> integer <var>hostshort</var> from
host byte order to network byte order.
</p></dd></dl>

<dl>
<dt><a name="index-ntohs"></a><u>Function:</u> uint16_t <b>ntohs</b><i> (uint16_t <var>netshort</var>)</i></dt>
<dd><p>This function converts the <code>uint16_t</code> integer <var>netshort</var> from
network byte order to host byte order.
</p></dd></dl>

<dl>
<dt><a name="index-htonl"></a><u>Function:</u> uint32_t <b>htonl</b><i> (uint32_t <var>hostlong</var>)</i></dt>
<dd><p>This function converts the <code>uint32_t</code> integer <var>hostlong</var> from
host byte order to network byte order.
</p>
<p>This is used for IPv4 Internet addresses.
</p></dd></dl>

<dl>
<dt><a name="index-ntohl"></a><u>Function:</u> uint32_t <b>ntohl</b><i> (uint32_t <var>netlong</var>)</i></dt>
<dd><p>This function converts the <code>uint32_t</code> integer <var>netlong</var> from
network byte order to host byte order.
</p>
<p>This is used for IPv4 Internet addresses.
</p></dd></dl>

<hr size="6">
<a name="Protocols-Database"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Byte-Order" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Inet-Example" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Internet-Namespace" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Protocols-Database-1"></a>
<h3 class="subsection">16.6.6 Protocols Database</h3>
<a name="index-protocols-database"></a>

<p>The communications protocol used with a socket controls low-level
details of how data are exchanged.  For example, the protocol implements
things like checksums to detect errors in transmissions, and routing
instructions for messages.  Normal user programs have little reason to
mess with these details directly.
</p>
<a name="index-TCP-_0028Internet-protocol_0029"></a>
<p>The default communications protocol for the Internet namespace depends on
the communication style.  For stream communication, the default is TCP
(&ldquo;transmission control protocol&rdquo;).  For datagram communication, the
default is UDP (&ldquo;user datagram protocol&rdquo;).  For reliable datagram
communication, the default is RDP (&ldquo;reliable datagram protocol&rdquo;).
You should nearly always use the default.
</p>
<a name="index-_002fetc_002fprotocols"></a>
<p>Internet protocols are generally specified by a name instead of a
number.  The network protocols that a host knows about are stored in a
database.  This is usually either derived from the file
&lsquo;<tt>/etc/protocols</tt>&rsquo;, or it may be an equivalent provided by a name
server.  You look up the protocol number associated with a named
protocol in the database using the <code>getprotobyname</code> function.
</p>
<p>Here are detailed descriptions of the utilities for accessing the
protocols database.  These are declared in &lsquo;<tt>netdb.h</tt>&rsquo;.
<a name="index-netdb_002eh-2"></a>
</p>
<dl>
<dt><a name="index-struct-protoent"></a><u>Data Type:</u> <b>struct protoent</b></dt>
<dd><p>This data type is used to represent entries in the network protocols
database.  It has the following members:
</p>
<dl compact="compact">
<dt> <code>char *p_name</code></dt>
<dd><p>This is the official name of the protocol.
</p>
</dd>
<dt> <code>char **p_aliases</code></dt>
<dd><p>These are alternate names for the protocol, specified as an array of
strings.  The last element of the array is a null pointer.
</p>
</dd>
<dt> <code>int p_proto</code></dt>
<dd><p>This is the protocol number (in host byte order); use this member as the
<var>protocol</var> argument to <code>socket</code>.
</p></dd>
</dl>
</dd></dl>

<p>You can use <code>getprotobyname</code> and <code>getprotobynumber</code> to search
the protocols database for a specific protocol.  The information is
returned in a statically-allocated structure; you must copy the
information if you need to save it across calls.
</p>
<dl>
<dt><a name="index-getprotobyname"></a><u>Function:</u> struct protoent * <b>getprotobyname</b><i> (const char *<var>name</var>)</i></dt>
<dd><p>The <code>getprotobyname</code> function returns information about the
network protocol named <var>name</var>.  If there is no such protocol, it
returns a null pointer.
</p></dd></dl>

<dl>
<dt><a name="index-getprotobynumber"></a><u>Function:</u> struct protoent * <b>getprotobynumber</b><i> (int <var>protocol</var>)</i></dt>
<dd><p>The <code>getprotobynumber</code> function returns information about the
network protocol with number <var>protocol</var>.  If there is no such
protocol, it returns a null pointer.
</p></dd></dl>

<p>You can also scan the whole protocols database one protocol at a time by
using <code>setprotoent</code>, <code>getprotoent</code> and <code>endprotoent</code>.
Be careful when using these functions because they are not reentrant.
</p>
<dl>
<dt><a name="index-setprotoent"></a><u>Function:</u> void <b>setprotoent</b><i> (int <var>stayopen</var>)</i></dt>
<dd><p>This function opens the protocols database to begin scanning it.
</p>
<p>If the <var>stayopen</var> argument is nonzero, this sets a flag so that
subsequent calls to <code>getprotobyname</code> or <code>getprotobynumber</code> will
not close the database (as they usually would).  This makes for more
efficiency if you call those functions several times, by avoiding
reopening the database for each call.
</p></dd></dl>

<dl>
<dt><a name="index-getprotoent"></a><u>Function:</u> struct protoent * <b>getprotoent</b><i> (void)</i></dt>
<dd><p>This function returns the next entry in the protocols database.  It
returns a null pointer if there are no more entries.
</p></dd></dl>

<dl>
<dt><a name="index-endprotoent"></a><u>Function:</u> void <b>endprotoent</b><i> (void)</i></dt>
<dd><p>This function closes the protocols database.
</p></dd></dl>

<hr size="6">
<a name="Inet-Example"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Protocols-Database" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Misc-Namespaces" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Internet-Namespace" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Internet-Socket-Example"></a>
<h3 class="subsection">16.6.7 Internet Socket Example</h3>

<p>Here is an example showing how to create and name a socket in the
Internet namespace.  The newly created socket exists on the machine that
the program is running on.  Rather than finding and using the machine&rsquo;s
Internet address, this example specifies <code>INADDR_ANY</code> as the host
address; the system replaces that with the machine&rsquo;s actual address.
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;

int 
make_socket (uint16_t port)
{
  int sock;
  struct sockaddr_in name;

  /* <span class="roman">Create the socket.</span> */
  sock = socket (PF_INET, SOCK_STREAM, 0);
  if (sock &lt; 0)
    {
      perror (&quot;socket&quot;);
      exit (EXIT_FAILURE);
    }

  /* <span class="roman">Give the socket a name.</span> */
  name.sin_family = AF_INET;
  name.sin_port = htons (port);
  name.sin_addr.s_addr = htonl (INADDR_ANY);
  if (bind (sock, (struct sockaddr *) &amp;name, sizeof (name)) &lt; 0)
    {
      perror (&quot;bind&quot;);
      exit (EXIT_FAILURE);
    }

  return sock;
}
</pre></td></tr></table>

<p>Here is another example, showing how you can fill in a <code>sockaddr_in</code>
structure, given a host name string and a port number:
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;netdb.h&gt;

void 
init_sockaddr (struct sockaddr_in *name,
               const char *hostname,
               uint16_t port)
{
  struct hostent *hostinfo;

  name-&gt;sin_family = AF_INET;
  name-&gt;sin_port = htons (port);
  hostinfo = gethostbyname (hostname);
  if (hostinfo == NULL) 
    {
      fprintf (stderr, &quot;Unknown host %s.\n&quot;, hostname);
      exit (EXIT_FAILURE);
    }
  name-&gt;sin_addr = *(struct in_addr *) hostinfo-&gt;h_addr;
}
</pre></td></tr></table>

<hr size="6">
<a name="Misc-Namespaces"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Inet-Example" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Open_002fClose-Sockets" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Sockets" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Other-Namespaces"></a>
<h2 class="section">16.7 Other Namespaces</h2>

<a name="index-PF_005fNS"></a>
<a name="index-PF_005fISO"></a>
<a name="index-PF_005fCCITT"></a>
<a name="index-PF_005fIMPLINK"></a>
<a name="index-PF_005fROUTE"></a>
<p>Certain other namespaces and associated protocol families are supported
but not documented yet because they are not often used.  <code>PF_NS</code>
refers to the Xerox Network Software protocols.  <code>PF_ISO</code> stands
for Open Systems Interconnect.  <code>PF_CCITT</code> refers to protocols from
CCITT.  &lsquo;<tt>socket.h</tt>&rsquo; defines these symbols and others naming protocols
not actually implemented.
</p>
<p><code>PF_IMPLINK</code> is used for communicating between hosts and Internet
Message Processors.  For information on this and <code>PF_ROUTE</code>, an
occasionally-used local area routing protocol, see the GNU Hurd Manual
(to appear in the future).
</p>
<hr size="6">
<a name="Open_002fClose-Sockets"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Misc-Namespaces" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Creating-a-Socket" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Sockets" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Opening-and-Closing-Sockets"></a>
<h2 class="section">16.8 Opening and Closing Sockets</h2>

<p>This section describes the actual library functions for opening and
closing sockets.  The same functions work for all namespaces and
connection styles.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Creating-a-Socket">16.8.1 Creating a Socket</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">           How to open a socket.
</td></tr>
<tr><td align="left" valign="top"><a href="#Closing-a-Socket">16.8.2 Closing a Socket</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">            How to close a socket.
</td></tr>
<tr><td align="left" valign="top"><a href="#Socket-Pairs">16.8.3 Socket Pairs</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">                These are created like pipes.
</td></tr>
</table>

<hr size="6">
<a name="Creating-a-Socket"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Open_002fClose-Sockets" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Closing-a-Socket" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Open_002fClose-Sockets" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Creating-a-Socket-1"></a>
<h3 class="subsection">16.8.1 Creating a Socket</h3>
<a name="index-creating-a-socket"></a>
<a name="index-socket_002c-creating"></a>
<a name="index-opening-a-socket"></a>

<p>The primitive for creating a socket is the <code>socket</code> function,
declared in &lsquo;<tt>sys/socket.h</tt>&rsquo;.
<a name="index-sys_002fsocket_002eh-6"></a>
</p>
<dl>
<dt><a name="index-socket-1"></a><u>Function:</u> int <b>socket</b><i> (int <var>namespace</var>, int <var>style</var>, int <var>protocol</var>)</i></dt>
<dd><p>This function creates a socket and specifies communication style
<var>style</var>, which should be one of the socket styles listed in
<a href="#Communication-Styles">Communication Styles</a>.  The <var>namespace</var> argument specifies
the namespace; it must be <code>PF_LOCAL</code> (see section <a href="#Local-Namespace">The Local Namespace</a>) or
<code>PF_INET</code> (see section <a href="#Internet-Namespace">The Internet Namespace</a>).  <var>protocol</var>
designates the specific protocol (see section <a href="#Socket-Concepts">Socket Concepts</a>); zero is
usually right for <var>protocol</var>.
</p>
<p>The return value from <code>socket</code> is the file descriptor for the new
socket, or <code>-1</code> in case of error.  The following <code>errno</code> error
conditions are defined for this function:
</p>
<dl compact="compact">
<dt> <code>EPROTONOSUPPORT</code></dt>
<dd><p>The <var>protocol</var> or <var>style</var> is not supported by the
<var>namespace</var> specified.
</p>
</dd>
<dt> <code>EMFILE</code></dt>
<dd><p>The process already has too many file descriptors open.
</p>
</dd>
<dt> <code>ENFILE</code></dt>
<dd><p>The system already has too many file descriptors open.
</p>
</dd>
<dt> <code>EACCES</code></dt>
<dd><p>The process does not have the privilege to create a socket of the specified
<var>style</var> or <var>protocol</var>.
</p>
</dd>
<dt> <code>ENOBUFS</code></dt>
<dd><p>The system ran out of internal buffer space.
</p></dd>
</dl>

<p>The file descriptor returned by the <code>socket</code> function supports both
read and write operations.  However, like pipes, sockets do not support file
positioning operations.
</p></dd></dl>

<p>For examples of how to call the <code>socket</code> function,
see <a href="#Local-Socket-Example">Example of Local-Namespace Sockets</a>, or <a href="#Inet-Example">Internet Socket Example</a>.
</p>

<hr size="6">
<a name="Closing-a-Socket"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Creating-a-Socket" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Socket-Pairs" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Open_002fClose-Sockets" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Closing-a-Socket-1"></a>
<h3 class="subsection">16.8.2 Closing a Socket</h3>
<a name="index-socket_002c-closing"></a>
<a name="index-closing-a-socket"></a>
<a name="index-shutting-down-a-socket"></a>
<a name="index-socket-shutdown"></a>

<p>When you have finished using a socket, you can simply close its
file descriptor with <code>close</code>; see <a href="libc_13.html#Opening-and-Closing-Files">Opening and Closing Files</a>.
If there is still data waiting to be transmitted over the connection,
normally <code>close</code> tries to complete this transmission.  You
can control this behavior using the <code>SO_LINGER</code> socket option to
specify a timeout period; see <a href="#Socket-Options">Socket Options</a>.
</p>
<a name="index-sys_002fsocket_002eh-7"></a>
<p>You can also shut down only reception or transmission on a
connection by calling <code>shutdown</code>, which is declared in
&lsquo;<tt>sys/socket.h</tt>&rsquo;.
</p>
<dl>
<dt><a name="index-shutdown"></a><u>Function:</u> int <b>shutdown</b><i> (int <var>socket</var>, int <var>how</var>)</i></dt>
<dd><p>The <code>shutdown</code> function shuts down the connection of socket
<var>socket</var>.  The argument <var>how</var> specifies what action to
perform:
</p>
<dl compact="compact">
<dt> <code>0</code></dt>
<dd><p>Stop receiving data for this socket.  If further data arrives,
reject it.
</p>
</dd>
<dt> <code>1</code></dt>
<dd><p>Stop trying to transmit data from this socket.  Discard any data
waiting to be sent.  Stop looking for acknowledgement of data already
sent; don&rsquo;t retransmit it if it is lost.
</p>
</dd>
<dt> <code>2</code></dt>
<dd><p>Stop both reception and transmission.
</p></dd>
</dl>

<p>The return value is <code>0</code> on success and <code>-1</code> on failure.  The
following <code>errno</code> error conditions are defined for this function:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p><var>socket</var> is not a valid file descriptor.
</p>
</dd>
<dt> <code>ENOTSOCK</code></dt>
<dd><p><var>socket</var> is not a socket.
</p>
</dd>
<dt> <code>ENOTCONN</code></dt>
<dd><p><var>socket</var> is not connected.
</p></dd>
</dl>
</dd></dl>

<hr size="6">
<a name="Socket-Pairs"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Closing-a-Socket" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Connections" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Open_002fClose-Sockets" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Socket-Pairs-1"></a>
<h3 class="subsection">16.8.3 Socket Pairs</h3>
<a name="index-creating-a-socket-pair"></a>
<a name="index-socket-pair"></a>
<a name="index-opening-a-socket-pair"></a>

<a name="index-sys_002fsocket_002eh-8"></a>
<p>A <em>socket pair</em> consists of a pair of connected (but unnamed)
sockets.  It is very similar to a pipe and is used in much the same
way.  Socket pairs are created with the <code>socketpair</code> function,
declared in &lsquo;<tt>sys/socket.h</tt>&rsquo;.  A socket pair is much like a pipe; the
main difference is that the socket pair is bidirectional, whereas the
pipe has one input-only end and one output-only end (see section <a href="libc_15.html#Pipes-and-FIFOs">Pipes and FIFOs</a>).
</p>
<dl>
<dt><a name="index-socketpair"></a><u>Function:</u> int <b>socketpair</b><i> (int <var>namespace</var>, int <var>style</var>, int <var>protocol</var>, int <var>filedes</var><tt>[2]</tt>)</i></dt>
<dd><p>This function creates a socket pair, returning the file descriptors in
<code><var>filedes</var>[0]</code> and <code><var>filedes</var>[1]</code>.  The socket pair
is a full-duplex communications channel, so that both reading and writing
may be performed at either end.
</p>
<p>The <var>namespace</var>, <var>style</var> and <var>protocol</var> arguments are
interpreted as for the <code>socket</code> function.  <var>style</var> should be
one of the communication styles listed in <a href="#Communication-Styles">Communication Styles</a>.
The <var>namespace</var> argument specifies the namespace, which must be
<code>AF_LOCAL</code> (see section <a href="#Local-Namespace">The Local Namespace</a>); <var>protocol</var> specifies the
communications protocol, but zero is the only meaningful value.
</p>
<p>If <var>style</var> specifies a connectionless communication style, then
the two sockets you get are not <em>connected</em>, strictly speaking,
but each of them knows the other as the default destination address,
so they can send packets to each other.
</p>
<p>The <code>socketpair</code> function returns <code>0</code> on success and <code>-1</code>
on failure.  The following <code>errno</code> error conditions are defined
for this function:
</p>
<dl compact="compact">
<dt> <code>EMFILE</code></dt>
<dd><p>The process has too many file descriptors open.
</p>
</dd>
<dt> <code>EAFNOSUPPORT</code></dt>
<dd><p>The specified namespace is not supported.
</p>
</dd>
<dt> <code>EPROTONOSUPPORT</code></dt>
<dd><p>The specified protocol is not supported.
</p>
</dd>
<dt> <code>EOPNOTSUPP</code></dt>
<dd><p>The specified protocol does not support the creation of socket pairs.
</p></dd>
</dl>
</dd></dl>

<hr size="6">
<a name="Connections"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Socket-Pairs" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Connecting" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Sockets" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Using-Sockets-with-Connections"></a>
<h2 class="section">16.9 Using Sockets with Connections</h2>

<a name="index-connection"></a>
<a name="index-client"></a>
<a name="index-server"></a>
<p>The most common communication styles involve making a connection to a
particular other socket, and then exchanging data with that socket
over and over.  Making a connection is asymmetric; one side (the
<em>client</em>) acts to request a connection, while the other side (the
<em>server</em>) makes a socket and waits for the connection request.
</p>

<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Connecting">16.9.1 Making a Connection</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">    	     What the client program must do.
</td></tr>
<tr><td align="left" valign="top"><a href="#Listening">16.9.2 Listening for Connections</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">		     How a server program waits for requests.
</td></tr>
<tr><td align="left" valign="top"><a href="#Accepting-Connections">16.9.3 Accepting Connections</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">    What the server does when it gets a request.
</td></tr>
<tr><td align="left" valign="top"><a href="#Who-is-Connected">16.9.4 Who is Connected to Me?</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">	     Getting the address of the
				other side of a connection.
</td></tr>
<tr><td align="left" valign="top"><a href="#Transferring-Data">16.9.5 Transferring Data</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">        How to send and receive data.
</td></tr>
<tr><td align="left" valign="top"><a href="#Byte-Stream-Example">16.9.6 Byte Stream Socket Example</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">	     An example program: a client for communicating
			      over a byte stream socket in the Internet namespace.
</td></tr>
<tr><td align="left" valign="top"><a href="#Server-Example">16.9.7 Byte Stream Connection Server Example</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">	     A corresponding server program.
</td></tr>
<tr><td align="left" valign="top"><a href="#Out_002dof_002dBand-Data">16.9.8 Out-of-Band Data</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">         This is an advanced feature.
</td></tr>
</table>

<hr size="6">
<a name="Connecting"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Connections" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Listening" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Connections" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Making-a-Connection"></a>
<h3 class="subsection">16.9.1 Making a Connection</h3>
<a name="index-connecting-a-socket"></a>
<a name="index-socket_002c-connecting"></a>
<a name="index-socket_002c-initiating-a-connection"></a>
<a name="index-socket_002c-client-actions"></a>

<p>In making a connection, the client makes a connection while the server
waits for and accepts the connection.  Here we discuss what the client
program must do with the <code>connect</code> function, which is declared in
&lsquo;<tt>sys/socket.h</tt>&rsquo;.
</p>
<dl>
<dt><a name="index-connect"></a><u>Function:</u> int <b>connect</b><i> (int <var>socket</var>, struct sockaddr *<var>addr</var>, socklen_t <var>length</var>)</i></dt>
<dd><p>The <code>connect</code> function initiates a connection from the socket
with file descriptor <var>socket</var> to the socket whose address is
specified by the <var>addr</var> and <var>length</var> arguments.  (This socket
is typically on another machine, and it must be already set up as a
server.)  See section <a href="#Socket-Addresses">Socket Addresses</a>, for information about how these
arguments are interpreted.
</p>
<p>Normally, <code>connect</code> waits until the server responds to the request
before it returns.  You can set nonblocking mode on the socket
<var>socket</var> to make <code>connect</code> return immediately without waiting
for the response.  See section <a href="libc_13.html#File-Status-Flags">File Status Flags</a>, for information about
nonblocking mode.
</p>
<p>The normal return value from <code>connect</code> is <code>0</code>.  If an error
occurs, <code>connect</code> returns <code>-1</code>.  The following <code>errno</code>
error conditions are defined for this function:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The socket <var>socket</var> is not a valid file descriptor.
</p>
</dd>
<dt> <code>ENOTSOCK</code></dt>
<dd><p>File descriptor <var>socket</var> is not a socket.
</p>
</dd>
<dt> <code>EADDRNOTAVAIL</code></dt>
<dd><p>The specified address is not available on the remote machine.
</p>
</dd>
<dt> <code>EAFNOSUPPORT</code></dt>
<dd><p>The namespace of the <var>addr</var> is not supported by this socket.
</p>
</dd>
<dt> <code>EISCONN</code></dt>
<dd><p>The socket <var>socket</var> is already connected.
</p>
</dd>
<dt> <code>ETIMEDOUT</code></dt>
<dd><p>The attempt to establish the connection timed out.
</p>
</dd>
<dt> <code>ECONNREFUSED</code></dt>
<dd><p>The server has actively refused to establish the connection.
</p>
</dd>
<dt> <code>ENETUNREACH</code></dt>
<dd><p>The network of the given <var>addr</var> isn&rsquo;t reachable from this host.
</p>
</dd>
<dt> <code>EADDRINUSE</code></dt>
<dd><p>The socket address of the given <var>addr</var> is already in use.
</p>
</dd>
<dt> <code>EINPROGRESS</code></dt>
<dd><p>The socket <var>socket</var> is non-blocking and the connection could not be
established immediately.  You can determine when the connection is
completely established with <code>select</code>; see section <a href="libc_13.html#Waiting-for-I_002fO">Waiting for Input or Output</a>.
Another <code>connect</code> call on the same socket, before the connection is
completely established, will fail with <code>EALREADY</code>.
</p>
</dd>
<dt> <code>EALREADY</code></dt>
<dd><p>The socket <var>socket</var> is non-blocking and already has a pending
connection in progress (see <code>EINPROGRESS</code> above).
</p></dd>
</dl>

<p>This function is defined as a cancellation point in multi-threaded
programs, so one has to be prepared for this and make sure that
allocated resources (like memory, files descriptors, semaphores or
whatever) are freed even if the thread is canceled.
</p></dd></dl>

<hr size="6">
<a name="Listening"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Connecting" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Accepting-Connections" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Connections" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Listening-for-Connections"></a>
<h3 class="subsection">16.9.2 Listening for Connections</h3>
<a name="index-listening-_0028sockets_0029"></a>
<a name="index-sockets_002c-server-actions"></a>
<a name="index-sockets_002c-listening"></a>

<p>Now let us consider what the server process must do to accept
connections on a socket.  First it must use the <code>listen</code> function
to enable connection requests on the socket, and then accept each
incoming connection with a call to <code>accept</code> (see section <a href="#Accepting-Connections">Accepting Connections</a>).  Once connection requests are enabled on a server socket,
the <code>select</code> function reports when the socket has a connection
ready to be accepted (see section <a href="libc_13.html#Waiting-for-I_002fO">Waiting for Input or Output</a>).
</p>
<p>The <code>listen</code> function is not allowed for sockets using
connectionless communication styles.
</p>
<p>You can write a network server that does not even start running until a
connection to it is requested.  See section <a href="#Inetd-Servers"><code>inetd</code> Servers</a>.
</p>
<p>In the Internet namespace, there are no special protection mechanisms
for controlling access to a port; any process on any machine
can make a connection to your server.  If you want to restrict access to
your server, make it examine the addresses associated with connection
requests or implement some other handshaking or identification
protocol.
</p>
<p>In the local namespace, the ordinary file protection bits control who has
access to connect to the socket.
</p>
<dl>
<dt><a name="index-listen"></a><u>Function:</u> int <b>listen</b><i> (int <var>socket</var>, int <var>n</var>)</i></dt>
<dd><p>The <code>listen</code> function enables the socket <var>socket</var> to accept
connections, thus making it a server socket.
</p>
<p>The argument <var>n</var> specifies the length of the queue for pending
connections.  When the queue fills, new clients attempting to connect
fail with <code>ECONNREFUSED</code> until the server calls <code>accept</code> to
accept a connection from the queue.
</p>
<p>The <code>listen</code> function returns <code>0</code> on success and <code>-1</code>
on failure.  The following <code>errno</code> error conditions are defined
for this function:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The argument <var>socket</var> is not a valid file descriptor.
</p>
</dd>
<dt> <code>ENOTSOCK</code></dt>
<dd><p>The argument <var>socket</var> is not a socket.
</p>
</dd>
<dt> <code>EOPNOTSUPP</code></dt>
<dd><p>The socket <var>socket</var> does not support this operation.
</p></dd>
</dl>
</dd></dl>

<hr size="6">
<a name="Accepting-Connections"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Listening" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Who-is-Connected" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Connections" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Accepting-Connections-1"></a>
<h3 class="subsection">16.9.3 Accepting Connections</h3>
<a name="index-sockets_002c-accepting-connections"></a>
<a name="index-accepting-connections"></a>

<p>When a server receives a connection request, it can complete the
connection by accepting the request.  Use the function <code>accept</code>
to do this.
</p>
<p>A socket that has been established as a server can accept connection
requests from multiple clients.  The server&rsquo;s original socket
<em>does not become part of the connection</em>; instead, <code>accept</code>
makes a new socket which participates in the connection.
<code>accept</code> returns the descriptor for this socket.  The server&rsquo;s
original socket remains available for listening for further connection
requests.
</p>
<p>The number of pending connection requests on a server socket is finite.
If connection requests arrive from clients faster than the server can
act upon them, the queue can fill up and additional requests are refused
with an <code>ECONNREFUSED</code> error.  You can specify the maximum length of
this queue as an argument to the <code>listen</code> function, although the
system may also impose its own internal limit on the length of this
queue.
</p>
<dl>
<dt><a name="index-accept"></a><u>Function:</u> int <b>accept</b><i> (int <var>socket</var>, struct sockaddr *<var>addr</var>, socklen_t *<var>length_ptr</var>)</i></dt>
<dd><p>This function is used to accept a connection request on the server
socket <var>socket</var>.
</p>
<p>The <code>accept</code> function waits if there are no connections pending,
unless the socket <var>socket</var> has nonblocking mode set.  (You can use
<code>select</code> to wait for a pending connection, with a nonblocking
socket.)  See section <a href="libc_13.html#File-Status-Flags">File Status Flags</a>, for information about nonblocking
mode.
</p>
<p>The <var>addr</var> and <var>length-ptr</var> arguments are used to return
information about the name of the client socket that initiated the
connection.  See section <a href="#Socket-Addresses">Socket Addresses</a>, for information about the format
of the information.
</p>
<p>Accepting a connection does not make <var>socket</var> part of the
connection.  Instead, it creates a new socket which becomes
connected.  The normal return value of <code>accept</code> is the file
descriptor for the new socket.
</p>
<p>After <code>accept</code>, the original socket <var>socket</var> remains open and
unconnected, and continues listening until you close it.  You can
accept further connections with <var>socket</var> by calling <code>accept</code>
again.
</p>
<p>If an error occurs, <code>accept</code> returns <code>-1</code>.  The following
<code>errno</code> error conditions are defined for this function:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The <var>socket</var> argument is not a valid file descriptor.
</p>
</dd>
<dt> <code>ENOTSOCK</code></dt>
<dd><p>The descriptor <var>socket</var> argument is not a socket.
</p>
</dd>
<dt> <code>EOPNOTSUPP</code></dt>
<dd><p>The descriptor <var>socket</var> does not support this operation.
</p>
</dd>
<dt> <code>EWOULDBLOCK</code></dt>
<dd><p><var>socket</var> has nonblocking mode set, and there are no pending
connections immediately available.
</p></dd>
</dl>

<p>This function is defined as a cancellation point in multi-threaded
programs, so one has to be prepared for this and make sure that
allocated resources (like memory, files descriptors, semaphores or
whatever) are freed even if the thread is canceled.
</p></dd></dl>

<p>The <code>accept</code> function is not allowed for sockets using
connectionless communication styles.
</p>
<hr size="6">
<a name="Who-is-Connected"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Accepting-Connections" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Transferring-Data" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Connections" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Who-is-Connected-to-Me_003f"></a>
<h3 class="subsection">16.9.4 Who is Connected to Me?</h3>

<dl>
<dt><a name="index-getpeername"></a><u>Function:</u> int <b>getpeername</b><i> (int <var>socket</var>, struct sockaddr *<var>addr</var>, socklen_t *<var>length-ptr</var>)</i></dt>
<dd><p>The <code>getpeername</code> function returns the address of the socket that
<var>socket</var> is connected to; it stores the address in the memory space
specified by <var>addr</var> and <var>length-ptr</var>.  It stores the length of
the address in <code>*<var>length-ptr</var></code>.
</p>
<p>See section <a href="#Socket-Addresses">Socket Addresses</a>, for information about the format of the
address.  In some operating systems, <code>getpeername</code> works only for
sockets in the Internet domain.
</p>
<p>The return value is <code>0</code> on success and <code>-1</code> on error.  The
following <code>errno</code> error conditions are defined for this function:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The argument <var>socket</var> is not a valid file descriptor.
</p>
</dd>
<dt> <code>ENOTSOCK</code></dt>
<dd><p>The descriptor <var>socket</var> is not a socket.
</p>
</dd>
<dt> <code>ENOTCONN</code></dt>
<dd><p>The socket <var>socket</var> is not connected.
</p>
</dd>
<dt> <code>ENOBUFS</code></dt>
<dd><p>There are not enough internal buffers available.
</p></dd>
</dl>
</dd></dl>


<hr size="6">
<a name="Transferring-Data"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Who-is-Connected" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Sending-Data" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Connections" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Transferring-Data-1"></a>
<h3 class="subsection">16.9.5 Transferring Data</h3>
<a name="index-reading-from-a-socket"></a>
<a name="index-writing-to-a-socket"></a>

<p>Once a socket has been connected to a peer, you can use the ordinary
<code>read</code> and <code>write</code> operations (see section <a href="libc_13.html#I_002fO-Primitives">Input and Output Primitives</a>) to
transfer data.  A socket is a two-way communications channel, so read
and write operations can be performed at either end.
</p>
<p>There are also some I/O modes that are specific to socket operations.
In order to specify these modes, you must use the <code>recv</code> and
<code>send</code> functions instead of the more generic <code>read</code> and
<code>write</code> functions.  The <code>recv</code> and <code>send</code> functions take
an additional argument which you can use to specify various flags to
control special I/O modes.  For example, you can specify the
<code>MSG_OOB</code> flag to read or write out-of-band data, the
<code>MSG_PEEK</code> flag to peek at input, or the <code>MSG_DONTROUTE</code> flag
to control inclusion of routing information on output.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Sending-Data">16.9.5.1 Sending Data</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">		Sending data with <code>send</code>.
</td></tr>
<tr><td align="left" valign="top"><a href="#Receiving-Data">16.9.5.2 Receiving Data</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">		Reading data with <code>recv</code>.
</td></tr>
<tr><td align="left" valign="top"><a href="#Socket-Data-Options">16.9.5.3 Socket Data Options</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">		Using <code>send</code> and <code>recv</code>.
</td></tr>
</table>

<hr size="6">
<a name="Sending-Data"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Transferring-Data" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Receiving-Data" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Transferring-Data" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Sending-Data-1"></a>
<h4 class="subsubsection">16.9.5.1 Sending Data</h4>

<a name="index-sys_002fsocket_002eh-9"></a>
<p>The <code>send</code> function is declared in the header file
&lsquo;<tt>sys/socket.h</tt>&rsquo;.  If your <var>flags</var> argument is zero, you can just
as well use <code>write</code> instead of <code>send</code>; see <a href="libc_13.html#I_002fO-Primitives">Input and Output Primitives</a>.  If the socket was connected but the connection has broken,
you get a <code>SIGPIPE</code> signal for any use of <code>send</code> or
<code>write</code> (see section <a href="libc_24.html#Miscellaneous-Signals">Miscellaneous Signals</a>).
</p>
<dl>
<dt><a name="index-send"></a><u>Function:</u> int <b>send</b><i> (int <var>socket</var>, void *<var>buffer</var>, size_t <var>size</var>, int <var>flags</var>)</i></dt>
<dd><p>The <code>send</code> function is like <code>write</code>, but with the additional
flags <var>flags</var>.  The possible values of <var>flags</var> are described
in <a href="#Socket-Data-Options">Socket Data Options</a>.
</p>
<p>This function returns the number of bytes transmitted, or <code>-1</code> on
failure.  If the socket is nonblocking, then <code>send</code> (like
<code>write</code>) can return after sending just part of the data.
See section <a href="libc_13.html#File-Status-Flags">File Status Flags</a>, for information about nonblocking mode.
</p>
<p>Note, however, that a successful return value merely indicates that
the message has been sent without error, not necessarily that it has
been received without error.
</p>
<p>The following <code>errno</code> error conditions are defined for this function:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The <var>socket</var> argument is not a valid file descriptor.
</p>
</dd>
<dt> <code>EINTR</code></dt>
<dd><p>The operation was interrupted by a signal before any data was sent.
See section <a href="libc_24.html#Interrupted-Primitives">Primitives Interrupted by Signals</a>.
</p>
</dd>
<dt> <code>ENOTSOCK</code></dt>
<dd><p>The descriptor <var>socket</var> is not a socket.
</p>
</dd>
<dt> <code>EMSGSIZE</code></dt>
<dd><p>The socket type requires that the message be sent atomically, but the
message is too large for this to be possible.
</p>
</dd>
<dt> <code>EWOULDBLOCK</code></dt>
<dd><p>Nonblocking mode has been set on the socket, and the write operation
would block.  (Normally <code>send</code> blocks until the operation can be
completed.)
</p>
</dd>
<dt> <code>ENOBUFS</code></dt>
<dd><p>There is not enough internal buffer space available.
</p>
</dd>
<dt> <code>ENOTCONN</code></dt>
<dd><p>You never connected this socket.
</p>
</dd>
<dt> <code>EPIPE</code></dt>
<dd><p>This socket was connected but the connection is now broken.  In this
case, <code>send</code> generates a <code>SIGPIPE</code> signal first; if that
signal is ignored or blocked, or if its handler returns, then
<code>send</code> fails with <code>EPIPE</code>.
</p></dd>
</dl>

<p>This function is defined as a cancellation point in multi-threaded
programs, so one has to be prepared for this and make sure that
allocated resources (like memory, files descriptors, semaphores or
whatever) are freed even if the thread is canceled.
</p></dd></dl>

<hr size="6">
<a name="Receiving-Data"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Sending-Data" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Socket-Data-Options" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Transferring-Data" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Receiving-Data-1"></a>
<h4 class="subsubsection">16.9.5.2 Receiving Data</h4>

<a name="index-sys_002fsocket_002eh-10"></a>
<p>The <code>recv</code> function is declared in the header file
&lsquo;<tt>sys/socket.h</tt>&rsquo;.  If your <var>flags</var> argument is zero, you can
just as well use <code>read</code> instead of <code>recv</code>; see <a href="libc_13.html#I_002fO-Primitives">Input and Output Primitives</a>.
</p>
<dl>
<dt><a name="index-recv"></a><u>Function:</u> int <b>recv</b><i> (int <var>socket</var>, void *<var>buffer</var>, size_t <var>size</var>, int <var>flags</var>)</i></dt>
<dd><p>The <code>recv</code> function is like <code>read</code>, but with the additional
flags <var>flags</var>.  The possible values of <var>flags</var> are described
in <a href="#Socket-Data-Options">Socket Data Options</a>.
</p>
<p>If nonblocking mode is set for <var>socket</var>, and no data are available to
be read, <code>recv</code> fails immediately rather than waiting.  See section <a href="libc_13.html#File-Status-Flags">File Status Flags</a>, for information about nonblocking mode.
</p>
<p>This function returns the number of bytes received, or <code>-1</code> on failure.
The following <code>errno</code> error conditions are defined for this function:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The <var>socket</var> argument is not a valid file descriptor.
</p>
</dd>
<dt> <code>ENOTSOCK</code></dt>
<dd><p>The descriptor <var>socket</var> is not a socket.
</p>
</dd>
<dt> <code>EWOULDBLOCK</code></dt>
<dd><p>Nonblocking mode has been set on the socket, and the read operation
would block.  (Normally, <code>recv</code> blocks until there is input
available to be read.)
</p>
</dd>
<dt> <code>EINTR</code></dt>
<dd><p>The operation was interrupted by a signal before any data was read.
See section <a href="libc_24.html#Interrupted-Primitives">Primitives Interrupted by Signals</a>.
</p>
</dd>
<dt> <code>ENOTCONN</code></dt>
<dd><p>You never connected this socket.
</p></dd>
</dl>

<p>This function is defined as a cancellation point in multi-threaded
programs, so one has to be prepared for this and make sure that
allocated resources (like memory, files descriptors, semaphores or
whatever) are freed even if the thread is canceled.
</p></dd></dl>

<hr size="6">
<a name="Socket-Data-Options"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Receiving-Data" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Byte-Stream-Example" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Transferring-Data" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Socket-Data-Options-1"></a>
<h4 class="subsubsection">16.9.5.3 Socket Data Options</h4>

<a name="index-sys_002fsocket_002eh-11"></a>
<p>The <var>flags</var> argument to <code>send</code> and <code>recv</code> is a bit
mask.  You can bitwise-OR the values of the following macros together
to obtain a value for this argument.  All are defined in the header
file &lsquo;<tt>sys/socket.h</tt>&rsquo;.
</p>
<dl>
<dt><a name="index-MSG_005fOOB"></a><u>Macro:</u> int <b>MSG_OOB</b></dt>
<dd><p>Send or receive out-of-band data.  See section <a href="#Out_002dof_002dBand-Data">Out-of-Band Data</a>.
</p></dd></dl>

<dl>
<dt><a name="index-MSG_005fPEEK"></a><u>Macro:</u> int <b>MSG_PEEK</b></dt>
<dd><p>Look at the data but don&rsquo;t remove it from the input queue.  This is
only meaningful with input functions such as <code>recv</code>, not with
<code>send</code>.
</p></dd></dl>

<dl>
<dt><a name="index-MSG_005fDONTROUTE"></a><u>Macro:</u> int <b>MSG_DONTROUTE</b></dt>
<dd><p>Don&rsquo;t include routing information in the message.  This is only
meaningful with output operations, and is usually only of interest for
diagnostic or routing programs.  We don&rsquo;t try to explain it here.
</p></dd></dl>

<hr size="6">
<a name="Byte-Stream-Example"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Socket-Data-Options" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Server-Example" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Connections" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Byte-Stream-Socket-Example"></a>
<h3 class="subsection">16.9.6 Byte Stream Socket Example</h3>

<p>Here is an example client program that makes a connection for a byte
stream socket in the Internet namespace.  It doesn&rsquo;t do anything
particularly interesting once it has connected to the server; it just
sends a text string to the server and exits.
</p>
<p>This program uses <code>init_sockaddr</code> to set up the socket address; see
<a href="#Inet-Example">Internet Socket Example</a>.
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">#include &lt;stdio.h&gt;
#include &lt;errno.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;netdb.h&gt;

#define PORT            5555
#define MESSAGE         &quot;Yow!!! Are we having fun yet?!?&quot;
#define SERVERHOST      &quot;mescaline.gnu.org&quot;

void 
write_to_server (int filedes)
{
  int nbytes;

  nbytes = write (filedes, MESSAGE, strlen (MESSAGE) + 1);
  if (nbytes &lt; 0)
    {
      perror (&quot;write&quot;);
      exit (EXIT_FAILURE);
    }
}


int
main (void)
{
  extern void init_sockaddr (struct sockaddr_in *name,
                             const char *hostname,
                             uint16_t port);
  int sock;
  struct sockaddr_in servername;

  /* <span class="roman">Create the socket.</span> */
  sock = socket (PF_INET, SOCK_STREAM, 0);
  if (sock &lt; 0)
    {
      perror (&quot;socket (client)&quot;);
      exit (EXIT_FAILURE);
    }

  /* <span class="roman">Connect to the server.</span> */
  init_sockaddr (&amp;servername, SERVERHOST, PORT);
  if (0 &gt; connect (sock,
                   (struct sockaddr *) &amp;servername,
                   sizeof (servername)))
    {
      perror (&quot;connect (client)&quot;);
      exit (EXIT_FAILURE);
    }

  /* <span class="roman">Send data to the server.</span> */
  write_to_server (sock);
  close (sock);
  exit (EXIT_SUCCESS);
}
</pre></td></tr></table>

<hr size="6">
<a name="Server-Example"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Byte-Stream-Example" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Out_002dof_002dBand-Data" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Connections" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Byte-Stream-Connection-Server-Example"></a>
<h3 class="subsection">16.9.7 Byte Stream Connection Server Example</h3>

<p>The server end is much more complicated.  Since we want to allow
multiple clients to be connected to the server at the same time, it
would be incorrect to wait for input from a single client by simply
calling <code>read</code> or <code>recv</code>.  Instead, the right thing to do is
to use <code>select</code> (see section <a href="libc_13.html#Waiting-for-I_002fO">Waiting for Input or Output</a>) to wait for input on
all of the open sockets.  This also allows the server to deal with
additional connection requests.
</p>
<p>This particular server doesn&rsquo;t do anything interesting once it has
gotten a message from a client.  It does close the socket for that
client when it detects an end-of-file condition (resulting from the
client shutting down its end of the connection).
</p>
<p>This program uses <code>make_socket</code> to set up the socket address; see
<a href="#Inet-Example">Internet Socket Example</a>.
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">#include &lt;stdio.h&gt;
#include &lt;errno.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;netdb.h&gt;

#define PORT    5555
#define MAXMSG  512

int
read_from_client (int filedes)
{
  char buffer[MAXMSG];
  int nbytes;

  nbytes = read (filedes, buffer, MAXMSG);
  if (nbytes &lt; 0)
    {
      /* <span class="roman">Read error.</span> */
      perror (&quot;read&quot;);
      exit (EXIT_FAILURE);
    }
  else if (nbytes == 0)
    /* <span class="roman">End-of-file.</span> */
    return -1;
  else
    {
      /* <span class="roman">Data read.</span> */
      fprintf (stderr, &quot;Server: got message: `%s'\n&quot;, buffer);
      return 0;
    }
}

int
main (void)
{
  extern int make_socket (uint16_t port);
  int sock;
  fd_set active_fd_set, read_fd_set;
  int i;
  struct sockaddr_in clientname;
  size_t size;

  /* <span class="roman">Create the socket and set it up to accept connections.</span> */
  sock = make_socket (PORT);
  if (listen (sock, 1) &lt; 0)
    {
      perror (&quot;listen&quot;);
      exit (EXIT_FAILURE);
    }

  /* <span class="roman">Initialize the set of active sockets.</span> */
  FD_ZERO (&amp;active_fd_set);
  FD_SET (sock, &amp;active_fd_set);

  while (1)
    {
      /* <span class="roman">Block until input arrives on one or more active sockets.</span> */
      read_fd_set = active_fd_set;
      if (select (FD_SETSIZE, &amp;read_fd_set, NULL, NULL, NULL) &lt; 0)
        {
          perror (&quot;select&quot;);
          exit (EXIT_FAILURE);
        }

      /* <span class="roman">Service all the sockets with input pending.</span> */
      for (i = 0; i &lt; FD_SETSIZE; ++i)
        if (FD_ISSET (i, &amp;read_fd_set))
          {
            if (i == sock)
              {
                /* <span class="roman">Connection request on original socket.</span> */
                int new;
                size = sizeof (clientname);
                new = accept (sock,
                              (struct sockaddr *) &amp;clientname,
                              &amp;size);
                if (new &lt; 0)
                  {
                    perror (&quot;accept&quot;);
                    exit (EXIT_FAILURE);
                  }
                fprintf (stderr,
                         &quot;Server: connect from host %s, port %hd.\n&quot;,
                         inet_ntoa (clientname.sin_addr),
                         ntohs (clientname.sin_port));
                FD_SET (new, &amp;active_fd_set);
              }
            else
              {
                /* <span class="roman">Data arriving on an already-connected socket.</span> */
                if (read_from_client (i) &lt; 0)
                  {
                    close (i);
                    FD_CLR (i, &amp;active_fd_set);
                  }
              }
          }
    }
}
</pre></td></tr></table>

<hr size="6">
<a name="Out_002dof_002dBand-Data"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Server-Example" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Datagrams" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Connections" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Out_002dof_002dBand-Data-1"></a>
<h3 class="subsection">16.9.8 Out-of-Band Data</h3>

<a name="index-out_002dof_002dband-data"></a>
<a name="index-high_002dpriority-data"></a>
<p>Streams with connections permit <em>out-of-band</em> data that is
delivered with higher priority than ordinary data.  Typically the
reason for sending out-of-band data is to send notice of an
exceptional condition.  To send out-of-band data use
<code>send</code>, specifying the flag <code>MSG_OOB</code> (see section <a href="#Sending-Data">Sending Data</a>).
</p>
<p>Out-of-band data are received with higher priority because the
receiving process need not read it in sequence; to read the next
available out-of-band data, use <code>recv</code> with the <code>MSG_OOB</code>
flag (see section <a href="#Receiving-Data">Receiving Data</a>).  Ordinary read operations do not read
out-of-band data; they read only ordinary data.
</p>
<a name="index-urgent-socket-condition"></a>
<p>When a socket finds that out-of-band data are on their way, it sends a
<code>SIGURG</code> signal to the owner process or process group of the
socket.  You can specify the owner using the <code>F_SETOWN</code> command
to the <code>fcntl</code> function; see <a href="libc_13.html#Interrupt-Input">Interrupt-Driven Input</a>.  You must
also establish a handler for this signal, as described in <a href="libc_24.html#Signal-Handling">Signal Handling</a>, in order to take appropriate action such as reading the
out-of-band data.
</p>
<p>Alternatively, you can test for pending out-of-band data, or wait
until there is out-of-band data, using the <code>select</code> function; it
can wait for an exceptional condition on the socket.  See section <a href="libc_13.html#Waiting-for-I_002fO">Waiting for Input or Output</a>, for more information about <code>select</code>.
</p>
<p>Notification of out-of-band data (whether with <code>SIGURG</code> or with
<code>select</code>) indicates that out-of-band data are on the way; the data
may not actually arrive until later.  If you try to read the
out-of-band data before it arrives, <code>recv</code> fails with an
<code>EWOULDBLOCK</code> error.
</p>
<p>Sending out-of-band data automatically places a &ldquo;mark&rdquo; in the stream
of ordinary data, showing where in the sequence the out-of-band data
&ldquo;would have been&rdquo;.  This is useful when the meaning of out-of-band
data is &ldquo;cancel everything sent so far&rdquo;.  Here is how you can test,
in the receiving process, whether any ordinary data was sent before
the mark:
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">success = ioctl (socket, SIOCATMARK, &amp;atmark);
</pre></td></tr></table>

<p>The <code>integer</code> variable <var>atmark</var> is set to a nonzero value if
the socket&rsquo;s read pointer has reached the &ldquo;mark&rdquo;.
</p>

<p>Here&rsquo;s a function to discard any ordinary data preceding the
out-of-band mark:
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">int
discard_until_mark (int socket)
{
  while (1)
    {
      /* <span class="roman">This is not an arbitrary limit; any size will do.</span>  */
      char buffer[1024];
      int atmark, success;

      /* <span class="roman">If we have reached the mark, return.</span>  */
      success = ioctl (socket, SIOCATMARK, &amp;atmark);
      if (success &lt; 0)
        perror (&quot;ioctl&quot;);
      if (result)
        return;

      /* <span class="roman">Otherwise, read a bunch of ordinary data and discard it.</span>
         <span class="roman">This is guaranteed not to read past the mark</span>
         <span class="roman">if it starts before the mark.</span>  */
      success = read (socket, buffer, sizeof buffer);
      if (success &lt; 0)
        perror (&quot;read&quot;);
    }
}
</pre></td></tr></table>

<p>If you don&rsquo;t want to discard the ordinary data preceding the mark, you
may need to read some of it anyway, to make room in internal system
buffers for the out-of-band data.  If you try to read out-of-band data
and get an <code>EWOULDBLOCK</code> error, try reading some ordinary data
(saving it so that you can use it when you want it) and see if that
makes room.  Here is an example:
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">struct buffer
{
  char *buf;
  int size;
  struct buffer *next;
};

/* <span class="roman">Read the out-of-band data from SOCKET and return it</span>
   <span class="roman">as a `struct buffer', which records the address of the data</span>
   <span class="roman">and its size.</span>

   <span class="roman">It may be necessary to read some ordinary data</span>
   <span class="roman">in order to make room for the out-of-band data.</span>
   <span class="roman">If so, the ordinary data are saved as a chain of buffers</span>
   <span class="roman">found in the `next' field of the value.</span>  */

struct buffer *
read_oob (int socket)
{
  struct buffer *tail = 0;
  struct buffer *list = 0;

  while (1)
    {
      /* <span class="roman">This is an arbitrary limit.</span>
         <span class="roman">Does anyone know how to do this without a limit?</span>  */
#define BUF_SZ 1024
      char *buf = (char *) xmalloc (BUF_SZ);
      int success;
      int atmark;

      /* <span class="roman">Try again to read the out-of-band data.</span>  */
      success = recv (socket, buf, BUF_SZ, MSG_OOB);
      if (success &gt;= 0)
        {
          /* <span class="roman">We got it, so return it.</span>  */
          struct buffer *link
            = (struct buffer *) xmalloc (sizeof (struct buffer));
          link-&gt;buf = buf;
          link-&gt;size = success;
          link-&gt;next = list;
          return link;
        }

      /* <span class="roman">If we fail, see if we are at the mark.</span>  */
      success = ioctl (socket, SIOCATMARK, &amp;atmark);
      if (success &lt; 0)
        perror (&quot;ioctl&quot;);
      if (atmark)
        {
          /* <span class="roman">At the mark; skipping past more ordinary data cannot help.</span>
             <span class="roman">So just wait a while.</span>  */
          sleep (1);
          continue;
        }

      /* <span class="roman">Otherwise, read a bunch of ordinary data and save it.</span>
         <span class="roman">This is guaranteed not to read past the mark</span>
         <span class="roman">if it starts before the mark.</span>  */
      success = read (socket, buf, BUF_SZ);
      if (success &lt; 0)
        perror (&quot;read&quot;);

      /* <span class="roman">Save this data in the buffer list.</span>  */
      {
        struct buffer *link
          = (struct buffer *) xmalloc (sizeof (struct buffer));
        link-&gt;buf = buf;
        link-&gt;size = success;

        /* <span class="roman">Add the new link to the end of the list.</span>  */
        if (tail)
          tail-&gt;next = link;
        else
          list = link;
        tail = link;
      }
    }
}
</pre></td></tr></table>

<hr size="6">
<a name="Datagrams"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Out_002dof_002dBand-Data" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Sending-Datagrams" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Sockets" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Datagram-Socket-Operations"></a>
<h2 class="section">16.10 Datagram Socket Operations</h2>

<a name="index-datagram-socket"></a>
<p>This section describes how to use communication styles that don&rsquo;t use
connections (styles <code>SOCK_DGRAM</code> and <code>SOCK_RDM</code>).  Using
these styles, you group data into packets and each packet is an
independent communication.  You specify the destination for each
packet individually.
</p>
<p>Datagram packets are like letters: you send each one independently
with its own destination address, and they may arrive in the wrong
order or not at all.
</p>
<p>The <code>listen</code> and <code>accept</code> functions are not allowed for
sockets using connectionless communication styles.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Sending-Datagrams">16.10.1 Sending Datagrams</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">    Sending packets on a datagram socket.
</td></tr>
<tr><td align="left" valign="top"><a href="#Receiving-Datagrams">16.10.2 Receiving Datagrams</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">  Receiving packets on a datagram socket.
</td></tr>
<tr><td align="left" valign="top"><a href="#Datagram-Example">16.10.3 Datagram Socket Example</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">     An example program: packets sent over a
                           datagram socket in the local namespace.
</td></tr>
<tr><td align="left" valign="top"><a href="#Example-Receiver">16.10.4 Example of Reading Datagrams</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">	 Another program, that receives those packets.
</td></tr>
</table>

<hr size="6">
<a name="Sending-Datagrams"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Datagrams" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Receiving-Datagrams" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Datagrams" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Sending-Datagrams-1"></a>
<h3 class="subsection">16.10.1 Sending Datagrams</h3>
<a name="index-sending-a-datagram"></a>
<a name="index-transmitting-datagrams"></a>
<a name="index-datagrams_002c-transmitting"></a>

<a name="index-sys_002fsocket_002eh-12"></a>
<p>The normal way of sending data on a datagram socket is by using the
<code>sendto</code> function, declared in &lsquo;<tt>sys/socket.h</tt>&rsquo;.
</p>
<p>You can call <code>connect</code> on a datagram socket, but this only
specifies a default destination for further data transmission on the
socket.  When a socket has a default destination you can use
<code>send</code> (see section <a href="#Sending-Data">Sending Data</a>) or even <code>write</code> (see section <a href="libc_13.html#I_002fO-Primitives">Input and Output Primitives</a>) to send a packet there.  You can cancel the default
destination by calling <code>connect</code> using an address format of
<code>AF_UNSPEC</code> in the <var>addr</var> argument.  See section <a href="#Connecting">Making a Connection</a>, for
more information about the <code>connect</code> function.
</p>
<dl>
<dt><a name="index-sendto"></a><u>Function:</u> int <b>sendto</b><i> (int <var>socket</var>, void *<var>buffer</var>. size_t <var>size</var>, int <var>flags</var>, struct sockaddr *<var>addr</var>, socklen_t <var>length</var>)</i></dt>
<dd><p>The <code>sendto</code> function transmits the data in the <var>buffer</var>
through the socket <var>socket</var> to the destination address specified
by the <var>addr</var> and <var>length</var> arguments.  The <var>size</var> argument
specifies the number of bytes to be transmitted.
</p>
<p>The <var>flags</var> are interpreted the same way as for <code>send</code>; see
<a href="#Socket-Data-Options">Socket Data Options</a>.
</p>
<p>The return value and error conditions are also the same as for
<code>send</code>, but you cannot rely on the system to detect errors and
report them; the most common error is that the packet is lost or there
is no-one at the specified address to receive it, and the operating
system on your machine usually does not know this.
</p>
<p>It is also possible for one call to <code>sendto</code> to report an error
owing to a problem related to a previous call.
</p>
<p>This function is defined as a cancellation point in multi-threaded
programs, so one has to be prepared for this and make sure that
allocated resources (like memory, files descriptors, semaphores or
whatever) are freed even if the thread is canceled.
</p></dd></dl>

<hr size="6">
<a name="Receiving-Datagrams"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Sending-Datagrams" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Datagram-Example" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Datagrams" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Receiving-Datagrams-1"></a>
<h3 class="subsection">16.10.2 Receiving Datagrams</h3>
<a name="index-receiving-datagrams"></a>

<p>The <code>recvfrom</code> function reads a packet from a datagram socket and
also tells you where it was sent from.  This function is declared in
&lsquo;<tt>sys/socket.h</tt>&rsquo;.
</p>
<dl>
<dt><a name="index-recvfrom"></a><u>Function:</u> int <b>recvfrom</b><i> (int <var>socket</var>, void *<var>buffer</var>, size_t <var>size</var>, int <var>flags</var>, struct sockaddr *<var>addr</var>, socklen_t *<var>length-ptr</var>)</i></dt>
<dd><p>The <code>recvfrom</code> function reads one packet from the socket
<var>socket</var> into the buffer <var>buffer</var>.  The <var>size</var> argument
specifies the maximum number of bytes to be read.
</p>
<p>If the packet is longer than <var>size</var> bytes, then you get the first
<var>size</var> bytes of the packet and the rest of the packet is lost.
There&rsquo;s no way to read the rest of the packet.  Thus, when you use a
packet protocol, you must always know how long a packet to expect.
</p>
<p>The <var>addr</var> and <var>length-ptr</var> arguments are used to return the
address where the packet came from.  See section <a href="#Socket-Addresses">Socket Addresses</a>.  For a
socket in the local domain the address information won&rsquo;t be meaningful,
since you can&rsquo;t read the address of such a socket (see section <a href="#Local-Namespace">The Local Namespace</a>).  You can specify a null pointer as the <var>addr</var> argument
if you are not interested in this information.
</p>
<p>The <var>flags</var> are interpreted the same way as for <code>recv</code>
(see section <a href="#Socket-Data-Options">Socket Data Options</a>).  The return value and error conditions
are also the same as for <code>recv</code>.
</p>
<p>This function is defined as a cancellation point in multi-threaded
programs, so one has to be prepared for this and make sure that
allocated resources (like memory, files descriptors, semaphores or
whatever) are freed even if the thread is canceled.
</p></dd></dl>

<p>You can use plain <code>recv</code> (see section <a href="#Receiving-Data">Receiving Data</a>) instead of
<code>recvfrom</code> if you don&rsquo;t need to find out who sent the packet
(either because you know where it should come from or because you
treat all possible senders alike).  Even <code>read</code> can be used if
you don&rsquo;t want to specify <var>flags</var> (see section <a href="libc_13.html#I_002fO-Primitives">Input and Output Primitives</a>).
</p>

<hr size="6">
<a name="Datagram-Example"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Receiving-Datagrams" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Example-Receiver" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Datagrams" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Datagram-Socket-Example"></a>
<h3 class="subsection">16.10.3 Datagram Socket Example</h3>

<p>Here is a set of example programs that send messages over a datagram
stream in the local namespace.  Both the client and server programs use
the <code>make_named_socket</code> function that was presented in <a href="#Local-Socket-Example">Example of Local-Namespace Sockets</a>, to create and name their sockets.
</p>
<p>First, here is the server program.  It sits in a loop waiting for
messages to arrive, bouncing each message back to the sender.
Obviously this isn&rsquo;t a particularly useful program, but it does show
the general ideas involved.
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">#include &lt;stdio.h&gt;
#include &lt;errno.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;sys/un.h&gt;

#define SERVER  &quot;/tmp/serversocket&quot;
#define MAXMSG  512

int
main (void)
{
  int sock;
  char message[MAXMSG];
  struct sockaddr_un name;
  size_t size;
  int nbytes;

  /* <span class="roman">Remove the filename first, it's ok if the call fails</span> */
  unlink (SERVER);

  /* <span class="roman">Make the socket, then loop endlessly.</span> */
  sock = make_named_socket (SERVER);
  while (1)
    {
      /* <span class="roman">Wait for a datagram.</span> */
      size = sizeof (name);
      nbytes = recvfrom (sock, message, MAXMSG, 0,
                         (struct sockaddr *) &amp; name, &amp;size);
      if (nbytes &lt; 0)
        {
          perror (&quot;recfrom (server)&quot;);
          exit (EXIT_FAILURE);
        }

      /* <span class="roman">Give a diagnostic message.</span> */
      fprintf (stderr, &quot;Server: got message: %s\n&quot;, message);

      /* <span class="roman">Bounce the message back to the sender.</span> */
      nbytes = sendto (sock, message, nbytes, 0,
                       (struct sockaddr *) &amp; name, size);
      if (nbytes &lt; 0)
        {
          perror (&quot;sendto (server)&quot;);
          exit (EXIT_FAILURE);
        }
    }
}
</pre></td></tr></table>

<hr size="6">
<a name="Example-Receiver"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Datagram-Example" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Inetd" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Datagrams" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Example-of-Reading-Datagrams"></a>
<h3 class="subsection">16.10.4 Example of Reading Datagrams</h3>

<p>Here is the client program corresponding to the server above.
</p>
<p>It sends a datagram to the server and then waits for a reply.  Notice
that the socket for the client (as well as for the server) in this
example has to be given a name.  This is so that the server can direct
a message back to the client.  Since the socket has no associated
connection state, the only way the server can do this is by
referencing the name of the client.
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">#include &lt;stdio.h&gt;
#include &lt;errno.h&gt;
#include &lt;unistd.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;sys/un.h&gt;

#define SERVER  &quot;/tmp/serversocket&quot;
#define CLIENT  &quot;/tmp/mysocket&quot;
#define MAXMSG  512
#define MESSAGE &quot;Yow!!! Are we having fun yet?!?&quot;

int
main (void)
{
  extern int make_named_socket (const char *name);
  int sock;
  char message[MAXMSG];
  struct sockaddr_un name;
  size_t size;
  int nbytes;

  /* <span class="roman">Make the socket.</span> */
  sock = make_named_socket (CLIENT);

  /* <span class="roman">Initialize the server socket address.</span> */
  name.sun_family = AF_LOCAL;
  strcpy (name.sun_path, SERVER);
  size = strlen (name.sun_path) + sizeof (name.sun_family);

  /* <span class="roman">Send the datagram.</span> */
  nbytes = sendto (sock, MESSAGE, strlen (MESSAGE) + 1, 0,
                   (struct sockaddr *) &amp; name, size);
  if (nbytes &lt; 0)
    {
      perror (&quot;sendto (client)&quot;);
      exit (EXIT_FAILURE);
    }

  /* <span class="roman">Wait for a reply.</span> */
  nbytes = recvfrom (sock, message, MAXMSG, 0, NULL, 0);
  if (nbytes &lt; 0)
    {
      perror (&quot;recfrom (client)&quot;);
      exit (EXIT_FAILURE);
    }

  /* <span class="roman">Print a diagnostic message.</span> */
  fprintf (stderr, &quot;Client: got message: %s\n&quot;, message);

  /* <span class="roman">Clean up.</span> */
  remove (CLIENT);
  close (sock);
}
</pre></td></tr></table>

<p>Keep in mind that datagram socket communications are unreliable.  In
this example, the client program waits indefinitely if the message
never reaches the server or if the server&rsquo;s response never comes
back.  It&rsquo;s up to the user running the program to kill and restart
it if desired.  A more automatic solution could be to use
<code>select</code> (see section <a href="libc_13.html#Waiting-for-I_002fO">Waiting for Input or Output</a>) to establish a timeout period
for the reply, and in case of timeout either re-send the message or
shut down the socket and exit.
</p>
<hr size="6">
<a name="Inetd"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Example-Receiver" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Inetd-Servers" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Sockets" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="The-inetd-Daemon"></a>
<h2 class="section">16.11 The <code>inetd</code> Daemon</h2>

<p>We&rsquo;ve explained above how to write a server program that does its own
listening.  Such a server must already be running in order for anyone
to connect to it.
</p>
<p>Another way to provide a service on an Internet port is to let the daemon
program <code>inetd</code> do the listening.  <code>inetd</code> is a program that
runs all the time and waits (using <code>select</code>) for messages on a
specified set of ports.  When it receives a message, it accepts the
connection (if the socket style calls for connections) and then forks a
child process to run the corresponding server program.  You specify the
ports and their programs in the file &lsquo;<tt>/etc/inetd.conf</tt>&rsquo;.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Inetd-Servers">16.11.1 <code>inetd</code> Servers</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Configuring-Inetd">16.11.2 Configuring <code>inetd</code></a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr size="6">
<a name="Inetd-Servers"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Inetd" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Configuring-Inetd" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Inetd" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="inetd-Servers"></a>
<h3 class="subsection">16.11.1 <code>inetd</code> Servers</h3>

<p>Writing a server program to be run by <code>inetd</code> is very simple.  Each time
someone requests a connection to the appropriate port, a new server
process starts.  The connection already exists at this time; the
socket is available as the standard input descriptor and as the
standard output descriptor (descriptors 0 and 1) in the server
process.  Thus the server program can begin reading and writing data
right away.  Often the program needs only the ordinary I/O facilities;
in fact, a general-purpose filter program that knows nothing about
sockets can work as a byte stream server run by <code>inetd</code>.
</p>
<p>You can also use <code>inetd</code> for servers that use connectionless
communication styles.  For these servers, <code>inetd</code> does not try to accept
a connection since no connection is possible.  It just starts the
server program, which can read the incoming datagram packet from
descriptor 0.  The server program can handle one request and then
exit, or you can choose to write it to keep reading more requests
until no more arrive, and then exit.  You must specify which of these
two techniques the server uses when you configure <code>inetd</code>.
</p>
<hr size="6">
<a name="Configuring-Inetd"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Inetd-Servers" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Socket-Options" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Inetd" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Configuring-inetd"></a>
<h3 class="subsection">16.11.2 Configuring <code>inetd</code></h3>

<p>The file &lsquo;<tt>/etc/inetd.conf</tt>&rsquo; tells <code>inetd</code> which ports to listen to
and what server programs to run for them.  Normally each entry in the
file is one line, but you can split it onto multiple lines provided
all but the first line of the entry start with whitespace.  Lines that
start with &lsquo;<samp>#</samp>&rsquo; are comments.
</p>
<p>Here are two standard entries in &lsquo;<tt>/etc/inetd.conf</tt>&rsquo;:
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">ftp	stream	tcp	nowait	root	/libexec/ftpd	ftpd
talk	dgram	udp	wait	root	/libexec/talkd	talkd
</pre></td></tr></table>

<p>An entry has this format:
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample"><var>service</var> <var>style</var> <var>protocol</var> <var>wait</var> <var>username</var> <var>program</var> <var>arguments</var>
</pre></td></tr></table>

<p>The <var>service</var> field says which service this program provides.  It
should be the name of a service defined in &lsquo;<tt>/etc/services</tt>&rsquo;.
<code>inetd</code> uses <var>service</var> to decide which port to listen on for
this entry.
</p>
<p>The fields <var>style</var> and <var>protocol</var> specify the communication
style and the protocol to use for the listening socket.  The style
should be the name of a communication style, converted to lower case
and with &lsquo;<samp>SOCK_</samp>&rsquo; deleted&mdash;for example, &lsquo;<samp>stream</samp>&rsquo; or
&lsquo;<samp>dgram</samp>&rsquo;.  <var>protocol</var> should be one of the protocols listed in
&lsquo;<tt>/etc/protocols</tt>&rsquo;.  The typical protocol names are &lsquo;<samp>tcp</samp>&rsquo; for
byte stream connections and &lsquo;<samp>udp</samp>&rsquo; for unreliable datagrams.
</p>
<p>The <var>wait</var> field should be either &lsquo;<samp>wait</samp>&rsquo; or &lsquo;<samp>nowait</samp>&rsquo;.
Use &lsquo;<samp>wait</samp>&rsquo; if <var>style</var> is a connectionless style and the
server, once started, handles multiple requests as they come in.
Use &lsquo;<samp>nowait</samp>&rsquo; if <code>inetd</code> should start a new process for each message
or request that comes in.  If <var>style</var> uses connections, then
<var>wait</var> <strong>must</strong> be &lsquo;<samp>nowait</samp>&rsquo;.
</p>
<p><var>user</var> is the user name that the server should run as.  <code>inetd</code> runs
as root, so it can set the user ID of its children arbitrarily.  It&rsquo;s
best to avoid using &lsquo;<samp>root</samp>&rsquo; for <var>user</var> if you can; but some
servers, such as Telnet and FTP, read a username and password
themselves.  These servers need to be root initially so they can log
in as commanded by the data coming over the network.
</p>
<p><var>program</var> together with <var>arguments</var> specifies the command to
run to start the server.  <var>program</var> should be an absolute file
name specifying the executable file to run.  <var>arguments</var> consists
of any number of whitespace-separated words, which become the
command-line arguments of <var>program</var>.  The first word in
<var>arguments</var> is argument zero, which should by convention be the
program name itself (sans directories).
</p>
<p>If you edit &lsquo;<tt>/etc/inetd.conf</tt>&rsquo;, you can tell <code>inetd</code> to reread the
file and obey its new contents by sending the <code>inetd</code> process the
<code>SIGHUP</code> signal.  You&rsquo;ll have to use <code>ps</code> to determine the
process ID of the <code>inetd</code> process as it is not fixed.
</p>

<hr size="6">
<a name="Socket-Options"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Configuring-Inetd" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Socket-Option-Functions" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Sockets" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Socket-Options-1"></a>
<h2 class="section">16.12 Socket Options</h2>
<a name="index-socket-options"></a>

<p>This section describes how to read or set various options that modify
the behavior of sockets and their underlying communications protocols.
</p>
<a name="index-level_002c-for-socket-options"></a>
<a name="index-socket-option-level"></a>
<p>When you are manipulating a socket option, you must specify which
<em>level</em> the option pertains to.  This describes whether the option
applies to the socket interface, or to a lower-level communications
protocol interface.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Socket-Option-Functions">16.12.1 Socket Option Functions</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">     The basic functions for setting and getting
                                 socket options.
</td></tr>
<tr><td align="left" valign="top"><a href="#Socket_002dLevel-Options">16.12.2 Socket-Level Options</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">        Details of the options at the socket level.
</td></tr>
</table>

<hr size="6">
<a name="Socket-Option-Functions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Socket-Options" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Socket_002dLevel-Options" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Socket-Options" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Socket-Option-Functions-1"></a>
<h3 class="subsection">16.12.1 Socket Option Functions</h3>

<a name="index-sys_002fsocket_002eh-13"></a>
<p>Here are the functions for examining and modifying socket options.
They are declared in &lsquo;<tt>sys/socket.h</tt>&rsquo;.
</p>
<dl>
<dt><a name="index-getsockopt"></a><u>Function:</u> int <b>getsockopt</b><i> (int <var>socket</var>, int <var>level</var>, int <var>optname</var>, void *<var>optval</var>, socklen_t *<var>optlen-ptr</var>)</i></dt>
<dd><p>The <code>getsockopt</code> function gets information about the value of
option <var>optname</var> at level <var>level</var> for socket <var>socket</var>.
</p>
<p>The option value is stored in a buffer that <var>optval</var> points to.
Before the call, you should supply in <code>*<var>optlen-ptr</var></code> the
size of this buffer; on return, it contains the number of bytes of
information actually stored in the buffer.
</p>
<p>Most options interpret the <var>optval</var> buffer as a single <code>int</code>
value.
</p>
<p>The actual return value of <code>getsockopt</code> is <code>0</code> on success
and <code>-1</code> on failure.  The following <code>errno</code> error conditions
are defined:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The <var>socket</var> argument is not a valid file descriptor.
</p>
</dd>
<dt> <code>ENOTSOCK</code></dt>
<dd><p>The descriptor <var>socket</var> is not a socket.
</p>
</dd>
<dt> <code>ENOPROTOOPT</code></dt>
<dd><p>The <var>optname</var> doesn&rsquo;t make sense for the given <var>level</var>.
</p></dd>
</dl>
</dd></dl>

<dl>
<dt><a name="index-setsockopt"></a><u>Function:</u> int <b>setsockopt</b><i> (int <var>socket</var>, int <var>level</var>, int <var>optname</var>, void *<var>optval</var>, socklen_t <var>optlen</var>)</i></dt>
<dd><p>This function is used to set the socket option <var>optname</var> at level
<var>level</var> for socket <var>socket</var>.  The value of the option is passed
in the buffer <var>optval</var> of size <var>optlen</var>.
</p>

</dd></dl>

<hr size="6">
<a name="Socket_002dLevel-Options"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Socket-Option-Functions" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Networks-Database" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Socket-Options" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Socket_002dLevel-Options-1"></a>
<h3 class="subsection">16.12.2 Socket-Level Options</h3>

<dl>
<dt><a name="index-SOL_005fSOCKET"></a><u>Constant:</u> int <b>SOL_SOCKET</b></dt>
<dd><p>Use this constant as the <var>level</var> argument to <code>getsockopt</code> or
<code>setsockopt</code> to manipulate the socket-level options described in
this section.
</p></dd></dl>

<a name="index-sys_002fsocket_002eh-14"></a>
<p>Here is a table of socket-level option names; all are defined in the
header file &lsquo;<tt>sys/socket.h</tt>&rsquo;.
</p>
<dl compact="compact">
<dt> <code>SO_DEBUG</code></dt>
<dd>
<p>This option toggles recording of debugging information in the underlying
protocol modules.  The value has type <code>int</code>; a nonzero value means
&ldquo;yes&rdquo;.
</p>
</dd>
<dt> <code>SO_REUSEADDR</code></dt>
<dd><p>This option controls whether <code>bind</code> (see section <a href="#Setting-Address">Setting the Address of a Socket</a>)
should permit reuse of local addresses for this socket.  If you enable
this option, you can actually have two sockets with the same Internet
port number; but the system won&rsquo;t allow you to use the two
identically-named sockets in a way that would confuse the Internet.  The
reason for this option is that some higher-level Internet protocols,
including FTP, require you to keep reusing the same port number.
</p>
<p>The value has type <code>int</code>; a nonzero value means &ldquo;yes&rdquo;.
</p>
</dd>
<dt> <code>SO_KEEPALIVE</code></dt>
<dd><p>This option controls whether the underlying protocol should
periodically transmit messages on a connected socket.  If the peer
fails to respond to these messages, the connection is considered
broken.  The value has type <code>int</code>; a nonzero value means
&ldquo;yes&rdquo;.
</p>
</dd>
<dt> <code>SO_DONTROUTE</code></dt>
<dd><p>This option controls whether outgoing messages bypass the normal
message routing facilities.  If set, messages are sent directly to the
network interface instead.  The value has type <code>int</code>; a nonzero
value means &ldquo;yes&rdquo;.
</p>
</dd>
<dt> <code>SO_LINGER</code></dt>
<dd><p>This option specifies what should happen when the socket of a type
that promises reliable delivery still has untransmitted messages when
it is closed; see <a href="#Closing-a-Socket">Closing a Socket</a>.  The value has type
<code>struct linger</code>.
</p>
<dl>
<dt><a name="index-struct-linger"></a><u>Data Type:</u> <b>struct linger</b></dt>
<dd><p>This structure type has the following members:
</p>
<dl compact="compact">
<dt> <code>int l_onoff</code></dt>
<dd><p>This field is interpreted as a boolean.  If nonzero, <code>close</code>
blocks until the data are transmitted or the timeout period has expired.
</p>
</dd>
<dt> <code>int l_linger</code></dt>
<dd><p>This specifies the timeout period, in seconds.
</p></dd>
</dl>
</dd></dl>

</dd>
<dt> <code>SO_BROADCAST</code></dt>
<dd><p>This option controls whether datagrams may be broadcast from the socket.
The value has type <code>int</code>; a nonzero value means &ldquo;yes&rdquo;.
</p>
</dd>
<dt> <code>SO_OOBINLINE</code></dt>
<dd><p>If this option is set, out-of-band data received on the socket is
placed in the normal input queue.  This permits it to be read using
<code>read</code> or <code>recv</code> without specifying the <code>MSG_OOB</code>
flag.  See section <a href="#Out_002dof_002dBand-Data">Out-of-Band Data</a>.  The value has type <code>int</code>; a
nonzero value means &ldquo;yes&rdquo;.
</p>
</dd>
<dt> <code>SO_SNDBUF</code></dt>
<dd><p>This option gets or sets the size of the output buffer.  The value is a
<code>size_t</code>, which is the size in bytes.
</p>
</dd>
<dt> <code>SO_RCVBUF</code></dt>
<dd><p>This option gets or sets the size of the input buffer.  The value is a
<code>size_t</code>, which is the size in bytes.
</p>
</dd>
<dt> <code>SO_STYLE</code></dt>
<dt> <code>SO_TYPE</code></dt>
<dd><p>This option can be used with <code>getsockopt</code> only.  It is used to
get the socket&rsquo;s communication style.  <code>SO_TYPE</code> is the
historical name, and <code>SO_STYLE</code> is the preferred name in GNU.
The value has type <code>int</code> and its value designates a communication
style; see <a href="#Communication-Styles">Communication Styles</a>.
</p>
</dd>
<dt> <code>SO_ERROR</code></dt>
<dd>
<p>This option can be used with <code>getsockopt</code> only.  It is used to reset
the error status of the socket.  The value is an <code>int</code>, which represents
the previous error status.
</p></dd>
</dl>

<hr size="6">
<a name="Networks-Database"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Socket_002dLevel-Options" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Sockets" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Networks-Database-1"></a>
<h2 class="section">16.13 Networks Database</h2>
<a name="index-networks-database"></a>
<a name="index-converting-network-number-to-network-name"></a>
<a name="index-converting-network-name-to-network-number"></a>

<a name="index-_002fetc_002fnetworks"></a>
<a name="index-netdb_002eh-3"></a>
<p>Many systems come with a database that records a list of networks known
to the system developer.  This is usually kept either in the file
&lsquo;<tt>/etc/networks</tt>&rsquo; or in an equivalent from a name server.  This data
base is useful for routing programs such as <code>route</code>, but it is not
useful for programs that simply communicate over the network.  We
provide functions to access this database, which are declared in
&lsquo;<tt>netdb.h</tt>&rsquo;.
</p>
<dl>
<dt><a name="index-struct-netent"></a><u>Data Type:</u> <b>struct netent</b></dt>
<dd><p>This data type is used to represent information about entries in the
networks database.  It has the following members:
</p>
<dl compact="compact">
<dt> <code>char *n_name</code></dt>
<dd><p>This is the &ldquo;official&rdquo; name of the network.
</p>
</dd>
<dt> <code>char **n_aliases</code></dt>
<dd><p>These are alternative names for the network, represented as a vector
of strings.  A null pointer terminates the array.
</p>
</dd>
<dt> <code>int n_addrtype</code></dt>
<dd><p>This is the type of the network number; this is always equal to
<code>AF_INET</code> for Internet networks.
</p>
</dd>
<dt> <code>unsigned long int n_net</code></dt>
<dd><p>This is the network number.  Network numbers are returned in host
byte order; see <a href="#Byte-Order">Byte Order Conversion</a>.
</p></dd>
</dl>
</dd></dl>

<p>Use the <code>getnetbyname</code> or <code>getnetbyaddr</code> functions to search
the networks database for information about a specific network.  The
information is returned in a statically-allocated structure; you must
copy the information if you need to save it.
</p>
<dl>
<dt><a name="index-getnetbyname"></a><u>Function:</u> struct netent * <b>getnetbyname</b><i> (const char *<var>name</var>)</i></dt>
<dd><p>The <code>getnetbyname</code> function returns information about the network
named <var>name</var>.  It returns a null pointer if there is no such
network.
</p></dd></dl>

<dl>
<dt><a name="index-getnetbyaddr"></a><u>Function:</u> struct netent * <b>getnetbyaddr</b><i> (unsigned long int <var>net</var>, int <var>type</var>)</i></dt>
<dd><p>The <code>getnetbyaddr</code> function returns information about the network
of type <var>type</var> with number <var>net</var>.  You should specify a value of
<code>AF_INET</code> for the <var>type</var> argument for Internet networks.
</p>
<p><code>getnetbyaddr</code> returns a null pointer if there is no such
network.
</p></dd></dl>

<p>You can also scan the networks database using <code>setnetent</code>,
<code>getnetent</code> and <code>endnetent</code>.  Be careful when using these
functions because they are not reentrant.
</p>
<dl>
<dt><a name="index-setnetent"></a><u>Function:</u> void <b>setnetent</b><i> (int <var>stayopen</var>)</i></dt>
<dd><p>This function opens and rewinds the networks database.
</p>
<p>If the <var>stayopen</var> argument is nonzero, this sets a flag so that
subsequent calls to <code>getnetbyname</code> or <code>getnetbyaddr</code> will
not close the database (as they usually would).  This makes for more
efficiency if you call those functions several times, by avoiding
reopening the database for each call.
</p></dd></dl>

<dl>
<dt><a name="index-getnetent"></a><u>Function:</u> struct netent * <b>getnetent</b><i> (void)</i></dt>
<dd><p>This function returns the next entry in the networks database.  It
returns a null pointer if there are no more entries.
</p></dd></dl>

<dl>
<dt><a name="index-endnetent"></a><u>Function:</u> void <b>endnetent</b><i> (void)</i></dt>
<dd><p>This function closes the networks database.
</p></dd></dl>
<hr size="6">
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Sockets" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="libc_17.html#Low_002dLevel-Terminal-Interface" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<p>
 <font size="-1">
  This document was generated by <em>root</em> on <em>April 20, 2012</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html 1.82</em></a>.
 </font>
 <br>

</p>
</body>
</html>