当前位置: 技术问答>linux和unix
帮我看看,这个代码怎么编不过呢,aio问题
来源: 互联网 发布时间:2016-02-23
本文导语: #include #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int sockfd, newfd; struct sockaddr_in server_addr, client_addr; char buffer[64]; struct aiocb myaiocb; ...
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int sockfd, newfd;
struct sockaddr_in server_addr, client_addr;
char buffer[64];
struct aiocb myaiocb;
ssize_t rt, aiocb_list_max_size, aiort;
struct aiocb *aiocb_list[1];
memset(buffer, '', sizeof(buffer));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("Socket error:%sna", strerror(errno));
exit(1);
}
bzero(&server_addr, sizeof(struct sockaddr_in));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(8000);
if(bind(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1)
{
printf("Bind error:%sna", strerror(errno));
exit(1);
}
if(listen(sockfd, 5) == -1)
{
printf("Listen error:%sna", strerror(errno));
exit(1);
}
bzero(&myaiocb, sizeof(struct aiocb));
myaiocb.aio_fildes = newfd + 1;
myaiocb.aio_offset = 0;
myaiocb.aio_buf = (void*)buffer;
myaiocb.aio_nbytes = sizeof(buffer);
rt = aio_read(&myaiocb);
if(rt == 0)
{
aiocb_list[0] = &myaiocb;
aiocb_list_max_size = 1;
while(1)
{
rt = aio_suspend(aiocb_list, aiocb_list_max_size, NULL);
if(rt == -1)
{
printf("suspend error!");
}
else
{
aiort = aio_return(aiocb_list[0]);
if(aiort == 0)
{
close(newfd);
break;
}
else
{
if(aiocb_list[0] != NULL)
{
printf("size: %dn", aiort);
printf("buffer: %sn", (char*)(aiocb_list[0]->aio_buf));
bzero(buffer, sizeof(buffer));
rt = aio_read(&myaiocb);
if(rt == 0)
continue;
}
break;
}
}
}
}
return 1;
}
[root@localhost sock_test]# gcc -o aio_read_test.out -g aio_read_test.c
aio_read_test.c: In function `main':
aio_read_test.c:59: warning: passing arg 1 of `aio_suspend' from incompatible pointer type
/tmp/cco8HlTC.o(.text+0x197): In function `main':
/home/zhoucl/sock_test/aio_read_test.c:50: undefined reference to `aio_read'
/tmp/cco8HlTC.o(.text+0x1da):/home/zhoucl/sock_test/aio_read_test.c:59: undefined reference to `aio_suspend'
/tmp/cco8HlTC.o(.text+0x20c):/home/zhoucl/sock_test/aio_read_test.c:66: undefined reference to `aio_return'
/tmp/cco8HlTC.o(.text+0x286):/home/zhoucl/sock_test/aio_read_test.c:79: undefined reference to `aio_read'
collect2: ld returned 1 exit status
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int sockfd, newfd;
struct sockaddr_in server_addr, client_addr;
char buffer[64];
struct aiocb myaiocb;
ssize_t rt, aiocb_list_max_size, aiort;
struct aiocb *aiocb_list[1];
memset(buffer, '', sizeof(buffer));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("Socket error:%sna", strerror(errno));
exit(1);
}
bzero(&server_addr, sizeof(struct sockaddr_in));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(8000);
if(bind(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1)
{
printf("Bind error:%sna", strerror(errno));
exit(1);
}
if(listen(sockfd, 5) == -1)
{
printf("Listen error:%sna", strerror(errno));
exit(1);
}
bzero(&myaiocb, sizeof(struct aiocb));
myaiocb.aio_fildes = newfd + 1;
myaiocb.aio_offset = 0;
myaiocb.aio_buf = (void*)buffer;
myaiocb.aio_nbytes = sizeof(buffer);
rt = aio_read(&myaiocb);
if(rt == 0)
{
aiocb_list[0] = &myaiocb;
aiocb_list_max_size = 1;
while(1)
{
rt = aio_suspend(aiocb_list, aiocb_list_max_size, NULL);
if(rt == -1)
{
printf("suspend error!");
}
else
{
aiort = aio_return(aiocb_list[0]);
if(aiort == 0)
{
close(newfd);
break;
}
else
{
if(aiocb_list[0] != NULL)
{
printf("size: %dn", aiort);
printf("buffer: %sn", (char*)(aiocb_list[0]->aio_buf));
bzero(buffer, sizeof(buffer));
rt = aio_read(&myaiocb);
if(rt == 0)
continue;
}
break;
}
}
}
}
return 1;
}
[root@localhost sock_test]# gcc -o aio_read_test.out -g aio_read_test.c
aio_read_test.c: In function `main':
aio_read_test.c:59: warning: passing arg 1 of `aio_suspend' from incompatible pointer type
/tmp/cco8HlTC.o(.text+0x197): In function `main':
/home/zhoucl/sock_test/aio_read_test.c:50: undefined reference to `aio_read'
/tmp/cco8HlTC.o(.text+0x1da):/home/zhoucl/sock_test/aio_read_test.c:59: undefined reference to `aio_suspend'
/tmp/cco8HlTC.o(.text+0x20c):/home/zhoucl/sock_test/aio_read_test.c:66: undefined reference to `aio_return'
/tmp/cco8HlTC.o(.text+0x286):/home/zhoucl/sock_test/aio_read_test.c:79: undefined reference to `aio_read'
collect2: ld returned 1 exit status
|
加 -lrt
|
同上,需要加上rt库,这个库包含了一些realtime的函数。