aio_write在我的简单客户端/服务器程序中不起作用

时间:2016-04-11 09:29:21

标签: c aio

我开始使用aio编写一个简单的客户端服务器程序。我的客户端程序:

int main(int argc, char const *argv[])
{

    int port = 6000;  
    struct sockaddr_in my_address;  
    memset(&my_address, 0, sizeof(my_address));  
    my_address.sin_family = AF_INET;  
    my_address.sin_addr.s_addr = htonl(0x7f000001); // 127.0.0.1
    my_address.sin_port = htons(port);

    int fd = socket(AF_INET, SOCK_STREAM, 0);

    connect(fd, (struct sockaddr*)&my_address, sizeof(my_address));

    char * str = "hello";

    struct aiocb my_aio;
    memset((void*)&my_aio, 0, sizeof(struct aiocb));
    my_aio.aio_fildes = fd;
    my_aio.aio_buf = str;
    my_aio.aio_nbytes = strlen(str);
    aio_write(&my_aio);
    return 0;
}

我的服务器程序:

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno, clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR on binding");

     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
     sleep(5);
     bzero(buffer,256);
     n = read(newsockfd,buffer,255);
     if (n < 0) error("ERROR reading from socket");
     printf("Here is the message: %s\n",buffer);

     return 0; 
}

我希望我的服务器端打印出你好,但事实并非如此。提前谢谢。

0 个答案:

没有答案