网络知识 科技 嵌入式中,TCP通信常用接口的使用封装

嵌入式中,TCP通信常用接口的使用封装

大家好,我是杂烩君。

之前我们的文章中也有分享TCP相关的过程,一个TCP通信的框图如:

嵌入式中,TCP通信常用接口的使用封装

这个图中也列出了常用的几个接口函数,其中,有几个函数调用时传参比较繁琐,为了提高我们的编码效率,有必要对这些接口的使用进一步地封装,使得函数调用更简单些。

TCP常用接口使用封装

下面给大家整理一份:

tcp_socket.h:

#ifndef TCP_SCOKET_Hn#define TCP_SCOKET_Hnn#include <stdio.h>n#include <stdlib.h>n#include <string.h>n#include <sys/types.h>n#include <sys/socket.h>n#include <netinet/in.h>n#include <arpa/inet.h>n#include <unistd.h>n#include <signal.h>nn#define MAX_CONNECT_NUM 10nnint tcp_init(const char* ip, int port);nint tcp_accept(int sfd);nint tcp_connect(const char* ip, int port);nint tcp_nonblocking_recv(int conn_sockfd, n void *rx_buf, n int buf_len, n int timeval_sec, n int timeval_usec);nint tcp_blocking_recv(int conn_sockfd, void *rx_buf, uint16_t buf_len);nint tcp_send(int conn_sockfd, uint8_t *tx_buf, uint16_t buf_len);nvoid tcp_close(int sockfd);nn#endif

tcp_socket.c:

#include "tcp_socket.h"nnint tcp_init(const char* ip, int port)n{n int optval = 1; n int server_fd = socket(AF_INET, SOCK_STREAM, 0);n if (server_fd < 0)n {n perror("socket");n return -1;n }nn /* 解除端口占用 */n if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0)n {n perror("setsockoptn");n return -1;n }nn struct sockaddr_in server_addr;n bzero(&server_addr, sizeof(struct sockaddr));n server_addr.sin_family = AF_INET;n server_addr.sin_port = htons(port);n if (NULL == ip)n {n server_addr.sin_addr.s_addr = htonl(INADDR_ANY); n }n elsen {n server_addr.sin_addr.s_addr = inet_addr(ip); n }nn if (bind(server_fd, (struct sockaddr*)&server_addr,sizeof(struct sockaddr)) < 0)n {n perror("bind");n close(server_fd);n return -1;n }nn if(listen(server_fd, MAX_CONNECT_NUM) < 0)n {n perror("listen");n close(server_fd);n return -1;n }nn return server_fd;n}nnint tcp_accept(int server_fd)n{n struct sockaddr_in client_addr = {0};n int addrlen = sizeof(struct sockaddr);n int new_fd = accept(server_fd, (struct sockaddr*) &client_addr, &addrlen);n if(new_fd < 0)n {n perror("accept");n close(server_fd);n return -1;n }n n return new_fd;n}nnint tcp_connect(const char *ip, int port)n{n int server_fd = socket(AF_INET, SOCK_STREAM, 0);n if (server_fd < 0)n {n perror("socket");n return -1;n }nn struct sockaddr_in server_addr;n bzero(&server_addr, sizeof(struct sockaddr));n server_addr.sin_family = AF_INET;n server_addr.sin_port = htons(port);n server_addr.sin_addr.s_addr = inet_addr(ip);nn if (connect(server_fd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr)) < 0)n {n perror("connect");n close(server_fd);n return -1;n }nn return server_fd;n}nnint tcp_nonblocking_recv(int conn_sockfd, void *rx_buf, int buf_len, int timeval_sec, int timeval_usec)n{n fd_set readset;n struct timeval timeout = {0, 0};n int maxfd = 0;n int fp0 = 0;n int recv_bytes = 0;n int ret = 0;n n timeout.tv_sec = timeval_sec;n timeout.tv_usec = timeval_usec;n FD_ZERO(&readset); n FD_SET(conn_sockfd, &readset); nn maxfd = conn_sockfd > fp0 ? (conn_sockfd+1) : (fp0+1); nn ret = select(maxfd, &readset, NULL, NULL, &timeout); n if (ret > 0) n {n if (FD_ISSET(conn_sockfd, &readset)) n {n if ((recv_bytes = recv(conn_sockfd, rx_buf, buf_len, MSG_DONТWAIT))== -1) n {n perror("recv");n return -1;n }n }n } n else n {n return -1;n }n n return recv_bytes;n}nnint tcp_blocking_recv(int conn_sockfd, void *rx_buf, uint16_t buf_len)n{n return recv(conn_sockfd, rx_buf, buf_len, 0);n}nnint tcp_send(int conn_sockfd, uint8_t *tx_buf, uint16_t buf_len)n{n return send(conn_sockfd, tx_buf, buf_len, 0);n}nnvoid tcp_close(int sockfd)n{n close(sockfd);n}

测试代码

tcp_server.c:

#include "tcp_socket.h"nnint main(int argc, char **argv)n{n printf("==================tcp server==================n");n int server_fd = tcp_init(NULL, 4321);n if (server_fd < 0)n {n printf("tcp_init error!n");n exit(EXIT_FAILURE);n }nn int client_fd = tcp_accept(server_fd);n if (client_fd < 0)n {n printf("tcp_accept error!n");n exit(EXIT_FAILURE);n }nn while (1)n {n char buf[128] = {0};n n int recv_len = tcp_blocking_recv(client_fd, buf, sizeof(buf));n if (recv_len <= 0)n {n printf("recv error!n");n tcp_close(client_fd);n tcp_close(server_fd);n exit(EXIT_FAILURE);n }n printf("recv : %sn", buf);nn int send_len = tcp_send(client_fd, buf, strlen(buf));n if (send_len <= 0)n {n printf("send error!n");n tcp_close(client_fd);n tcp_close(server_fd);n exit(EXIT_FAILURE); n }n elsen {n printf("send success! send: %s, send_len: %dn", buf, send_len);n }n }n tcp_close(server_fd);nn return 0;n}

tcp_client.c:

#include "tcp_socket.h"nnint main(int argc, char **argv)n{n printf("==================tcp cient==================n");n if (argc < 3)n {n printf("usage:./tcp_client ip portn");n exit(EXIT_FAILURE);n }nn char ip_buf[32] = {0};n int port = 0;nn memcpy(ip_buf, argv[1], strlen(argv[1]));n port = atoi(argv[2]);nn int server_fd = tcp_connect(ip_buf, port);n if (server_fd < 0)n {n printf("tcp_connect error!n");n exit(EXIT_FAILURE);n }nn while (1)n {n char buf[128] = {0};n if (scanf("%s", buf))n {n int send_len = tcp_send(server_fd, buf, strlen(buf));n if (send_len <= 0)n {n printf("tcp_send error!n");n tcp_close(server_fd);n exit(EXIT_FAILURE); n }n elsen {n printf("send success! send: %s, send_len: %dn", buf, send_len);n }nn bzero(buf, sizeof(buf));n int recv_len = tcp_blocking_recv(server_fd, buf, sizeof(buf));n if (recv_len <= 0)n {n printf("tcp_blocking_recv error!n");n tcp_close(server_fd);n exit(EXIT_FAILURE);n }n printf("recv : %sn", buf);n } n }nn return 0;n}

测试结果:

嵌入式中,TCP通信常用接口的使用封装

如果觉得文章不错,麻烦帮忙转发,谢谢!

如果文章对你有帮助,麻烦帮忙点赞、收藏、转发,谢谢!

猜你喜欢:

分享一份不错的嵌入式资料汇总贴


给“嵌入式大杂烩读者”整理了一个嵌入式实用项目汇总仓库,私信回复1024,即可看到链接

返回顶部