C ++ to implement TCP / UDP network programming process and the main function uses the method described

socket communication

Network byte order

  • Little-Endian: high upper memory address, low address memory is low. (Storage intel architecture)
  • Big-Endian: address high low deposit, low deposit high address. (Transmission mode network)
#include <arpa/inet.h>

// 将本地转网络,转IP  转192.168.1.11->string->atoi->int->htonl->网络字节序,可以使用 int inet_pton(); 进行直接转换
uint32_t  htonl(uint32_t hostlong); 
// 本地转网络,转port
uint16_t  htons(uint16_t hostshort);
// 网络转本地,转ip
uint32_t ntohl(uint32_t netlong);
// 网络转本地,转port
uint16_t ntohs(uint16_t netshort);
// string转网络字节
int inet_pton(int af, const char * restrict src, void * restrict dst);
// af: AF_INET, AF_INET6
// src: ip地址,点分十进制
// dst: 转换之后的 网络字节序的地址

To create a socket server

  1. Create a socket handle
  2. bind () bind ip + port
  3. listen () is provided on the line monitor, the number of simultaneous connections
  4. accept () blocking listen for client connection
  5. read () to read data, the read data requires toupper () for rotation uppercase to lowercase
  6. write () is written, the value written back to the client
  7. When the read () read 0 is the close of when to close () Close

Socket function analysis

Create a socket

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

  • domain: the selected ip address protocol, AF_INET, AF_INET6
  • type: Type SOCK_STREAM (TCP / stream form), SOCK_DGRAM (UDP / packets form)
  • protocol: Representation Agreement No. 0

return value:

  • Successful return 0, the new socket file description

  • Failure: -1

#include <sys/socket.h>

fd = socket(AF_INET, SOCK_STREAM, 0)

sockaddr address structure analysis

#include <sys/socket.h>
#include <arpa/inet.h>

struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(9527);
int dst;
inet_pton(AF_INET, "192.168.22.45", (void *)&dst);
addr.sin_addr.s_addr = dst;
addr.sin_addr.s_addr = htonl(INADDR_ANY) // INADDR_ANY 取出系统有效的任意ip地址,是二进制类型
bind(fd, (struct sockaddr *)&addr, size);

Analysis of the bind function

Binding function

bind(fd, (struct sockaddr *)&addr, size);

  • fd: socket file object
  • (Struct sockaddr *) & addr: the address structure sockaddr stored cast to be passed sockaddr
  • size: the size of addr, sizeof be acquired using

listen function analysis

Monitor function

int listen(int sockfd, int backlog);

  • sockfd: Socket
  • backlog: maximum number of connections, a maximum of 128
  • Return Value: 0, -1 error

accept function analysis

Congestion function

int accept(int sockfd, struct sockaddr addr, socklen_t addrlen);

  • sockfd : socket 函数返回值
  • addr : 传出参数, 成功返回服务器的ip和端口号
  • addrlen : 传入传出。入: addr的大小。 出:客户端addr的实际大小

返回值:

  • 成功: 大于0,返回成功的套接字文件描述符
  • 失败:返回-1

connect函数分析

int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); 使用现有的socket与服务器建立连接

  • sockfd: socket 函数返回值
  • addr: 传入服务器的地址结构
  • 返回值:
    • 成功:0
    • 失败:-1

Guess you like

Origin www.cnblogs.com/fandx/p/12150672.html