简体   繁体   English

编译C程序时出错

[英]I have an error when compiling a C program

I am trying to compile this C program but I am getting this message我正在尝试编译这个 C 程序,但我收到了这条消息

warning: passing 'int *' to parameter of type 'socklen_t *'
(aka 'unsigned int *') converts between pointers to integer types with
different sign [-Wpointer-sign]
if ((new_s = accept(s, (struct sockaddr *) &sin, &addr_len)) < 0) {
                                                 ^~~~~~~~~

someone could help me?有人可以帮助我吗?

#include<string.h>
#include<stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h> // for open
#include <unistd.h> // for close
#define SERVER_PORT 5432
#define MAX_PENDING 5
#define MAX_LINE 256

int main()
{
    struct sockaddr_in sin;
    char buf[MAX_LINE];
    int buf_len, addr_len;
    int s, new_s;
    /* build address data structure */
    bzero((char *)&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = INADDR_ANY;
    sin.sin_port = htons(SERVER_PORT);
    /* setup passive open */
    if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
        perror("simplex-talk: socket");
        exit(1);
    }
    if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) < 0) {
        perror("simplex-talk: bind");
        exit(1);
    }

    listen(s, MAX_PENDING);
    /* wait for connection, then receive and print text */
    while(1) {
        if ((new_s = accept(s, (struct sockaddr *) &sin, &addr_len)) < 0) {
            perror("simplex-talk: accept");
            exit(1);
        }
        while (buf_len == recv(new_s, buf, sizeof(buf), 0))
            fputs(buf, stdout);
        close(new_s);
    }
}

As the error message states, you're passing an int * argument to accept for the last argument when the function expects a socklen_t * for this argument.如错误消息所述,当 function 期望此参数有socklen_t *时,您正在传递一个int *参数以accept最后一个参数。

So change the type of addr_len to socklen_t .因此,将addr_len的类型更改为socklen_t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM