1. socket
1.1. vim man view socket
:!man socket
1.2. Dependent header files
#include <sys/types.h>
#include <sys/socket.h>
1.3. Prototype
int socket(int domain, int type, int protocol);
domain | illustrate |
---|---|
OF_INET | IPv4 protocol |
AF_INET6 | IPv6 protocol |
AF_LOCAL | Unix Domain Protocol |
type | illustrate |
---|---|
SOCK_STREAM | Byte stream socket (TCP/SCTP) |
SOCK_DGRAM | Datagram socket (UDP) |
SOCK_RAM | Raw sockets (IPv4/IPv6) |
SOCK_SEQPACKET | Ordered Packet Sockets (SCTP) |
protocol | illustrate |
---|---|
IPPROTO_CP | TCP transport protocol |
IPPROTO_UDP | UDP transport protocol |
IPPROTO_SCTP | SCTP transfer protocol |
The socket function will return a small non-negative integer value when successful , which is similar to a file descriptor, so it is called a socket descriptor . Just specify the protocol family (IPV4, IPV6 or Unix) and socket type
- Code
socket_fd=socket(AF_INET,SOCK_STREAM,0);
2. connect
2.1. Prototype
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
The connect() function is used in client applications to establish a connection with the server. Note that the connect() function may block until the connection is successfully established or an error occurs . Therefore, you can use appropriate timeout mechanisms or non-blocking Socket settings to control the behavior of the connection process.
Enter:
- sockfd: The file descriptor of the client Socket to be connected.
- addr: Pointer to the server address structure, which contains the server's IP address and port number.
- addrlen: The length of the server address structure.
return value:
- 0 means the connection is successful
- A return value of -1 indicates that the connection failed, and the corresponding error code is set (you can use the perror() function to print error information).
2.2. Code
#define SERVER_PORT 8596
#define MESSAGE_LENGTH 1024
int ret = -1;
int socket_fd;
//server addr
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVER_PORT);
//inet_addr()函数,将点分十进制IP转换成网络字节序IP
serverAddr.sin_addr.s_addr = inet_addr(args[1]);
if(connect(socket_fd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0){
perror("connect error");
return 1;
}
3. send
3.1. Prototype
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
send is a function used in network programming to send data to a connected socket or send datagrams to a socket.
Enter:
- sockfd: Integer parameter, indicating the file descriptor of the socket to send data.
- buf: Pointer to the buffer to send data to.
- len: unsigned integer parameter, indicating the length of the data to be sent.
- flags: Integer parameter used to specify optional flags for the send operation. Common flags include 0 (no special flag) and MSG_DONTWAIT (non-blocking mode transmission).
return value
- If the data is sent successfully, send returns the number of bytes sent.
- If the connection is closed (for a stream socket) or the send times out (for a datagram socket), send returns 0.
- If an error occurs, send returns -1 and sets the corresponding error code. You can obtain specific error information through the errno global variable.
3.2. Source code
#define MESSAGE_LENGTH 1024
char sendbuf[MESSAGE_LENGTH];
while(1)
{
memset(sendbuf, 0, MESSAGE_LENGTH);
printf("send message:");
fgets(sendbuf,MESSAGE_LENGTH,stdin);
//printf("\n");
ret = send(socket_fd, sendbuf, strlen(sendbuf), 0);
if(ret <= 0 ){
printf("the connection is disconnection!\n");
break;
}
}
4. recv
4.1. Prototype
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
recv is a function used in network programming to receive data from a connected socket or to receive datagrams from a socket.
It should be noted that the recv function is a blocking call , that is, it will wait until no data arrives. If non-blocking operations are required, non-blocking I/O or multi-thread/multi-process methods can be used to handle receive operations.
Enter:
- sockfd: Integer parameter, indicating the file descriptor of the socket to receive data.
- buf: Pointer to the receive data buffer, used to store received data.
- len: unsigned integer parameter, indicating the length of the receive data buffer.
- flags: Integer parameter used to specify optional flags for the receive operation. Common flags include 0 (no special flag), MSG_DONTWAIT (non-blocking mode reception) and MSG_WAITALL (blocking mode reception until data of the specified length is received), etc.
return value:
- If data is received successfully, recv returns the number of bytes received.
- recv returns 0 if the connection is closed (for a stream socket) or the receive times out (for a datagram socket).
- If an error occurs, recv returns -1 and sets the corresponding error code. Specific error information can be obtained through the errno global variable.
5. Complete code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERVER_PORT 8596
#define MESSAGE_LENGTH 1024
int main(int argc,char* args[])
{
int ret = -1;
int socket_fd;
//server addr
struct sockaddr_in serverAddr;
char sendbuf[MESSAGE_LENGTH];
char recvbuf[MESSAGE_LENGTH];
int data_len;
if((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("socket");
return 1;
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVER_PORT);
//inet_addr()函数,将点分十进制IP转换成网络字节序IP
serverAddr.sin_addr.s_addr = inet_addr(args[1]);
if(connect(socket_fd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
{
perror("connect");
return 1;
}
printf("success to connect server...\n");
while(1)
{
memset(sendbuf, 0, MESSAGE_LENGTH);
printf("send message:");
fgets(sendbuf,MESSAGE_LENGTH,stdin);
//printf("\n");
ret = send(socket_fd, sendbuf, strlen(sendbuf), 0);
if(ret <= 0 ){
printf("the connection is disconnection!\n");
break;
}
if(strcmp(sendbuf, "quit") == 0){
break;
}
printf("receive message:");
recvbuf[0] = '\0';
data_len = recv(socket_fd, recvbuf, MESSAGE_LENGTH, 0);
recvbuf[data_len] = '\0';
printf("%s\n", recvbuf);
}
close(socket_fd);
return 0;
}