简体   繁体   English

在Linux上无法在Mac上运行的随机函数

[英]Random function working on Linux not on Mac

I'm currently learning network programming in university and programmed a server. 我目前正在大学学习网络编程,并为服务器编程。 As we need this one for further progress I need to reuse the code every time. 因为我们需要这一代码来取得进一步的进展,所以我每次都需要重用代码。 There is a random function with an error I don't really get because on the linux pcs at university everything compiles well only my mac which I do all my homework shows this error. 有一个随机函数,但我并没有真正得到错误,因为在大学的linux电脑上,所有东西都只能编译好,我做所有作业的mac都显示此错误。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <time.h>
//#define PORT "3490"  // the port users will be connecting to
#define BACKLOG 10   // how many pending connections queue will hold


int random(int min, int max){
      srand ( time(NULL) ); 
      return min + rand() / (RAND_MAX / (max - min + 1) + 1);
    }


void sigchld_handler(int s)
{
    // waitpid() might overwrite errno, so we save and restore it:
    int saved_errno = errno;
    while(waitpid(-1, NULL, WNOHANG) > 0);
    errno = saved_errno;
}
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
    if (sa->sa_family == AF_INET) {
        return &(((struct sockaddr_in*)sa)->sin_addr);
    }
    return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
int main(int argc, char *argv[])
{
    int sockfd, new_fd;  // listen on sock_fd, new connection on new_fd
    struct addrinfo hints, *servinfo, *p;
    struct sockaddr_storage their_addr; // connector's address information
    socklen_t sin_size;
    struct sigaction sa;
    int yes=1;
    char s[INET6_ADDRSTRLEN];
    int rv;
    //int linetoread;

    if (argc != 3) {
        fprintf(stderr,"usage: server port txtfile\n");
        exit(1);
    }

    FILE* fp;
    char buffer[255];
    int i = 0;
    char rnd[200][255];
    int fixed;

    fp = fopen(argv[2], "r");

    while(fgets(buffer, 255, (FILE*) fp)) {
        //printf("%s\n", buffer);
        for (int j = 0; j < 255; j++){
          rnd[i][j] = buffer[j];
        }
        i++;
    }

    fclose(fp);
    //printf("number of lines: %d\n", i);
    //linetoread = rand() % i;
    //printf("%d\n", random(0,i));

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE; // use my IP
    if ((rv = getaddrinfo(NULL, argv[1], &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }
    // loop through all the results and bind to the first we can
    for(p = servinfo; p != NULL; p = p->ai_next) {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("server: socket");
            continue;
        }
        if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
                sizeof(int)) == -1) {
            perror("setsockopt");
            exit(1);
        }
        if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
            close(sockfd);
            perror("server: bind");
            continue;
        }
        break;
    }
    freeaddrinfo(servinfo); // all done with this structure
    if (p == NULL)  {
        fprintf(stderr, "server: failed to bind\n");
        exit(1);
    }
    if (listen(sockfd, BACKLOG) == -1) {
        perror("listen");
        exit(1);
    }
    sa.sa_handler = sigchld_handler; // reap all dead processes
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    if (sigaction(SIGCHLD, &sa, NULL) == -1) {
        perror("sigaction");
        exit(1);
    }
    printf("server: waiting for connections...\n");
    while(1) {  // main accept() loop
        sin_size = sizeof their_addr;
        new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
        if (new_fd == -1) {
            perror("accept");
            continue;
        }
        inet_ntop(their_addr.ss_family,
            get_in_addr((struct sockaddr *)&their_addr),
            s, sizeof s);
        printf("server: got connection from %s\n", s);
        if (!fork()) { // this is the child process
            close(sockfd); // child doesn't need the listener
            fixed = random(0,i-1);
            //printf("line: %d\n", fixed);
            if (send(new_fd, rnd[fixed], strlen(rnd[fixed]), 0) == -1)
                perror("send");
            close(new_fd);
            exit(0);
        }
        close(new_fd);  // parent doesn't need this
    }
    return 0;
}       

Error on my mac: 我的Mac上出现错误:

server.c:16:5: error: conflicting types for 'random'
int random(int min, int max){
    ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/stdlib.h:233:7: note: 
      previous declaration is here
long     random(void) __swift_unavailable("Use arc4random instead.");
         ^
server.c:132:19: error: too many arguments to function call, expected 0, have 2
                        fixed = random(0,i-1);
                                ~~~~~~ ^~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/stdlib.h:233:1: note: 
      'random' declared here
long     random(void) __swift_unavailable("Use arc4random instead.");
^
2 errors generated.

random() is a known function name. random()是已知的函数名称。 change it to something else and it will work. 将其更改为其他内容,它将起作用。

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

相关问题 C:故障?:排序功能在LINUX(RHEL 6.6)上不起作用,但在MAC OS X(10.3)上起作用 - C: Glitch?: Sort function NOT working on LINUX (RHEL 6.6), but MAC OS X (10.3) 与Mac和Linux上的exp函数略有不同的结果 - Slightly different result from exp function on Mac and Linux 在Linux上运行时C fgets函数不起作用 - C fgets function not working when ran on linux fgets()linux vs mac - fgets() linux vs mac CMake适用于Mac,但不适用于Linux? - CMake works on Mac, but not Linux? 是否有任何 LAME C++ 包装器\\简化器(从纯代码在 Linux Mac 和 Win 上工作)? - Is there any LAME C++ wrapper\simplifier (working on Linux Mac and Win from pure code)? 当我尝试在Linux而不是Mac上进行编译时,为什么会出现“错误:函数&#39;fileno&#39;的隐式声明” - Why am I getting “error: implicit declaration of function ‘fileno’” when I try to compile on Linux but not on a Mac 在 C/C++ 程序中,系统(Windows、Linux 和 Mac OS X)如何调用 main() 函数? - In a C/C++ program, how does the system (Windows, Linux, and Mac OS X) call the main() function? 为什么我在 cppreference 中找不到 random() 函数,但在 Linux 手册页中可以找到? - Why can't I find the random() function in cppreference but can in Linux man page? Mac中的GCC与Linux中的GCC - GCC in Mac vs GCC in linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM