tcp_socket.h:
#ifndef TCP_SCOKET_H
#define TCP_SCOKET_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_CONNECT_NUM 10
int tcp_init(const char* ip, int port);
int tcp_accept(int sfd);
int tcp_connect(const char* ip, int port);
int tcp_nonblocking_recv(int conn_sockfd,
void *rx_buf,
int buf_len,
int timeval_sec,
int timeval_usec);
int tcp_blocking_recv(int conn_sockfd, void *rx_buf, uint16_t buf_len);
int tcp_send(int conn_sockfd, uint8_t *tx_buf, uint16_t buf_len);
void tcp_close(int sockfd);
#endif
tcp_socket.c:
#include "tcp_socket.h"
int tcp_init(const char* ip, int port)
{
int optval = 1;
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0)
{
perror("socket");
return -1;
}
/* 解除端口占用 */
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0)
{
perror("setsockoptn");
return -1;
}
struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(struct sockaddr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
if (NULL == ip)
{
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
}
else
{
server_addr.sin_addr.s_addr = inet_addr(ip);
}
if (bind(server_fd, (struct sockaddr*)&server_addr,sizeof(struct sockaddr)) < 0)
{
perror("bind");
close(server_fd);
return -1;
}
if(listen(server_fd, MAX_CONNECT_NUM) < 0)
{
perror("listen");
close(server_fd);
return -1;
}
return server_fd;
}
int tcp_accept(int server_fd)
{
struct sockaddr_in client_addr = {0};
int addrlen = sizeof(struct sockaddr);
int new_fd = accept(server_fd, (struct sockaddr*) &client_addr, &addrlen);
if(new_fd < 0)
{
perror("accept");
close(server_fd);
return -1;
}
return new_fd;
}
int tcp_connect(const char *ip, int port)
{
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0)
{
perror("socket");
return -1;
}
struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(struct sockaddr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr(ip);
if (connect(server_fd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr)) < 0)
{
perror("connect");
close(server_fd);
return -1;
}
return server_fd;
}
int tcp_nonblocking_recv(int conn_sockfd, void *rx_buf, int buf_len, int timeval_sec, int timeval_usec)
{
fd_set readset;
struct timeval timeout = {0, 0};
int maxfd = 0;
int fp0 = 0;
int recv_bytes = 0;
int ret = 0;
timeout.tv_sec = timeval_sec;
timeout.tv_usec = timeval_usec;
FD_ZERO(&readset);
FD_SET(conn_sockfd, &readset);
maxfd = conn_sockfd > fp0 ? (conn_sockfd+1) : (fp0+1);
ret = select(maxfd, &readset, NULL, NULL, &timeout);
if (ret > 0)
{
if (FD_ISSET(conn_sockfd, &readset))
{
if ((recv_bytes = recv(conn_sockfd, rx_buf, buf_len, MSG_DONTWAIT))== -1)
{
perror("recv");
return -1;
}
}
}
else
{
return -1;
}
return recv_bytes;
}
int tcp_blocking_recv(int conn_sockfd, void *rx_buf, uint16_t buf_len)
{
return recv(conn_sockfd, rx_buf, buf_len, 0);
}
int tcp_send(int conn_sockfd, uint8_t *tx_buf, uint16_t buf_len)
{
return send(conn_sockfd, tx_buf, buf_len, 0);
}
void tcp_close(int sockfd)
{
close(sockfd);
}
x下面测试代码:
#include "tcp_socket.h"
int main(int argc, char **argv)
{
printf("==================tcp server==================n");
int server_fd = tcp_init(NULL, 4321);
if (server_fd < 0)
{
printf("tcp_init error!n");
exit(EXIT_FAILURE);
}
int client_fd = tcp_accept(server_fd);
if (client_fd < 0)
{
printf("tcp_accept error!n");
exit(EXIT_FAILURE);
}
while (1)
{
char buf[128] = {0};
int recv_len = tcp_blocking_recv(client_fd, buf, sizeof(buf));
if (recv_len <= 0)
{
printf("recv error!n");
tcp_close(client_fd);
tcp_close(server_fd);
exit(EXIT_FAILURE);
}
printf("recv : %sn", buf);
int send_len = tcp_send(client_fd, buf, strlen(buf));
if (send_len <= 0)
{
printf("send error!n");
tcp_close(client_fd);
tcp_close(server_fd);
exit(EXIT_FAILURE);
}
else
{
printf("send success! send: %s, send_len: %dn", buf, send_len);
}
}
tcp_close(server_fd);
return 0;
}
tcp_client.c:
#include "tcp_socket.h"
int main(int argc, char **argv)
{
printf("==================tcp cient==================n");
if (argc < 3)
{
printf("usage:./tcp_client ip portn");
exit(EXIT_FAILURE);
}
char ip_buf[32] = {0};
int port = 0;
memcpy(ip_buf, argv[1], strlen(argv[1]));
port = atoi(argv[2]);
int server_fd = tcp_connect(ip_buf, port);
if (server_fd < 0)
{
printf("tcp_connect error!n");
exit(EXIT_FAILURE);
}
while (1)
{
char buf[128] = {0};
if (scanf("%s", buf))
{
int send_len = tcp_send(server_fd, buf, strlen(buf));
if (send_len <= 0)
{
printf("tcp_send error!n");
tcp_close(server_fd);
exit(EXIT_FAILURE);
}
else
{
printf("send success! send: %s, send_len: %dn", buf, send_len);
}
bzero(buf, sizeof(buf));
int recv_len = tcp_blocking_recv(server_fd, buf, sizeof(buf));
if (recv_len <= 0)
{
printf("tcp_blocking_recv error!n");
tcp_close(server_fd);
exit(EXIT_FAILURE);
}
printf("recv : %sn", buf);
}
}
return 0;
}
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。
举报投诉
-
TCP
+关注
关注
8文章
1353浏览量
79059 -
C代码
+关注
关注
1文章
89浏览量
14299
发布评论请先 登录
相关推荐
怎么使具体实现代码不可见实现函数的封装?
如何实现函数的封装,即能让别人调用,但是看不到具体的实现代码。举个例子,一个开源项目,要把代码公布出去,但是代码中有些比较敏感的部分不想让别
发表于 03-12 22:18
DSP编程技巧之链接汇编代码与C_C++代码
在DSP的开发中,常用的算法都可以用C/C++代码来高效实现。但是对一些特殊寄存器的读写,例如某些CPU寄存器的读写,因为C/
发表于 10-18 09:47
•7次下载
基于单片机AT89C52和DS1302的温度时间显示方案含源代码程序
电子发烧友网站提供《基于单片机AT89C52和DS1302的温度时间显示方案含源代码程序.zip》资料免费下载
发表于 01-02 11:34
•17次下载
评论