Linux network programming notes

Source: Internet
Author: User
Linux network programming notes-general Linux technology-Linux programming and kernel information. The following is a detailed description. There are three types of sockets:

1. Streaming socket (SOCKET_STREAM)
Provides reliable connection-oriented data transmission services. Data is considered as a byte stream with no length restrictions. For example, FTP uses this protocol.

2. datagram socket (SOCKET_DGRAM)
Provides a connectionless data transmission service, which does not guarantee reliability.

3. original socket (SOCKET_RAW)
This interface allows direct access to lower-level protocols, such as IP and ICMP.

II. The basic socket system has the following calls:

Create a socket: socket ()
Bind local port: bind ()
Establish a connection: connect (), accept ()
Listening port: listen ()
Data transmission: send (), recv ()
Input/Output multiplexing: select ()
Disable socket only: closesocket ()

Three Data Types

Struct sockaddr
{
Unsigned short sa_family; // address family, generally AF_INET
Char sa_data [14]; // 14-byte Protocol address
}

Struct sockaddr_in
{
Short int sin_family; // address family
Unsigned short int sin_port; // port number
Struct in_addr; // ip address
Unsigned char sin_zero [8]; // fill
}

Four common functions

1 socket ()
Header file:
# Include
# Include
Function prototype:
Int socket (int domain, int type, int protocol)
Domain: protocol type, generally AF_INET
Type: socket type
Protocol: used to specify the number of the transport protocol used by the socket. It is usually set to 0.

2 bind ()
Header file:
# Include
# Include
Function prototype:
Int bind (int sockfd, struct sockaddr * my_addr, int addrlen)
Sockfd: socket Descriptor
My_addr: A sockaddr pointer pointing to information including the local IP address and port number.
Addrlen: usually set to sizeof (struct sockaddr)

3 connect ()
Header file:
# Include
# Include
Function prototype:
Int connect (int sockfd, struct sockaddr * serv_addr, int addrlen)
Sockfd: socket descriptor of the target server
Serv_addr: pointer containing the IP address and port number of the target machine
Addrlen: sizeof (struct sockaddr)

4 listen ()
Header file:
# Include
Function prototype:
Int listen (int sockfd, int backlog );
Sockfd: socket descriptor returned by the socket () system call
Backlog: specify the maximum number of requests in the Request queue. incoming connection requests will wait for accept () in the queue.

5 accept ()
Header file:
# Include
# Inlcude
Function prototype:
Int accept (int sockfd, void * addr, int addrlen)
Sockfd: indicates the socket descriptor to be monitored.
Addr: Usually a pointer to the sockaddr_in variable, which is used to store the information of the host requesting the Connection Service.
Addrlen: sizeof (struct sockaddr_in)

6 send ()
Header file:
# Include
Function prototype:
Int send (int sockfd, const void * msg, int len, int flags );
Sockfd: socket descriptor used to transmit data
Msg: pointer to send data
Flags: 0

7 recv ()
Header file:
# Include
# Include
Function prototype:
Int recv (int sockfd, void * buf, int len, unsigned int flags)
Sockfd: socket descriptor for receiving data
Buf: buffer for storing data
Len: Buffer Length
Flags: 0

8 sendto ()
Header file:
# Include
# Include
Function prototype:
Int sendto (int sockfd, const void * msg, int len, unsigned int flags, const struct sockaddr * to, int tolen );


9 recvfrom ()
Header file:
# Include
# Include
Function prototype:
Int recvfrom (int sockfd, void * buf, int len, unsigned int flags, struct sockaddr * from, int fromlen)


10 read () write ()
Int read (int fd, char * buf, int len)
Int write (int fd, char * buf, int len)

11 shutdown ()
Close (sockfd)
Int shutdown (int sockfd, int how)
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.