AIX 5.3 Aio TCP Communication

Source: Internet
Author: User
Tags htons

Recently looked at this, did a AIO case to try, unfortunately, the compiler has no problem, the runtime needs permissions, open the way of access to the Internet, this permission is root can be, the company is not possible to give me this permission.

1. Determine the state of the Aio0 driver. Run the following AIX command:
Lsdev-c-L Aio0

Example Output:

Aio0 Defined asynchronous I/O

2. Run the cfgmgr AIX command:
Cfgmgr-l Aio0

3. Chdev-l aio0-p-a autoconfig= ' available '

Example output:aio0 changed

4. Run The following command to check the state of the Aio0 driver:
Lsdev-c-L Aio0

Example output:
Aio0 Available asynchronous I/O

Just post the code:

Server

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

void Sig_handler (int signo, siginfo_t *info, void *context)
{
    int ret;
  &nbs P struct AIOCB *req;
    if (Info->si_signo = Sigio)
    {
 req = (struct aiocb*) info-> Si_value.sival_ptr;
 if (aio_error (req) = = 0)
 {
     ret = Aio_return (req);
      (char*) (req->aio_buf+ret) = = ' ";
     printf ("recv data =%s, Len =%d\n", req->aio_buf, ret);
 }
   }
}

int main (int argc, char *argv[])
{
    int listenfd, CONNFD;
    struct sock Addr_in serveraddr;
    Char buff[20];
   
    if ((LISTENFD = socket (af_inet, sock_stream, 0) = = 1)
&NBSP;&NBSP;&N Bsp {
 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 ((CONNFD = Accept (LISTENFD, (struct sockaddr*) null, NULL) = =-1)
{
printf ("Accept Error:%s (errno=%d) \ n", Strerror (errno), errno);
}


struct AIOCB MY_AIOCB;
struct Sigaction sig_act;
My_aiocb.aio_fildes = CONNFD;
My_aiocb.aio_nbytes = 20;
My_aiocb.aio_offset = 0;

My_aiocb.aio_sigevent.sigev_notify = sigev_signal;
My_aiocb.aio_sigevent.sigev_signo = Sigio;
My_aiocb.aio_sigevent.sigev_value.sival_ptr = &my_aiocb;

Sigemptyset (&sig_act.sa_mask);
Sig_act.sa_flags = Sa_siginfo;
Sig_act.sa_sigaction = Sig_handler;

const struct AIOCB * Cblist[1] = {&MY_AIOCB, NULL, NULL, NULL, NULL};
Sigaction (Sigio, &sig_act, NULL);

int ret = Aio_read (&MY_AIOCB);
while (Aio_suspend (cblist, 1, NULL) = = 0)
{
ret = Aio_read (&MY_AIOCB);
}

Close (CONNFD);
Close (LISTENFD);
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;
}

If someone has run over, tell me the result, thank you.

This is mainly used in the signal way, of course, can also use callback, are similar.

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.