--- linux socket programming

A, Socket is what
1, socket socket:
  socket originated in Unix, and Unix / Linux is one of the basic philosophy of "everything is a file", you can use the "open open -> read write / read -> Close close" mode operation. Socket is a realization of the model, i.e., it is a special socket file, some of its socket function is performed by the operation (read / write the IO, open, close).
  Plainly Socket application layer and TCP / IP communications protocol suite middleware abstraction layer, which is a set of interfaces. In design mode, Socket is actually a facade pattern, it is the complexity of TCP / IP protocol suite is hidden behind the Socket interface for users, a simple interface is all set, let Socket to organize data in order to comply with the specified protocol.

Note: In fact, there is no socket layer concept, it's just an application design patterns facade, so that the programming becomes easier. It is a software abstraction layer. In network programming, we use a lot is achieved through the socket.

2, the socket descriptor is what
in fact we were in the kernel socket communication when two opened up space for us to send and receive data, when in fact we create a socket when a file operation Descriptor.
Here Insert Picture Description
3, what network byte order

  • Cpu endian order is stored in the data memory, which can be divided into big-endian, little-endian. (It depends on the architecture cpu)
    big-endian: high low address memory.
    Little-endian: low address memory low.
    Host byte order: the current endian computers
    can imagine two different computers in different byte order because the computer's lead to ambiguity of our data, which makes the data we receive and send data inconsistencies.
    This time we use the same provision, the network byte order (that is specified using the same transmission in the network byte order, here is the big-endian).
    Address Translation
	   #include <sys/socket.h>
       #include <netinet/in.h>
       #include <arpa/inet.h>
       int inet_aton(const char *cp, struct in_addr *inp);
       in_addr_t inet_addr(const char *cp);
       in_addr_t inet_network(const char *cp);
       char *inet_ntoa(struct in_addr in);
       struct in_addr inet_makeaddr(int net, int host);
       in_addr_t inet_lnaof(struct in_addr in);

Determine the current size of the machine end

#include <iostream>
using namespace std;
int main(){
	int p = 0x11223344;
	char* pp = (char*)&p;
	cout << *pp << endl;
	system("pause");
	return EXIT_SUCCESS;
}

The results show

4, socket programming API

  • socket interface
       #include <sys/types.h>      
       #include <sys/socket.h>
       int socket(int domain, int type, int protocol);

domain: domain parameter specifies a communication domain (often used AF_INET)
type: the type of socket (typically using TCP and udp of SOCK_STREAM SOCK_DGRAM)
Protocol: Protocol type, often used IPPROTO_TCP go (i.e. 6), IPPROTO_UDP (17). If it is 0, then the default protocol
Return Value: generating a socket descriptor, else return -1 and errno

  • bind interfaces
       #include <sys/types.h>
       #include <sys/socket.h>
       int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);

bindSocket as sockfdthe local address my_addr. my_addrLength addrlen(bytes). The traditional name for a socket is to assign a name. When using the socket (2), when the function creates a socket, it exists in an address space (address family) but has not been assigned a name to it
returns 0 on success, failure to return -1

  • listen Interface
       #include <sys/socket.h>
       int listen(int s, int backlog);

It is possible to automatically receive incoming connections and a queue length limit specified for the connection request note is the maximum number of simultaneous connections.
S: is the socket descriptor
backlog: Number
Return Value: 0 successful return, else return -1
the Listen implemented the maximum connection principle
there are two queues in the kernel, a connection is unfinished and has completed the connection, while the maximum number of connections is unfinished maximum number of nodes connected to the queue

  • connect Interface
       #include <sys/types.h>          /* See NOTES */
       #include <sys/socket.h>
       int connect(int sockfd, const struct sockaddr *addr,
                   socklen_t addrlen);

The client requests a connection interface
sockfd: a socket descriptor
addr: the address information of the server
addrlen: variable length address information
Return Value: returns 0 on success, else return 1; and errno

  • accept Interface
       #include <sys/types.h>
       #include <sys/socket.h>
       int accept(int s, struct sockaddr *addr, socklen_t *addrlen);

s: socket descriptor
addr: used to store the address information of the client
addrlen: variable length
Return Value: returns -1 failed, successful return socket descriptor for communication

  • Send message Interface
       #include <sys/types.h>
       #include <sys/socket.h>
       int send(int s, const void *msg, size_t len, int flags);
       int sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen);
       int sendmsg(int s, const struct msghdr *msg, int flags);

send, sendtoAnd sendmsgfor transmitting the message to another socket. sendOnly a socket for connection, and sendto, and sendmsgcan be used under any circumstances.
Therefore, in the send communication tcp, udp used in the sendto
s: socket for communication word descriptors
msg: transmit buffer information
len: length
flags: properties option
const struct sockaddr *to, socklen_t tolen: this is in sendtouse, the need to specify the address information

  • Receive information Interface
       #include <sys/types.h>
       #include <sys/socket.h>
       ssize_t recv(int sockfd, void *buf, size_t len, int flags);
       ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
                        struct sockaddr *src_addr, socklen_t *addrlen);
       ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);

recv, recvfrom, recvmsg fact and send, sendto, sendmsg similar.

Understanding sockaddr, sockaddr_in
Here Insert Picture Description
fact sockaddrand sockaddr_inaddress is occupied by the same size, but in the previous stage we are using unix sockaddr, so used in the design of the interface is sockaddr, but sockaddrwe can not be good for space assignment, so we have to use sockaddr_inan assignment and then turn strong

Guess you like

Origin blog.csdn.net/boke_fengwei/article/details/90739756
Recommended