yixinxiangchen
yixinxiangchen
2017-06-12 10:06

c语言代码问题,请问一下这段代码属于哪个领域?错误在哪里

  • c++
  • c

#include
#include
#include
#include
#include
#define SERVPORT 3333
#define BACKLOG 10

void main()
{
int sockfd,client_fd;

    struct sockaddr_in my_addr;
    struct sockaddr_in remote_addr;
    char*msg="You are welcome!\n";
    if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
    {
            printf("socket创建出错!");
            exit(1);
    }
    my_addr.sin_family=AF_INET;
    my_addr.sin_port=htons(SERVPORT);
    my_addr.sin_addr.s_addr=INADDR_ANY;
    memset(&(my_addr.sin_zero),0,8);

    if(bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr))==-1)
    {
            printf("bind出错");
            exit(1);
    }

    if(listen(sockfd,BACKLOG)==-1)
    {
            printf("listen出错");
            exit(1);
    }
    while(1)
    {
            sin_size=sizeof(struct sockaddr_in);
            if((client_fd=accept(sockfd,(struct sockaddr*)&remote_addr,&sin_size))==-1)
            {
                    printf("accept出错");
                    continue;
            }

            if(send(client_fd,msg,strlen(msg),0)==-1)
            {
                    printf("send出错");
                    close(client_fd);
                    exit(0);
            }
            close(client_fd);
    }

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答