AIX poll TCP

Source: Internet
Author: User
Tags htons

AIX supports poll, tests it, and matches expectations.

Server

#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#include <poll.h>

/*
Pollin Date can be read
Pollrdnorm Date can be read

Pollrdband Priority date can be read

Pollpri Urgent date can be read

Pollout Date can be write

Pollwrnorm Date can be write

Pollwrband Priority date can be write

Pollmsg used by Sigpoll

Poller Error

Pollhup Descriptor Hup Up

Pollnval Descriptor Illegal

*/
int main (int argc, char *argv[])
{
int LISTENFD, CONNFD;
struct sockaddr_in serveraddr;
Char buff[20];

if ((LISTENFD = socket (af_inet, sock_stream, 0)) = = 1)
{
printf ("Create Error:%s (errno=%d) \ n", Strerror (errno), errno);
Exit (0);
}
memset (&serveraddr, 0, sizeof (SERVERADDR));
serveraddr.sin_family = af_inet;
SERVERADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any);
Serveraddr.sin_port = htons (6789);

    if (Bind (LISTENFD, (struct sockaddr*) &serveraddr, sizeof (serveraddr)) = = 1)
     {
 printf ("Bind error:%s (errno=%d) \ n", Strerror (errno), errno);
 exit (0);
    }
    if (Listen (LISTENFD, 5) = = 1)
    {
 printf ("Listen Error:%s (errno=%d) \ n ", Strerror (errno), errno);
 exit (0);
   }
    if (fcntl (LISTENFD, F_SETFL, o_nonblock) = = 1)
    {
 printf ("Fcntl Error:%s (errno=%d) \ n ", Strerror (errno), errno);
 exit (0);
   }

int timeout;
int ret, i = 0, k = 0;

struct POLLFD fds[6];
for (k = 0; k < 6; k++)
{
FDS[K].FD =-1;
}
FDS[I++].FD = LISTENFD;
Fds[i-1].events = Pollin;

while (1)
{
printf ("i =%d\n", i);
RET = Poll (FDS, I,/*&timeout*/-1); 0 return immediately
if (ret = 1)
{
printf ("Poll error:%s (errno=%d) \ n", Strerror (errno), errno);
Exit (0);
}
else if (ret = 0)
{
printf ("Poll Timeout Error:%s (errno=%d) \ n", Strerror (errno), errno);
Exit (0);
}
Else
{
if (Fds[0].revents & Pollin)
{

int CONNFD = Accept (LISTENFD, (struct sockaddr*) null, NULL);
if (CONNFD = = 1)
{
printf ("Accept Error:%s (errno=%d) \ n", Strerror (errno), errno);
}
Else
{
if (i > 5)
{
printf ("Max Connect num, close it\n");
Close (CONNFD);
Continue
Exit (0);
}
printf ("Accept new socket\n");
if (Fcntl (CONNFD, F_SETFL, o_nonblock) = = 1)
{
printf ("fcntl error:connfd =%d,%s (errno=%d) \ n", CONNFD, Strerror (errno), errno);
Exit (0);
}
for (k = 1; k < 6; k++)
{
if (fds[k].fd = = 1)
{
printf ("k =%d nn\n", K);
FDS[K].FD = CONNFD;
Fds[k].events = Pollin;
i++;
Break
}
}
}
}
Else
{
printf ("read ... \ n");
for (k = 1; k < 6; k++)
{
if (fds[k].fd = = 1)
Continue
printf ("k =%d\n", k);
if (Fds[k].revents & (Pollin | Pollrdnorm | Pollrdband | Pollpri | Pollerr))
{
int len = recv (FDS[K].FD, Buff, 20, 0);
if (len = = 1)
{
printf ("recv error:connfd =%d,%s (errno=%d) \ n", Connfd,strerror (errno), errno);
Exit (0);
}
else if (len = = 0)
{
printf ("Socket close\n");
Close (FDS[K].FD);
FDS[K].FD =-1;
}
Else
{
Buff[len] = ' the ';
printf ("recv data =%s, Len =%d\n", buff, Len);
memset (Buff, 0, 20);
}
}
}
}
printf ("One More time.\n");
}
}

for (i = 0; i < 7; i++)
{
if (fds[i].fd!=-1)
Close (FDS[I].FD);
}

return 0;
}

Client

#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdio.h>

int main (int argc, char *argv[])
{
int sockfd;
Char buff[20];
struct sockaddr_in serveraddr;

if (argc!= 2)
{
printf ("Usage:./client addr\n");
Exit (0);
}

if ((SOCKFD = socket (af_inet, sock_stream, 0)) = = 1)
{
printf ("Create Error:%s (errno=%d) \ n", Strerror (errno), errno);
Exit (0);
}
memset (&serveraddr, 0, sizeof (SERVERADDR));
serveraddr.sin_family = af_inet;
Serveraddr.sin_port = htons (6789);
if (Inet_pton (Af_inet, argv[1], &serveraddr.sin_addr) = = 1)
{
printf ("Inet_pton error for%s\n", argv[1]);
Exit (0);
}
if (Connect (SOCKFD, (struct sockaddr*) &serveraddr, sizeof (serveraddr)) = = 1)
{
printf ("Connect Error:%s (errno=%d) \ n", Strerror (errno), errno);
Exit (0);
}
printf ("Send data \ n");
Fgets (buff, stdin);
if (send (SOCKFD, buff, strlen (buff), 0) = = 1)
{
printf ("Send error:%s (errno=%d) \ n", Strerror (errno), errno);
Exit (0);
}
Close (SOCKFD);

return 0;
}

This is not too much to say, run, no problem.

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.