Beej's Guide Network to Programming series serialization 09

Source: Internet
Author: User

5.2. socket () --- get the file descriptor
I don't want to talk about it for a long time-I want to call the system function socket (). The following is his prototype:

# Include <sys/types. h>

# Include <sys/socket. h>

 

Int socket (intdomain, int type, int protocol );

 

But what about these parameters? Which socket does they allow you to use (IPv4 or IPv6; TCP or UDP ).

It was once hard-coded by people, and you can do the same. (For domain, you can select PF_INET or PF_INET6; for type, you can select SOCK_STREAM or SOCK_DGRAM; SET protocol to 0, or you can call getprotobyname () to find the protocol you want, "tcp" or "UDP ".)

Note: SOCK_STREAM is equivalent to TCP; SOCK_DGRAM is equivalent to UDP. So you don't have to pay twice for another J

 

(The editor described the relationship between PF _ * and AF _ * here)

Note: they are actually equivalent. If you are interested, you can read "AF_xxxVersus PF_xxx" in Chapter 4, Section 2nd of UnixNetwork Programming"

 

What you really need to do is to directly use the result value obtained by calling getaddrinfo () to the socket () function as follows:

Int s;

Struct addrinfohints, * res;

 

// Do the lookup

// [Pretend wealready filled out the "hints" struct]

Getaddrinfo (www.example.com, "http", & hints, & res );

 

// [Again, youshoshould do error-checking on getaddrinfo (), and walk

// The "res" linked list looking for valid entries instead of just

// Assuming thefirst one is good (like sums of these example do .)

// See the sectionon client/server for real examples.]

 

S = socket (res-> ai_family, res-> ai_socktype, res-> ai_protocol );

 

The socket () function simply returns a socket descriptor for use by other system functions, or returns the-1 error. The global variable errno is set with an error value. (For details about errno, see the man documentation)

Good, good, good! But what are the benefits of using this method? The answer is: Concise!

 

From the column xiaobin_HLJ80

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.