提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
提示:这里可以添加本文要记录的大概内容:
介绍linux socket 判断连接是否断开 send和recv函数
提供示例代码: ser端 3秒发送一个命令给 cli端,然后cli端处理命令
提示:以下是本篇文章正文内容,下面案例可供参考
一、如何判断连接是否断开 send和recv函数?
send:
send() 或 sendto() 函数发送数据时,当尝试向一个已经关闭的套接字发送数据时,默认情况下会生成 SIGPIPE 信号 给你的进程,导致进程退出
可以使用 MSG_NOSIGNAL 标志来避免生成 SIGPIPE 信号。
c
ssize_t send_received = send(clifd, cmd_buf, sizeof(cmd_buf) - 1, MSG_NOSIGNAL);
printf("send_received = %d\n", send_received);
if (send_received == -1) {
if (errno == EPIPE) {
printf("Client disconnected.\n");
} else {
perror("Send failed");
}
close(clifd);
return -1;
}
recv:
可以判断返回值 recv返回 0表述对端关闭
c
ssize_t bytes_received = recv(sockfd, cmd_buf, sizeof(cmd_buf) - 1, 0);
if (bytes_received < 0) {
perror("recv failed");
close(sockfd);
return -1;
} else if (bytes_received == 0) {
printf("ser closed\n");
close(sockfd);
return 0;
}
二、代码示例
socket_cmd.h
c
#ifndef __SOCKET_CMD_H__
#define __SOCKET_CMD_H__
typedef enum {
CMD_TYPE_TEST = 0x1100,
CMD_TYPE_EXEC,
CMD_TYPE_DOWNLOAD,
CMD_TYPE_UPLOAD,
CMD_TYPE_MAX,
} cmd_type_t;
typedef enum {
OP_TYPE_TEST = 0x2100,
OP_TYPE_REQ,
OP_TYPE_ACK,
OP_TYPE_FAIL,
OP_TYPE_MAX,
} op_type_t;
struct s_cmd
{
int cmd;
int op;
char msg[256];
};
#endif
socket_cmd_ser.c
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include "socket_cmd.h"
#define SERVER_IP "127.0.0.1" // 服务器的 IP 地址
#define SERVER_PORT 8080 // 服务器的端口号
int ser_sock_create(char * addr, int port, int listen_num)
{
char s_ip[16] = {0};
int sockfd;
int clifd;
struct sockaddr_in server_addr = {0};
struct sockaddr_in client_addr = {0};
char cmd_buf[512] = {0};
int ret = 0;
int addr_len = 0;
if(addr == NULL || port > 65535 || port < 0 || listen_num <= 0 || listen_num > 1024)
{
return -1;
}
printf("addr = %s, port = %d, listen_num = %d\n", addr, port, listen_num);
memcpy(s_ip, addr, strnlen(addr, 16));
// 创建套接字
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("socket creation failed");
return -1;
}
// Set the SO_REUSEADDR option
int optval = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) {
perror("Setsockopt failed");
close(sockfd);
return 1;
}
// 设置客户端地址信息
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port); // 客户端绑定的端口号
server_addr.sin_addr.s_addr = inet_addr(s_ip); // 绑定所有可用接口
// 绑定套接字到客户端地址
if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind failed");
close(sockfd);
return -1;
}
addr_len = sizeof(client_addr);
// 获取并打印绑定的 IP 地址和端口号
if (getsockname(sockfd, (struct sockaddr *)&server_addr, &addr_len) == -1) {
perror("getsockname failed");
close(sockfd);
return -1;
}
char ip_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &server_addr.sin_addr, ip_str, sizeof(ip_str));
printf("ser bind %s:%d\n", ip_str, ntohs(server_addr.sin_port));
// 开始监听连接
if (listen(sockfd, listen_num) < 0) {
perror("Listen failed");
return -1;
}
clifd = accept(sockfd, NULL, NULL);
if (clifd < 0) {
perror("Accept failed");
return -1;
}
// 这里可以添加发送和接收数据的代码
while(1)
{
memset(cmd_buf, 0, sizeof(cmd_buf));
struct s_cmd cmd = {0};
cmd.cmd = CMD_TYPE_TEST;
cmd.op = OP_TYPE_TEST;
memcpy(cmd.msg, "hello cmd", strnlen( "hello cmd", 128));
memcpy(cmd_buf, &cmd, sizeof(cmd));
ssize_t send_received = send(clifd, cmd_buf, sizeof(cmd) , MSG_NOSIGNAL);
//ssize_t send_received = write(clifd, cmd_buf, sizeof(cmd_buf) - 1);
printf("send_received = %d\n", send_received);
if (send_received == -1) {
if (errno == EPIPE) {
printf("Client disconnected.\n");
} else {
perror("Send failed");
}
close(clifd);
return -1;
}
usleep(3* 1000 * 1000);
}
// 关闭套接字
close(sockfd);
}
int main() {
int sockfd;
ser_sock_create("0.0.0.0", 9090, 1);
return 0;
}
socket_cmd_cli.c
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "socket_cmd.h"
#define SERVER_IP "127.0.0.1" // 服务器的 IP 地址
#define SERVER_PORT 8080 // 服务器的端口号
#define CLIENT_PORT 19091 // 客户端绑定的端口号
int cli_cmd_dispatch(struct s_cmd * cmd)
{
struct s_cmd* tmp_cmd = NULL;
if(cmd == NULL)
{
return -1;
}
tmp_cmd = malloc(sizeof(struct s_cmd));
if(tmp_cmd == NULL)
{
return -1;
}
memcpy(tmp_cmd, cmd, sizeof(struct s_cmd));
switch(tmp_cmd->cmd)
{
case CMD_TYPE_TEST:
break;
default:
break;
}
free(tmp_cmd);
return 0;
}
int cli_check_cmd_valid(struct s_cmd * cmd)
{
if(cmd == NULL)
{
return -1;
}
return 0;
}
int cli_sock_create(char * seraddr, int serport, char * cliaddr, int cliport)
{
char s_ip[16] = {0};
char c_ip[16] = {0};
int sockfd;
struct sockaddr_in server_addr = {0};
struct sockaddr_in client_addr = {0};
char cmd_buf[512] = {0};
int ret = 0;
int addr_len = 0;
if(seraddr == NULL || serport > 65535 || serport < 0 || cliport < 0 || cliport > 65535)
{
return -1;
}
if(cliaddr == NULL)
{
memcpy(c_ip, "0.0.0.0", strnlen("0.0.0.0", 16));
}else
{
memcpy(c_ip, cliaddr, strnlen(cliaddr, 16));
}
memcpy(s_ip, seraddr, strnlen(seraddr, 16));
// 创建套接字
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
// 设置客户端地址信息
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(CLIENT_PORT); // 客户端绑定的端口号
client_addr.sin_addr.s_addr = inet_addr(c_ip); // 绑定所有可用接口
// 绑定套接字到客户端地址
if (bind(sockfd, (struct sockaddr *)&client_addr, sizeof(client_addr)) < 0) {
perror("bind failed");
close(sockfd);
exit(EXIT_FAILURE);
}
addr_len = sizeof(client_addr);
// 获取并打印绑定的 IP 地址和端口号
if (getsockname(sockfd, (struct sockaddr *)&client_addr, &addr_len) == -1) {
perror("getsockname failed");
close(sockfd);
exit(EXIT_FAILURE);
}
char ip_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &client_addr.sin_addr, ip_str, sizeof(ip_str));
printf("cli bind %s:%d\n", ip_str, ntohs(client_addr.sin_port));
// 设置服务器地址信息
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(serport);
server_addr.sin_addr.s_addr = inet_addr(s_ip);
// 连接到服务器
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connection to server failed");
close(sockfd);
exit(EXIT_FAILURE);
}
printf("connetct to ser %s:%d\n", s_ip, serport);
// 这里可以添加发送和接收数据的代码
while(1)
{
struct s_cmd cmd = {0};
memset(cmd_buf, 0, sizeof(cmd_buf));
ssize_t bytes_received = recv(sockfd, cmd_buf, sizeof(struct s_cmd), 0);
if (bytes_received < 0) {
perror("recv failed");
close(sockfd);
return -1;
} else if (bytes_received == 0) {
printf("ser closed\n");
close(sockfd);
return 0;
}
memcpy(&cmd, cmd_buf, sizeof(struct s_cmd));
printf("bytes_received = %d, cmd.cmd = 0x%x, cmd.op = 0x%x, cmd.msg = %s\n", bytes_received, cmd.cmd, cmd.op, cmd.msg);
if(cli_check_cmd_valid(&cmd) == 0)
{
cli_cmd_dispatch(&cmd);
}
}
// 关闭套接字
close(sockfd);
}
int main() {
int sockfd;
cli_sock_create("127.0.0.1", 9090, NULL, 0);
return 0;
}
总结
socket通信 ser端 3秒发送一个命令给 cli端,然后cli端处理命令的代码和如何判断socket对端断开连接。