aix 5.3 下 aio tcp 通信

来源:互联网 发布:curl算法 编辑:程序博客网 时间:2024/05/01 00:38

最近在看这个,就做了一个aio  case试一下,可惜的是编译没有问题,运行时需要权限,打开权限的方式网上搜到的, 这个权限得是root才可以,公司是不可能给我这个权限的。

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

 

就把代码贴一下:

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;
    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) == '\0';
     printf("recv data = %s, len = %d\n", req->aio_buf, ret);
 }
    }
}

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((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, 20, 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;
}

 

如果有人run 过,告诉我一下结果,谢谢了。

这个主要是用的signal的方式,当然也可以用callback,都差不多。

原创粉丝点击