lepustse / tcpclient Goto Github PK
View Code? Open in Web Editor NEW基于多线程的非阻塞 socket 编程
基于多线程的非阻塞 socket 编程
当 res = read(thiz->pipe_read_fd, pipe_buff, BUFF_SIZE);
的返回值为1024,且pipe buff和sock buff相连,pipe_buff[res] = '\0'
;会修改sock buff的magic header,导致rt_free时产生异常
static void select_handle(rt_tcpclient_t *thiz, char *pipe_buff, char *sock_buff)
{
fd_set fds;
rt_int32_t max_fd = 0, res = 0;
max_fd = MAX_VAL(thiz->sock_fd, thiz->pipe_read_fd) + 1;
FD_ZERO(&fds);
while (1)
{
FD_SET(thiz->sock_fd, &fds);
FD_SET(thiz->pipe_read_fd, &fds);
res = select(max_fd, &fds, RT_NULL, RT_NULL, RT_NULL);
/* exception handling: exit */
EXCEPTION_HANDLE(res, "select handle", "error", "timeout");
/* socket is ready */
if (FD_ISSET(thiz->sock_fd, &fds))
{
res = recv(thiz->sock_fd, sock_buff, BUFF_SIZE, 0);
/* exception handling: exit */
EXCEPTION_HANDLE(res, "socket recv handle", "error", "TCP disconnected");
/* have received data, clear the end */
sock_buff[res] = '\0';
RX_CB_HANDLE(sock_buff, res);
EXIT_HANDLE(sock_buff);
}
/* pipe is read */
if (FD_ISSET(thiz->pipe_read_fd, &fds))
{
/* read pipe */
res = read(thiz->pipe_read_fd, pipe_buff, BUFF_SIZE);
/* exception handling: exit */
EXCEPTION_HANDLE(res, "pipe recv handle", "error", "");
/* have received data, clear the end */
pipe_buff[res] = '\0';
/* write socket */
send(thiz->sock_fd, pipe_buff, res, 0);
/* exception handling: warning */
EXCEPTION_HANDLE(res, "socket write handle", "error", "warning");
EXIT_HANDLE(pipe_buff);
}
}
exit:
rt_free(pipe_buff);
rt_free(sock_buff);
}
应用线程主动closesocket 时,select返回0,但select设置超时后也返回0,这两者怎么区分?
(ps:closesocket 过N久后,select也会返回1,表示一个可读事件,读取recv返回0,表示断开)
应用线程主动closesocket 时,select返回1,代表有一个可读事件,读取recv返回0,表示断开。
多个线程同时往pipe中写入数据,而发送数据读取pipe中的数据进行发送,那是否存在多包数据粘包在一起发送的情况呢?
在tcpclient.c中,342行(pipe_buff[res] = '\0';)这句话,当res=BUFF_SIZE时,写操作会超出内存边界,导致(free(pipe_buff);)失败,报错内容(to free a bad data block: mem: 0x2001708c, used flag: 1, magic code: 0x1e00),需要屏蔽342行,第325行(sock_buff[res] = '\0';)也可能导致同样的问题。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.