23,217
社区成员




#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
int main ()
{
int socketfd;
int nRet = -1;
int len;
int nOn;
int nYes;
int nkeepIdle = 5;
int nkeepInterval = 5;
int nkeepCount = 3;
struct sockaddr_in addr;
socketfd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
setsockopt(socketfd, IPPROTO_TCP, TCP_NODELAY, (char*)&nOn, sizeof(int));
setsockopt(socketfd, SOL_SOCKET, SO_KEEPALIVE, (char*)&nYes, sizeof(int));
nRet = setsockopt(socketfd, SOL_TCP, TCP_KEEPIDLE, (void *)&nkeepIdle, sizeof(nkeepIdle));
nRet = setsockopt(socketfd, SOL_TCP, TCP_KEEPINTVL, (void *)&nkeepInterval, sizeof(nkeepInterval));
nRet = setsockopt(socketfd, SOL_TCP, TCP_KEEPCNT, (void *)&nkeepCount, sizeof(nkeepCount));
addr.sin_family = AF_INET;
addr.sin_port = htons(8000);
addr.sin_addr.s_addr = INADDR_ANY;
bind(socketfd, (struct sockaddr*)&addr, sizeof(addr));
listen(socketfd, 2);
nRet = ioctl(socketfd, FIONREAD, &len);
printf("the ret = %d, socket = %d, error = %d\n", nRet, socketfd, errno);
return 0;
}