当前位置: 技术问答>linux和unix
bind error :怎么会有这样的错误呢
来源: 互联网 发布时间:2017-04-04
本文导语: #include #include #include #include #include #include #include #include #define portnumber 3333 int main(int argc,char *argv[]) { int sockfd,new_fd; struct sockaddr_in server_addr; struct sockaddr_in client_ad...
#include
#include
#include
#include
#include
#include
#include
#include
#define portnumber 3333
int main(int argc,char *argv[])
{
int sockfd,new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size;
int nbytes;
char buffer[1024];
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
fprintf(stderr,"Socket error:%sna",strerror(errno));
exit(1);
}
printf("it is successed to biult socketn");
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
// server_addr.sinaddr.s_addr=inet_addr(192.168.80.129);
server_addr.sin_port=htons(portnumber);
if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr)) == -1);
{
fprintf(stderr,"bind error:%sna",strerror(errno));
exit(1);
}
if(listen(sockfd,5) == -1)
{
fprintf(stderr,"listen error:%sna",strerror(errno));
exit(1);
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sockfd,(struct sockaddr*)(&client_addr),&sin_size)) == -1)
{
fprintf(stderr,"accept error:%sna",strerror(errno));
exit(1);
}
fprintf(stderr,"server get connection from %sn",inet_ntoa(client_addr.sin_addr));
if((nbytes = read(new_fd,buffer,1024)) == -1)
{
fprintf(stderr,"read error:%sna",strerror(errno));
exit(1);
}
buffer[nbytes] = "";
printf("server received %sn",buffer);
close(new_fd);
}
}
#include
#include
#include
#include
#include
#include
#include
#define portnumber 3333
int main(int argc,char *argv[])
{
int sockfd,new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size;
int nbytes;
char buffer[1024];
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
fprintf(stderr,"Socket error:%sna",strerror(errno));
exit(1);
}
printf("it is successed to biult socketn");
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
// server_addr.sinaddr.s_addr=inet_addr(192.168.80.129);
server_addr.sin_port=htons(portnumber);
if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr)) == -1);
{
fprintf(stderr,"bind error:%sna",strerror(errno));
exit(1);
}
if(listen(sockfd,5) == -1)
{
fprintf(stderr,"listen error:%sna",strerror(errno));
exit(1);
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sockfd,(struct sockaddr*)(&client_addr),&sin_size)) == -1)
{
fprintf(stderr,"accept error:%sna",strerror(errno));
exit(1);
}
fprintf(stderr,"server get connection from %sn",inet_ntoa(client_addr.sin_addr));
if((nbytes = read(new_fd,buffer,1024)) == -1)
{
fprintf(stderr,"read error:%sna",strerror(errno));
exit(1);
}
buffer[nbytes] = "";
printf("server received %sn",buffer);
close(new_fd);
}
}
|
if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr)) == -1);
删除最后面的分号,分号,号 ..
删除最后面的分号,分号,号 ..