1. Socket ()
When using sockets for network communication, the first thing a process does is call the socket (), generate a socket, and indicate the communication protocol that will be used, such as TCP, UDP, XNS,SPP, and so on.
1 #include <sys/types.h>2 #include <sys/socket.h>3int socket ( int int int protocol);
The function returns a non-negative integer value successfully, otherwise returns-1.
Family: Specifying the Protocol family
- Af_inet,
- Af_inet6
- Af_unix,
- Af_iso,
- Af_route
Type: Represents the socket type.
- Sock_stream
- Sock_dgram
- Sock_raw
Protocol: Indicates that the protocol used is specified. For most apps, protocol is set to 0, which means the default protocol is used.
2. Bind ()
After calling the socket function to create the socket, there is a namespace, but it is not named. Bind () Ties the socket address (local host address and local port address) to the socket handle that was created, assigning the name to the socket to specify the {protocol, local address, local port} in this address
1 #include <sys/types.h>2 #include <sys/socket.h>3int bind (int structint addrlen);
FD: The socket descriptor returned by the socket () function.
ADDRESSP: A pointer to the protocol that contains the address information: name, port, and IP address.
Addrlen: The number of bytes in the address structure.
Bind () connects the delivery address to the socket
Together, the call returns 0 successfully, otherwise returns-1.
3. Connect ()
The client process uses connect () to connect the socket to the server socket after the socket is generated with the socket ().
1 #include <stdio.h>2int connect (int fd, struct sockaddr *addressp, int Addren);
FD: Socket Descriptor
ADDRESSP: Socket address pointer
Addrlen: The number of bytes in the address structure.
4. Listen () function
When multiple service requests occur before the server finishes the current request, the server can reject the incoming service request. To better handle these issues, the server can use the Listen () function to exclude all service requests in one request queue and process them as quickly as possible.
The socket executor establishes an input request queue that stores the arriving service requests on this queue until they are processed, that is, the listen () function not only makes the socket passive listening, but also tells the socket executor to handle multiple simultaneous service requests to the socket.
1 #include <sys/socket.h>2int Listen (intint qlen);
FD: Socket file descriptor, only sock_stream, Sock_seqpacket type
Qlen: Connection request Queue Length.
Successful return 0, otherwise-1
When a connection request arrives, the request queue is inserted. The server uses the Accept () function to remove a request from the queue and respond to the request.
5. Accept ()
1 #include <sys/types.h>2 #include <sys/socket.h>3int Accept ( intint *addrlen);
If the call to accept () is a no connection request on the queue, then this call blocks the caller's run before a request arrives, and accept () returns -1,errno to Ewouldblock, indicating that no connection request arrives.
6. Read () and write ()
These two functions are used to receive and send data from two functions. Read () for reading data from the socket buffer, write () for writing data toward the socket buffer
int Read (intcharint len); int write (intcharint len);
7. Close ()
Use to close the socket and immediately return to the process.
Example: The server sends the string "Hello, you are connected!" to the client via the socket connection.
1#include <stdio.h>2#include <stdlib.h>3#include <errno.h>4#include <string.h>5#include <sys/types.h>6#include <netinet/inch.h>7#include <sys/socket.h>8#include <sys/wait.h>9 Ten #defineServport 3333//Server listener Port number One# define BACKLOGTen //maximum number of simultaneous connection requests A - Main () - { the intSOCKFD, CLIENT_FD;//SOCK_FD: Monitor socket; CLIENT_FD: data transfer socket - intsin_size; - structSockaddr_in my_addr;//Native Address information - structSockaddr_in remote_addr;//Client Address information + - if(SOCKFD = socket (af_inet, Sock_stream,0)) == -1){ +Perror ("Socket creat error!"); AExit (-1); at}//set up sockets - -my_addr.sin_family =af_inet; -my_addr.sin_port=htons (servport); -MY_ADDR.SIN_ADDR.S_ADDR =Inaddr_any; -Bzero (& (My_addr.sin_zero),8); in - if(Bind (SOCKRFD, (structSOCKADDR *) &my_addr,sizeof(structSOCKADDR)) ==-1) to { +Perror ("bind error!"); -Exit1); the}//binding * $ if(Listen (SOCKFD, BACKLOG) = =-1)Panax Notoginseng { -Perror ("Listen error!"); theExit1); +}//Monitor A the while(1){ +Sin_size=sizeof(structsockaddr_in); - if(client_fd = Accept (SOCKFD, (structSOCKADDR *) &remote_addr, &sin_size)) = =-1) $ { $Perror ("Accept error!"); - Continue; - } theprintf"received a connection from%s\n", Inet_ntoa (REMOTE_ADDR.SIN_ADDR)); - if(!Fork ()) {Wuyi if(Send (CLIENT_FD,"Hello, you are connected!\n", -,0) ==-1) thePerror ("Send error!"); - Close (CLIENT_FD); WuExit0); - } About Close (CLIENT_FD); $ } -}
TCP Socket Programming