首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

bind error :如何会有这样的异常呢

2013-03-17 
bind error :怎么会有这样的错误呢#include stdlib.h#include stdio.h#include errno.h#include st

bind error :怎么会有这样的错误呢
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#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:%s\n\a",strerror(errno));
                exit(1);
        }

        printf("it is successed to biult socket\n");

        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:%s\n\a",strerror(errno));
                exit(1);
        }

        if(listen(sockfd,5) == -1)
        {
                fprintf(stderr,"listen error:%s\n\a",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:%s\n\a",strerror(errno));
                        exit(1);
                }


              fprintf(stderr,"server get connection from %s\n",inet_ntoa(client_addr.sin_addr));
                if((nbytes = read(new_fd,buffer,1024)) == -1)
                {
                        fprintf(stderr,"read error:%s\n\a",strerror(errno));
                        exit(1);
                }
                buffer[nbytes] = "\0";
                printf("server received %s\n",buffer);
                close(new_fd);

        }
}




bind() bind?error linux? linux网络编程错误
[解决办法]
 if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr)) == -1);

删除最后面的分号,分号,号 ..

热点排行