?? fcntl.c
字號:
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <fcntl.h>
#define PORT 3333
#define BACKLOG 10
#define MAX_CONNECTED_NO 10
#define MAXDATASIZE 100
main()
{
struct sockaddr_in addr,client_addr;
int sin_size,recvbytes,flags;
int sockfd,client_fd;
char buf[MAXDATASIZE];
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket");
exit(1);
}
printf("socket success!,sockfd=%d\n",sockfd);
addr.sin_family=AF_INET;
addr.sin_port=htons(PORT);
addr.sin_addr.s_addr=INADDR_ANY;
bzero(&(addr.sin_zero),8);
if(bind(sockfd,(struct sockaddr *)&addr,sizeof(struct sockaddr))==-1)
{
perror("bind");
exit(1);
}
printf("bind success!\n");
if(listen(sockfd,BACKLOG)==-1)
{
perror("listen");
exit(1);
}
printf("listening.......\n");
/*調用vfcntl函數設置非阻塞參數*/
if((flags=fcntl(sockfd,F_SETFL,0))<0)
perror("fcntl F_SETFL");
flags |=O_NONBLOCK;
if(fcntl(sockfd,F_SETFL,flags)<0)
perror("fcntl");
while(1){
sin_size=sizeof(struct sockaddr_in);
if((client_fd=accept(sockfd,(struct sockaddr *)&client_addr,&sin_size))==-1)
{
perror("accept");
exit(1);
}
if((recvbytes=recv(client_fd,buf,MAXDATASIZE,0))==-1)
{
perror("recv");
exit(1);
}
if((read(client_fd,buf,MAXDATASIZE))==-1)
{
perror("read");
exit(1);
}
printf("received a connection:%s\n",buf);
close(client_fd);
exit(1);
}/*while*/
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -