Network IPC: Sockets
Using the socket to implement communication between two different hosts (involving some basic computer network knowledge is skipped.) )
Server-side:
1.socket function: Generate a socket
int socket (int domain,int type,int protocol);
Parameter resolution: Domain:{af_inet:ipv4 network protocol, Af_inet6:ipv6 Network protocol}
Type:{tcp:sock_stream,udp:sock_dgram}
Protocol: Specifies the transmission protocol number used by the socket, typically 0;
2.bind function
Associating a socket with an address
int bind (int sockfd,const struct sockaddr *addr,socklen_t len);
SOCKFD: Socket *ADDR: Address structure addresses Len: Length of address structure
In IPV4, the socket address is represented by the structure sockaddr_in:
struct SOCKADDR_IN {
sa_family_t sin_family; Communication domain, typically af_inet
in_port_t Sin_port; interface address, Binary
struct IN_ADDR sin_addr; IP address, binary
}
3.listen function: The server IP and this port is in the listening state, if a client in the network has a connection request, accept the request.
int listen (int sockfd,int backlog);
SOCKFD: Socket Backlog: The maximum number of requests that the server can accept, typically 10, and a maximum of 128
4.accpet function: Accept the client's request to establish a communication connection with the client side. When the server is listening, the client has a connection request and the server does not process it immediately, but instead adds the request to the waiting queue and waits until the server is idle. Processing generates a new socket, which is used for communication between the server and the client. The original socket socket continues to be used for listening.
int accept (int s,struct sockaddr *addr,int *addrlen)
S:socket return value addr: struct pointer variable, and bind is the same type (the system will put the IP and port number of the remote client into this pointer variable) ADDRLEN: Structure Body length
If successful, the value returned is the new socket socket
5.RECV function: Use new sockets to accept data from remote clients and store data in parameter buf
Prototype: int recv (int sockfd,void *buf,int len,unsigned int flags);
Parameter: Sockfdà is the return value of the preceding accept. That is, NEW_FD, which is the new socket.
Bufà represents a buffer
Lenà indicates the length of the buffer
Flagsà is typically 0
Success returns the length of the accepted data.
6.send function: Send data to a remote client with a new socket
Prototype: int send (int s,const void * Msg,int len,unsigned int flags);
Parameter: S is the return value of the preceding accept. that is NEW_FD
MSG is generally a constant string
Len indicates length
The flags are usually 0
Client:
1. Connect function: Used to request a connection to a remote server, connect the socket of the parameter SOCKFD to the server IP and port number specified in the parameter serv_addr.
Prototype: int connect (int sockfd,struct sockaddr * serv_addr,int addrlen);
Parameter: SOCKFD is the return value of the front socket, which is the SFD
SERV_ADDR is a struct pointer variable that stores the IP and port number information for a remote server.
Addrlen represents the length of a struct variable
Return value: Success returns 0, failure returns-1
2.Close function: When you are finished using the file, you can close the file using close () if it is no longer needed, and close () causes the data to be written back to disk and frees the resources that the file occupies
Prototype: int close (int fd);
Parameter: FD for the front sfd,new_fd
Return value: Returns 0 if the file is closed successfully, returns 1 if an error occurs
Sample: A simple communication process. PS: The program does not use the RECV function and the Send function, but instead uses the read and write functions
Server-side:
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <sys/types.h>5#include <arpa/inet.h>6#include <sys/socket.h>7#include <netinet/inch.h>8 intMainintargcChar*argv[])9 {Ten intFd_listen; OneFd_listen=socket (Af_inet,sock_stream,0);//Sockets A - if(fd_listen==-1) - { thePerror ("Socket"); -Exit1); - } - structSockaddr_in seraddr;//Address structure Body +memset (&SERADDR,0,sizeof(SERADDR)); -seraddr.sin_family=af_inet; +Seraddr.sin_port=htons (1234);//Port ASERADDR.SIN_ADDR.S_ADDR=INET_ADDR ("192.168.1.182");//IP Address at if(-1==bind (Fd_listen, (Const structsockaddr*) &seraddr, (socklen_t)sizeof(SERADDR))) - { -Perror ("Bind"); - Close (fd_listen); -Exit1); - } inListen (Fd_listen,Ten);//Listener Functions - to structsockaddr_in peeraddr; +memset (&PEERADDR,0,sizeof(PEERADDR)); -Socklen_t len=sizeof(PEERADDR); the intFd_peer=accept (Fd_listen, (structsockaddr*) &peeraddr,&len); * $printf"who:%s:%d\n", Inet_ntoa (PEERADDR.SIN_ADDR), Ntohs (Peeraddr.sin_port));Panax Notoginseng if(fd_peer==-1) - { thePerror ("Accept"); + Close (fd_listen); AExit1); the } + Charbuf[1024x768]=""; - intReadn=read (Fd_peer,buf,1023);//reading data from a new socket $printf"readn:%d,msg:%s\n", readn,buf); $ Char*p="-------------------"; - Write (Fd_peer,p,strlen (p)); - Close (fd_listen); the Close (fd_peer); - return 0;Wuyi}View Code
Client:
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <sys/types.h>5#include <sys/socket.h>6#include <arpa/inet.h>7#include <netinet/inch.h>8 intMainintargcChar*argv[])9 {Ten intSfd=socket (Af_inet,sock_stream,0); One if(sfd==-1) A { -Perror ("Socket"); -Exit1); the } - structsockaddr_in peeraddr; -peeraddr.sin_family=af_inet; -Peeraddr.sin_port=htons (1234); +PEERADDR.SIN_ADDR.S_ADDR=INET_ADDR ("192.168.1.182"); - + if(-1==connect (SFD, (structsockaddr*) &peeraddr,sizeof(PEERADDR)))//Request a connection, add your own information to the socket A { atPerror ("Connect"); - Close (SFD); - } - Char*p="Hello World"; - Write (Sfd,p,strlen (p)); - Charbuf[1024x768]=""; in intReadn=read (Sfd,buf,1023); -printf"readn:%d:%s\n", readn,buf); to Close (SFD); + return 0; -}View Code
Summary: This program simulates the TCP protocol to realize the simple communication between server and client side, and introduces the whole process.
Introduction to basic socket programming