Address Translation Functions :
1. Convert the IP address of the string to the address of the network byte order
- in_addr_t inet_addr (const char *strip);
- If successful, returns the address of the 32-bit network byte order, if error, returns Inaddr_none is not recommended, because 255.255.255.255 this address cannot be converted
- int Inet_aton (const char *strip, struct in_addr *addr);
- If successful, returns 0 if the address string is incorrect, returns 0, converts only the IPV4 address
- int Inet_pton (int family, const char *strptr, void *addrptr);
- Returns: 1 success, 0-input is not a valid expression,-1 error family:af_inet6,af_inet
- 2. Convert the address of the network byte order to the IP address of the string
- Char *inet_ntoa (struct in_addr inaddr);
- Returns: A pointer to the string IP. Non-thread safe, avoid use cases when multithreading: char *ip = Inet_ntoa (ADDR.SIN_ADDR);
- const char *inet_ntop (int family, const void *addr, char *strip, size_t len);
- Parameters: family– Address family, value af_inet or Af_inet6 addr– the address to be converted returns: pointer to string IP, failure to return null; Example: Char ip[64]; Inet_ntop (af_inet, &ADDR.SIN_ADDR, IP, 64);
- Creating sockets
- 1. Socket () int socket (int family, int type, int protocol);
- Function Description: Creates a socket file description word. Parameters: family– Protocol family (Network layer) pf_inet--------IPV4 Protocol Pf_inet6--------IPV6 protocol
- Pf_local--------Unix Domain Protocol (pf_local) Pf_route-------Routing Socket Interface type– socket type SOCK_STREAM--------byte throttle socket (TCP and Ac_local) Sock_ Dgram---------Datagram Socket Interface (UDP and ac_local) protocol– take 0 return: Success is a non-negative descriptor, failure returns-1 example: int fd = socket (pf_inet, sock_stream, 0);
- 2. Connect ()
int connect (int sock, const struct SOCKADDR *addr, socklen_t len);
Function Description: Establish a connection from the client to the server returns: Successfully returned 0, failed to return-1
- 3 recv () int recv (int sock, void *buf, int size, int flag);
- Function Description: Receives data from the sock socket. The server uses it to receive a customer request and the customer uses it to accept the server's response. If there is no data, it will block. You can also use recv to receive connection-oriented UDP messages.
- Parameter SOCKFD: Socket descriptor BUF: Pointer to memory block size: Memory block sizes in bytes Flags: typically 0 returns: Number of bytes received by >0-1 receive failed (eagain-socket non-blocking, eintr signal interrupted) 0 connection has been Off, stating that the socket is not available and that a connection needs to be re-established
- 4 Send () ssize_t send (int sock, const void *buf, size_t len, int flags);
- The function sends data on a TCP connection, returns the length of the data that was successfully transmitted, and returns-1 on error. Send copies the outgoing data to the OS kernel, or you can use Send to send a connection-oriented UDP message.
- Parameter description: Sock: Socket descriptor data: Pointer to the data to be sent Data_len: data length flags: Always 0
5 close () int close (int s); function closes the socket.
Parameter description: S-to close the socket
- Connect Program
1#include <unistd.h>2#include <stdio.h>3#include <sys/socket.h>4#include <netinet/inch.h>5#include <arpa/inet.h>6#include <pthread.h>7 8 intMain ()9 {Ten intsock,size; Oneuint16_t Port =8000; A Const Char*ip ="192.168.7.118"; - Charbuf[1024x768] ="Songlongfei"; - structsockaddr_in addr; thememset (&ADDR,0,sizeof(addr)); -Sock = socket (Af_inet,sock_stream,0); -addr.sin_family =af_inet; -Addr.sin_port =htons (port); +Inet_pton (af_inet,ip,&addr.sin_addr); - if(Connect (sock,structsockaddr*) &addr,sizeof(addr)) !=0) +printf"Connect failed\n"); A while(1) at { -Fgets (BUF,sizeof(BUF), stdin); - if(size = Send (Sock,buf,sizeof(BUF),0)) <0) -printf"Send failed\n"); - Else -printf"Connect succ!\n"); inRecv (Sock,buf,sizeof(BUF),0); -printf"%s\n", buf); to } + Close (sock); -}
Linux application Programming-Network Programming Chapter