- 论坛徽章:
- 0
|
下面代码中,如果结构体中是数组则在运行过程中ACCEPT会失败,错误输出为accept error[-1]:Invalid argument
如果结构体为其他则运行一切正常.为什么,有人能解释下吗 谢谢- struct send_info{
- char len[11]; //换成int len; 则一切正常,accept正常运行
- };
- int main(int argc,char ** argv){
- int i,len;
- int socketfd,childfd;
- char buff[100];
- struct send_info s_temp;
- struct sockaddr_in servaddr,childaddr;
- if((socketfd=socket(AF_INET,SOCK_STREAM,0))<1){
- printf("socket error\n");
- exit(0);
- }
- bzero(&servaddr,sizeof(servaddr));
- servaddr.sin_family=AF_INET;
- servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
- servaddr.sin_port=htons(5543);
- if(bind(socketfd,(struct sockaddr *)&servaddr,sizeof(servaddr))<0){
- printf("bind error\n");
- exit(0);
- }
- listen(socketfd,2);
- while(1){
- bzero(&childaddr,sizeof(childaddr));
- if((childfd=accept(socketfd,(struct sockaddr*) &childaddr,&len))<0){
- printf("accept error[%d]:%s\n",childfd,strerror(errno));
- exit (0);
- }else{
- printf("accept ok\n");
- }
- printf("get a new connect form %s:%d\n",inet_ntop(AF_INET,&childaddr.sin_addr,buff,sizeof(buff)),ntohs(childaddr.sin_port));
-
- close(childfd);
- }
- }
复制代码 |
|