The Linux C poll function uses

Source: Internet
Author: User
Tags htons

When learning about Linux concurrent non-blocking servers. See how you can use the poll function. Preliminary understanding and writing procedures were tested.

Server:

#include <unistd.h>#include<sys/types.h>/*Basic system data Types*/#include<sys/socket.h>/*Basic Socket Definitions*/#include<netinet/inch.h>/*sockaddr_in{} and other Internet Defns*/#include<arpa/inet.h>/*inet (3) Functions*/#include<poll.h>#include<stdlib.h>#include<errno.h>#include<stdio.h>#include<string.h>#defineMax_link 10intMain () {structsockaddr_in server_addr,client_addr; Bzero (&AMP;SERVER_ADDR,sizeof(SERVER_ADDR)); Server_addr.sin_family=af_inet; Server_addr.sin_addr.s_addr=htonl (Inaddr_any); Server_addr.sin_port= Htons (6888); socklen_t Socklen=sizeof(structsockaddr_in); intser_fd,con_fd; SER_FD=socket (Af_inet, Sock_stream,0); if(Bind (SER_FD, (structsockaddr*) &server_addr,sizeof(structsockaddr_in)) = =-1) {perror ("bind error"); Exit (-1); }    if(Listen (SER_FD,Ten) <0) {perror ("Listen error"); return-1; }        structPOLLFD Fds[max_link]; fds[0].fd=ser_fd; fds[0].events=Pollrdnorm; intI=1;  for(; i<max_link;i++) {FDS[I].FD=-1; Fds[i].events=Pollrdnorm; }        intReady_n; intLink_count=1; Charbuffer[1024x768];  while(1) {Ready_n=poll (fds,link_count,-1); if(ready_n==-1) {printf ("poll error \ n"); return 0; }        if(fds[0].revents &pollrdnorm) {            intCli=accept (SER_FD, (structsockaddr*) &client_addr, &Socklen);  for(i=1; i<max_link;i++){                if(fds[i].fd<0) {FDS[I].FD=CLI; printf ("new connect:%d \ t", i); printf ("%d\n", Link_count);  Break; }} Link_count++; Ready_n--; }         for(i=1; I<max_link && ready_n>0; i++){            if(Fds[i].fd >0){                if(Fds[i].revents &pollrdnorm) {                    intN=RECV (Fds[i].fd,buffer,1024x768,0); if(n==0) {FDS[I].FD=-1; Link_count--; }Else if(n>0) {Send (Fds[i].fd,buffer,n,0); } ready_n--; }            }                }                    }        return 0;}

Client:

#include <unistd.h>#include<sys/types.h>/*Basic system data Types*/#include<sys/socket.h>/*Basic Socket Definitions*/#include<netinet/inch.h>/*sockaddr_in{} and other Internet Defns*/#include<arpa/inet.h>/*inet (3) Functions*/#include<sys/Select.h>/*Select function*/#include<stdlib.h>#include<errno.h>#include<stdio.h>#include<string.h>#defineMAXLINE 10240#defineMax (a) (a) > (b)? (a): (b))//typedef struct SOCKADDR SA;voidHandleintsockfd);intMainintargcChar**argv) {    Char* Servinetaddr ="127.0.0.1"; intServport =6888; CharBuf[maxline]; intCONNFD; structsockaddr_in servaddr; if(ARGC = =2) {servinetaddr= argv[1]; }    if(ARGC = =3) {servinetaddr= argv[1]; Servport= Atoi (argv[2]); }    if(Argc >3) {printf ("usage:selectechoclient <IPaddress> <port>\n"); return-1; } CONNFD= Socket (Af_inet, Sock_stream,0); Bzero (&AMP;SERVADDR,sizeof(SERVADDR)); Servaddr.sin_family=af_inet; Servaddr.sin_port=htons (Servport); Inet_pton (Af_inet, SERVINETADDR,&servaddr.sin_addr); if(Connect (CONNFD,structSOCKADDR *) &servaddr,sizeof(SERVADDR)) <0) {perror ("Connect Error"); return-1; } printf ("Welcome to Selectechoclient\n");     Handle (CONNFD); /*Do it all*/Close (CONNFD); printf ("exit\n"); Exit (0);}voidHandleintCONNFD) {FILE* fp =stdin; CharSendline[maxline], recvline[maxline];    Fd_set RSet; Fd_zero (&RSet); intMaxfds = Max (Fileno (FP), CONNFD) +1; intnread;  for (;;) {Fd_set (Fileno (FP),&RSet); Fd_set (CONNFD,&RSet); if(Select(Maxfds, &rset, NULL, NULL, NULL) = =-1) {perror ("Select Error"); Continue; }        if(Fd_isset (CONNFD, &RSet)) {            //Receiving server ResponseNread =Read (CONNFD, recvline, MAXLINE); if(Nread = =0) {printf ("server close the connection\n");  Break; }             Else if(Nread = =-1) {perror ("Read Error");  Break; }            Else {                //Server ResponseWrite (Stdout_fileno, recvline, nread); }          }        if(Fd_isset (Fileno (FP), &RSet)) {            //Standard input readable            if(Fgets (Sendline, MAXLINE, fp) = =NULL) {                //EOF Exit                 Break; }             Else{Write (CONNFD, Sendline, strlen (Sendline)); }        }    } }

The Linux C poll function uses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.