Trunk section
Server-Side:socket->bind->listen->accept->close;
Client: socket->connect->close;
After the connection is established, the data is transmitted ....
The main functions involved are as follows:
The socket function is used to create the socket file identification number;
The BIND function is used for binding sockets and addresses;
The Listen function is used to listen to the socket
The Accept function is used to wait for client connection requests;
The Connect function requests a request from the server.
Specific function explanation
The socket successfully returned the socket's file identifier; the failure returned-1;
Parameters: domain represents the protocol family used by this machine: for example, Af_inet represents an Internet network protocol family; Af_unix represents the system UNIX process communication protocol family
The type represents the specific protocol Sock_stream represents the TCP protocol, and SOCK_DGRAM represents the UDP protocol
Protocal indicates that a specific agreement is also specified if the type is specified to be 0;
The values of the appeal parameters are given by a macro;
Bind succeeds returns 0; fails-1; Binds the local port to the socket word.
Parameter: SOCKFD represents the socket identification number produced by the socket function;
The MY_ADDR represents the local port information, and the latter parameter is the length of the type;
SOCKADDR structure: linux/socket.h header file
As_family represents the protocol family;
Sa_data means the specific address;
SOCKADDR_IN structure: linux/in.h header file
Sin_family represents the protocol family; Sin_port represents the port to listen to, SIN_ADDR indicates which hosts the machine can communicate with if the Inaddr_any represents any
The listen function turns the socket into a listening socket; success is 0; failure-1
Parameter: SOCKFD represents a socket that has already been bound;
The backlog indicates that multiple clients use it to indicate that a maximum number of sockets can be simultaneously monitored
The Accept function indicates waiting for the client to request, and the success is put back the socket identification number after the client request has been accepted;
Parameters: SOCKFD Indicates a socket that has been monitored;
The addr represents the address information of the requesting connection client, and the latter is the information size
The Connect function indicates that the client is requesting a connection to the server, with a success of 0 and a failure of-1
Parameter: SOCKFD represents the socket generated by the client;
SERV_ADDR represents the address information of the server, and the latter is the size of the structure;
The Write function is used for writing messages and writing to FD;
The read function is used to read the FD information;
Information helper Functions
This class is a byte conversion function:
Function letter Meaning: H means the machine host;n represents the network; s means that short;l represents a long
The need for byte conversion here is that the byte order of different machines is not the same, so we can communicate with different machines by unifying the byte order of the network.
This type of function is the conversion of the IP to the domain name:
The first is to turn the domain name into a network-recognizable machine structure pointer, and the second is to convert the IP form;
The key point is the structure body hostent---netdb.h header file returned
This function converts a character IP to a 32-bit ip--string to a in_addr struct.
The purpose of this type of function is to obtain server information:
The server structure is as follows:
---------------------------------------------------I'm a split line-----------the above-mentioned TCP-based-----------------------
Communication functions in UDP:
Server-Side:socket->bind->close;
Client: socket->close;
These two functions are key functions in UDP
The first represents the Accept data function:
Parameter: SOCKFD represents the socket for receiving data;
BUF: Represents a pointer to the data being accepted;
Len: Data size;
Flags: As explained below
From: Is the source's address information
Fromlen: Indicates the size of the from;
The second function represents a send function:
The parameters are very first similar;----
---------------------------------------------------I'm a split line-----------the above UDP-explained-----------------------
Advanced functions:
The first 3 parameters are the same as the previous write and read; the last parameter is evaluated as follows: can be combined
Receiving addresses in the UDP protocol;
Closed channel control in TCP,
---------------------------------------------------I'm a split line----------------------------------
IP/TCP Protocol Data format:
IP protocol:
ICMP protocol:
UDP protocol:
TCP protocol:
Linux Programming---network programming 1