编译出程序,烧写到开发板中,运行后提示bind: Socket operation on non-socket
网上找了下原因,说是==与=优先级的问题,后来我也照着添加上(),但还是不行
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define MYPORT 3490
#define BACKLOG 10
int main()
{
int sockfd,new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero),sizeof(struct sockaddr_in));
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)
{
sin_size = sizeof(struct sockaddr_in);
new_fd = (accept(sockfd,(struct sockaddr*)&their_addr,&sin_size));
if((new_fd) == -1)
{
perror("accept");
continue;
}
printf("Gotconnection from %s\n",inet_ntoa(their_addr.sin_addr));
if(!fork())
{
if(send(new_fd,"Hello world!\n",14,0) == -1)
perror("send");
close(new_fd);
exit(1);
}
close(new_fd);
while(waitpid(-1,NULL,0)>0);
}
}
召唤万能的版主~~
|