linux network programming - basics

1. Basics

(1)socket

int socket(int domain, int type, int protocol);

 domain: determining communication features, such as address format

AF (address family) address family 
AF_INET IPv4 Internet domain 
AF_INET6 IPv6 Internet domain 
AF_UNIX UNIX domain 
AF_UNSPEC unspecified

 type: defining a socket type, provided transport layer characterized in the main

SOCK_STREAM orderly, reliable, bi-directional byte-stream oriented 
fixed length, orderly, connection-oriented, packet transmission SOCK_SEQPACKET 
SOCK_DGRAM fixed length, connectionless and unreliable packet    
SOCK_RAM IP packet interface protocol layers, i.e. without the use of predefined transport Floor
    

 protocol: usually 0, indicating the selection according to the default protocol given domain and socket type.

 

return socket fd, so that it can use part of api file IO

close
dup dup2
poll
select
write
read

 Sockets are bidirectional, you can disable input / output

int shutdown(int sockfd, int how)
how:
    SHUT_RD
    SHUT_WR
    SHUT_RDWR

 

(2) Endian

  Different ways of transmission network equipment, different byte order, in order to allow all devices to understand the packet header information, the need to harmonize National Cheng Kung University endian,

  The unified content is content routing device reads, that destination address, source address, destination port number, source port number.

uint32_t htonl(uint32);
uint16_t htons(uint16);
uint32_t ntohl(uint32);
uint16_t ntohs(uint16);

const char * inet_ntop(int domain, const void *addr, char *str, soklen_t size);
int inet_pton(int domain, const char *str, void *addr);

  

(3) Address Format

  Different communication domains, define different address formats, in order to unify the use of socket api, struct sockaddr unified interface type.

  IPv4 domain, struct sockaddr_in address format

 

2. Under Internet client, server

(1) bind address

int bind(int sock, const struct sockaddr *addr, socklen_t len);

 Call bind, so that socket with a specific network adapter binding, if the IP is set to INADDR_ANY, the socket can accept packet of all network adapters.

 For the client, bind to a specific network card is meaningless, if not binding, when you call connect or listen, random automatically bind the network card.

 Use getsockname get the address of the socket is bound.

int getsockname(int sockfd, struct sockaddr *addr, socklen_t *len);

 Use getpeername obtain the address of the other party to establish a connection

int getpeername(int sockfd, struct sockaddr *addr, socklen_t *len);

(2) to establish a connection

 If you are facing the socket (SOCK_STREAM, SOCK_SEQPACKET) connection, you can call connect to establish a connection

int connect(int sockfd, const struct sockaddr *addr, socklen_t len);

 The connection may fail, as the case may require treatment.

 

 connect the socket connector may also be used without effect:

    Packet transmission destination address is set to connect the address, so that after the data is transmitted, do not need to specify the address.

    Only accept data destination.

 

 Oriented socket, connected to declare use listen to accept connection

int listen(int sockfd, int backlog);

 After returning socket listen, they begin to accept the connection, the connection request will be saved to listen queue,

 And use the accept queue accepts the connection request.

int accept(int sockfd, struct sockaddr *addr, socklen_t *len);

 relates to accept three sockets, socket sockfd original server, socket server process accept client connections return value, the client socket.

 Raw socket and the socket must type domain and the client is the same, in order to establish a connection.

 addr: client socket for receiving information, if you do not care, can be NULL

 len: size of outgoing transmission parameters addr 

 

 If the listen queue is empty, call accept, will be blocked, if you use non-blocking mode, it will immediately return -1, errno is set to EAGAIN or EWOULDBLOCK.

 You can use select poll to handle the blocking accpet.

 

3. The data transmitting / receiving

 Using socket fd, it is possible to use the read, write data acceptance, transmission.

 If you want more features, you can use the send, recv

ssize_t send(int sockfd, const void *buf, size_t nbytes, int flags);

  When using send, sockfd must have determined that the target address, namely the use of connect.

 flags for the setting data transmission function

Do MSG_DONTROUTE a local network to route data 
MSG_DONTWAIT nonblocking 
MSG_OOB band data

 If you send successful return, it does not mean that the other party has received the data, but the data was successfully sent instructions to the network.

 And send a similar sendto

ssize_t sendto(int sockfd, const void *buf, size_t nbytes, int flags, const struct sockaddr *desaddr, socklen_t deslen);

 sendto allows you to specify the destination address when sending

 Another transmission api: sendmsg, allowing multiple data specifying the transmission buffer, and writev very similar.

 

 recv and read similar, but you can specify flags

sszie_t recv(int sockfd, void *buf, size_t nbytes, int flags);
MSG_OOB acceptance band data 
MSG_PEEK view the message contents, but does not take away packets 
MSG_TRUNC even if the packet is truncated, asked to return the actual length of the packet 
MSG_WAITALL wait until all data is available (SOCK_STREAM only)

 MSG_WAITALL: For SOCK_STREAM, received data may be less than the requested using MSG_WAITALL ensure complete acceptance data. MSG_WAITALL for SOCK_DGRAM and SOCK_SEQPACKET did not have any effect, because based on datagram socket once read the entire message is returned.

 When the sender call shutdown end of transmission, the transmitting end according to the protocol or off by default, then when finished receiving data, returns the recv 0.

 If you want to locate the sender, you can call recvfrom

 recvfrom commonly used for connectionless sockets, with recv or recvfrom

 recvmsg will accept data into a plurality of buffers (similar to the readv)

 

4. socket option

 Two sockets interface control behavior, a setup option, a view options

int setsockopt(int sockfd, int level, int option, const void *val, socklen_t len);
int getsockopt(int sockfd, int level, int option, const void *val, socklen_t *len);

 TCP binding to the same address are not allowed within a period of time, the use of port multiplexing can be solved

int reuse = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int));

 

The band data

 Some communication protocols optional feature allows higher priority to data transmission, even if the data has been normal in the transmit buffer.

 TCP support this feature, a support band data byte transfer. UDP is not supported.

 

Guess you like

Origin www.cnblogs.com/yangxinrui/p/11324489.html