当前位置: 技术问答>linux和unix
socket 编程中的 Accept函数问题
来源: 互联网 发布时间:2016-01-11
本文导语: #include #include #include #include #include #include #include #include #include #include #define MYPORT 3490 /* the port users will be connecting to */ ...
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 3490 /* the port users will be connecting to */
#define BACKLOG 10 /* how many pending connections queue will hold */
main()
{
int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd */
struct sockaddr_in my_addr; /* my address information */
struct sockaddr_in their_addr; /* connector's address information */
int sin_size;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; /* host byte order */
my_addr.sin_port = htons(MYPORT); /* short, network byte order */
my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
while(1) { /* main accept() loop */
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
perror("accept");
continue;
}
printf("server: got connection from %sn",
inet_ntoa(their_addr.sin_addr));
if (!fork()) { /* this is the child process */
if (send(new_fd, "Hello, world!n", 14, 0) == -1)
perror("send");
close(new_fd);
exit(0);
}
close(new_fd); /* parent doesn't need this */
while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up child processes */
}
}
[cry@localhost C++自己练习]$ g++ -o server server.cpp
server.cpp:61:6: 警告:文件未以空白行结束
server.cpp: In function ‘int main()’:
server.cpp:45: 错误:从类型 ‘int*’ 到类型 ‘socklen_t*’ 的转换无效
server.cpp:45: 错误: 初始化实参 3,属于 ‘int accept(int, sockaddr*, socklen_t*)’
[cry@localhost C++自己练习]$
请高手指点为什么会报这个错。怎样改正。谢谢!
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 3490 /* the port users will be connecting to */
#define BACKLOG 10 /* how many pending connections queue will hold */
main()
{
int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd */
struct sockaddr_in my_addr; /* my address information */
struct sockaddr_in their_addr; /* connector's address information */
int sin_size;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; /* host byte order */
my_addr.sin_port = htons(MYPORT); /* short, network byte order */
my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
while(1) { /* main accept() loop */
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
perror("accept");
continue;
}
printf("server: got connection from %sn",
inet_ntoa(their_addr.sin_addr));
if (!fork()) { /* this is the child process */
if (send(new_fd, "Hello, world!n", 14, 0) == -1)
perror("send");
close(new_fd);
exit(0);
}
close(new_fd); /* parent doesn't need this */
while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up child processes */
}
}
[cry@localhost C++自己练习]$ g++ -o server server.cpp
server.cpp:61:6: 警告:文件未以空白行结束
server.cpp: In function ‘int main()’:
server.cpp:45: 错误:从类型 ‘int*’ 到类型 ‘socklen_t*’ 的转换无效
server.cpp:45: 错误: 初始化实参 3,属于 ‘int accept(int, sockaddr*, socklen_t*)’
[cry@localhost C++自己练习]$
请高手指点为什么会报这个错。怎样改正。谢谢!
|
呵呵,俺也遇到过,把int sin_size;改为socklen_t sin_size就可以了。
原因在编译器的输出里已经说的很明白了,accept的最后一个参数是需要socklen_t*的类型
希望能够对你有所帮助
原因在编译器的输出里已经说的很明白了,accept的最后一个参数是需要socklen_t*的类型
希望能够对你有所帮助