Linux_c++ TCP socket programming function

socket function

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

domain is the protocol cluster, and the values ​​are as follows

  • AF_INET: Use Ipv4 protocol
  • AF_INET6: Use Ipv6 protocol
  • AF_UNIX: Local communication, generally refers to when the client and server are on the same machine.
  • AF_NETLINK: communication between kernel and user mode

type refers to the type of socket, the values ​​are as follows

  • SOCK_STREAM: Data stream communication, that is, TCP communication is used.
  • SOCK_DGRAM: Data packet communication, that is, UDP communication is used.

protocol is generally 0

head File#include<sys/types.h> #include<sys/socket.h>

Return value: If the
function succeeds, it returns a positive integer, which is the socket descriptor. If the function fails, it returns -1.

#include<sys/types.h> //基本系统数据类型
#include<sys/socket.h>
#include<iostream>
using namespace std;

int main()
{
    
    
	int sockfd;
	sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(sockfd < 0)
	{
    
    
		cout<<"create socket error"<<endl;		
	}
	cout<<sockfd<<endl;
	return 0;
}

bin function

The bin function is a binding address space function. In the socket structure, there is a default IP address and a default port number, but the server program must call the bind function to bind its own IP address information and a specific port number.
int bind(int sockfd,const struct sockaddr* addr,socklen_t addrlen)

  • sockfd: Identifies a socket descriptor to be bound
  • addr: is a sockadddr structure, which has 3 forms, namely Ipv4, Ipv6, Unix
  • addrlen: Specify the buffer length of addr
//Ipv4地址域
struct sockaddr_in{
    
    
	sa_family_t sin_family;  //地址簇AF_INET
	in_port_t sin_port;     //端口
	struct in_addr sin_addr; //地址
};

struct in_addr{
    
    
	uint32_t s_addr; //网络字节序
};
//Ipv6地址域
struct sockaddr_in6{
    
    
	sa_family_t sin6_family;  //AF_INET6
	in_port_t sin6_port;	//端口
	uint32_t sin6_flowinfo;  
	struct in6_addr sin6_addr;
	uint32_t sin6_scope_id; 
}
struct in6_addr{
    
    
	unsigned char s6_addr[16];
};

Use of bin function

#include<sys/types.h> //基本系统数据类型
#include<sys/socket.h>
#include<iostream>
using namespace std;

int main()
{
    
    
	struct sockaddr_in sockaddr;
	int port = 3000;
	int socketfd;
	memset(&sockaddr,0,sizeof(sockaddr));
	sockaddr.sin_family  = AF_INET;
	sockaddr.sin_addr.s_addr = htonl(127.0.0.1);
	sockaddr.sin_port = htons(port);
	socketfd = socket(AF_INET,SOCK_STREAM,0);
	if(sockfd == -1)
	{
    
    
		cout<<"socket create error"<<endl;
		return -1;
	}
	return 0;
}

listen function

Mainly used for server-side monitoring.
int listen(int pid,int backlog);
pid: socket descriptor for monitoring
backlog: indicates the maximum number of connections for connection requests

accept function

Used by the server program to get the first client request from the client connection request queue of the streaming socket in the listening state, and create a new socket for communication with the client.
int accetp(int pid,struct sockaddr* addr,socklen_t *addrlen)

  • pid: is the streaming socket descriptor in the listening state
  • addr: get the client's protocol address, including Ip and port number
  • addrlen: The size of the client address structure.
struct sockaddr_in NewSocket;
int addrlen;
addrlen = sizeof(NewSocket);
int NewServerSocket = accept(ListenSocket,(struct sockaddr*)&NewSocket,&addrlen);
if(NewServerSocket == -1)
{
    
    
	cout<<"accept socket error"<<endl;
}

connect function

Used by the client to connect to the server
int connect(int pid,const struct sockaddr* name,socklen_t namelen)

  • pid: socket descriptor that has not yet been connected
  • name: pointer to the address structure of the server socket
  • namelen: the size of the socket address structure

If the function executes successfully, it returns 0, otherwise it returns -1

write function

Both the client and the server can use the write function to send data to the other end of the TCP connection.
int write(int pid,const char* buf,int len)

  • pid: the socket of the sender
  • buf: send buffer
  • len: the size of the buffer

Return value: returns the number of bytes actually sent

read function

Both the client and the server can use the read function to receive data.
int read(int pid,char *buf,int len)

  • pid: socket descriptor
  • buf: buffer, used to store the read data
  • len: the size of the buffer

Return value: Returns the number of bytes actually read, if the connection is closed, it returns 0

send function

The function is similar to the write function
size_t send(int pid,const char* buf,size_t len,int flag)

The meaning of the first three parameters is similar to write, and flag is the transmission control flag.

  • 0: means regular, similar to write
  • MSG_DONTROUTE: Notify the kernel that the destination host is on the directly connected local network
  • MSG_DONTWAIT: Set a single I\O operation to non-blocking mode

Return value: returns the number of bytes actually sent

recv function

The function is similar to the read function
ssize_t recv(int sockfd,void *buf,size_t len,ing flag)
. The meaning of the first three parameters is similar to read, and flag is the transmission control flag.

  • 0: indicates normal, similar to read
  • MSG_DONTWAIT: Set a single I\O operation to non-blocking mode
  • MSG_OOB: The reading data is out-of-band data instead of general data

Return value: Returns the number of bytes actually read, if the connection is closed, it returns 0

close

Close socket

int close(int sockfd)

Return 0 on success, -1 on failure

Guess you like

Origin blog.csdn.net/qq_42708024/article/details/111357206