服务端socket初始化部分
if (srvsock > 0)
{
shutdown(srvsock, SHUT_RDWR);
close(srvsock);
}
srvsock = socket(AF_INET, SOCK_STREAM, 0);
if (srvsock < 0)
return (BOOLEAN)1; /*log it */
i = fcntl(srvsock, F_SETFL, O_NONBLOCK);
if (i < 0)
{
shutdown(srvsock, SHUT_RDWR);
close(srvsock);
return (BOOLEAN)1; /*log it */
}
k = 1;
v = 1;
i = setsockopt(srvsock, SOL_SOCKET, SO_REUSEADDR, &k, sizeof(int));
if (i < 0)
{
shutdown(srvsock, SHUT_RDWR);
close(srvsock);
return (BOOLEAN)1; /*log it */
}
local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY;
local.sin_port = htons(MTRACE_TCP_PORT);
i = bind(srvsock, (struct sockaddr *)&local, sizeof(local));
if (i < 0)
{
shutdown(srvsock, SHUT_RDWR);
close(srvsock);
return (BOOLEAN)1; /*log it */
}
i = listen(srvsock, MAX_CLIENT_NUM);
if (i < 0)
{
shutdown(srvsock, SHUT_RDWR);
close(srvsock);
return (BOOLEAN)1; /*log it */
}
接收部分
while ((j = recv(sockid, msgbuf, sizeof(msgbuf), 0/*MSG_NOSIGNAL*/)) > 0)
{
/ *process... */
}
if (j == 0) /*peer closed */
{
/* close */
}
else if (j < 0)
{
if (errno != EAGAIN)
{
/* close */
}
}
了socket section 7,上面说
SO_RCVLOWAT and SO_SNDLOWAT
Specify the minimum number of bytes in the buffer until the socket layer will pass the data to the protocol (SO_SNDLOWAT) or the user on receiving (SO_RCVLOWAT). These two values are not changeable in Linux and their argument size is always fixed to 1 byte. getsockopt is able to read them; setsockopt will always return ENOPROTOOPT.