日期:2014-05-16 浏览次数:20782 次
#include <stdio.h>
#include <strings.h>
#include <linux/limits.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <linux/types.h>
#include <errno.h>
#include <asm/poll.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
typedef struct
{
char info[1024];
char name[20];
int id;
}STU;
#define MAXLINE 1024
#define SERV_PORT 9988
#define CTRLVA 1
ssize_t Readline(int fd, void *buf, size_t num);
ssize_t writen(int fd, const void *vptr, size_t n);
/*设置为非阻塞*/
static int setnonblocking(int sock)
{
int opts;
opts = fcntl(sock, F_GETFL);
if (opts < 0)
{
return -1;
}
opts = opts|O_NONBLOCK;
if (fcntl(sock, F_SETFL, opts) < 0)
{
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
int i,maxi,listenfd,connfd,sockfd;
int nready;
ssize_t n;
char line[MAXLINE];
socklen_t clilen;
struct pollfd client[OPEN_MAX];
struct sockaddr_in cliaddr,seraddr;
STU stu;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&seraddr, sizeof(seraddr));
seraddr.sin_family = AF_INET;
seraddr.sin_addr.s_addr = htonl(INADDR_ANY);
seraddr.sin_port = htons(SERV_PORT);
bind(listenfd, (struct sockaddr*)&seraddr, sizeof(seraddr));
listen(listenfd, 5);
/*if (setnonblocking(listenfd))
{
printf("err\n");
exit(-1);
}*/
client[0].fd = listenfd;
client[0].events = POLLRDNORM;
for (i=1; i<OPEN_MAX; i++)
{
client[i].fd = -1;
}
maxi = 0;
for (; ; )
{
printf("333\n");
nready = poll(client, maxi+1, -1);
&n