0

I have a problem with a server on MacOS using POSIX socket functions. The problem is that when my client try con connect to the server with the connect() function the server (macOS) send a tcp RST packet and close connection. I tried to disable the FW but the problem is still there.

I put only what I think is useful for you to understand my problem. The same identical code works on Linux (Ubuntu) very well. I think this is a problem about security policy on MacOS.

Server code:

int main (int argc, char *argv[]){
    int s;
    struct sockaddr_in saddr, caddr;
    socklen_t addrlen = sizeof(struct sockaddr_in);
    inet_aton(argv[1], &saddr.sin_addr);
    uint16_t port = htons(atoi(argv[2]));
    int result;
    int bklog = 10;
    int i,n;

    int *sockVett;
    sockVett = new int[nESP];

    while(1) {
        s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (s == -1 ){
            cout << "socket() failed\n";
            return -1;

        }
        for(i=0; i < nESP; i++){
            sockVett[i] = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
        }

        saddr.sin_family = AF_INET;
        saddr.sin_port = port;
        saddr.sin_addr.s_addr = INADDR_ANY;

        bind(s, (struct sockaddr *) &saddr, sizeof(saddr));

        listen (s, bklog);


        for(i=0; i < nESP; i++){
            if(debug) printf("SockVett[%d] is waiting for connection..\n", i+1);
            sockVett[i] = accept(s, (struct sockaddr *) &caddr, &addrlen);
            if(debug) printf("SockVett[%d] connected\n", i+1);
        }
        if(debug) printf("All ESP connected\n\n ---Start program---\n");


    }
}

My server app blocks at the first accept() call in for(...) loop and with Wireshark I observed that my server send a TCP RST packet and so close the connection.

Thanks everybody!

thb
  • 13,796
  • 3
  • 40
  • 68
  • What port number have you actually tried using? Have you tried checking the return values from `bind()`, `listen()`, etc. for errors? – Ken Thomases Dec 06 '18 at 20:29
  • yes I check error but everything seems ok. As port number I'm using 1500.. I tried also 5000 and 5001 but nothing has changed – user10458285 Dec 06 '18 at 20:34
  • What's the value of `nESP`? And why do you initialize `sockVett[]` with sockets? Those will only be ignored after the descriptors are replaced in the array by the accepted connection sockets later. – Ken Thomases Dec 06 '18 at 20:36
  • `nESP` is a variable that tells me how many client I have to connect to my server and I initialize one socket for each client (with `sockVett [ ] = socket( ...) ` ). On Ubuntu this code works. – user10458285 Dec 07 '18 at 09:32
  • If you create too many, you may be exhausting some process or system limit. And, there's no reason to create a socket for each client. You don't use that socket. You just overwrite the fd later and leak it. The connection for the client is what's returned by `accept()`. – Ken Thomases Dec 07 '18 at 19:53
  • the problem is not there because I'm using `nESP = 1` for debug. Anyway I use `sockVett[ ]` because after the `accept()` I create nESP threads, each one of them to serve one socket. – user10458285 Dec 08 '18 at 15:18
  • Why do you call `socket` in the first `for` loop and throw away the results? You never look at the values you store in the `sockVett`array in the first `for` loop and you overwrite them in the second `for` loop so that you never can! – David Schwartz Dec 09 '18 at 03:12
  • Ok, maybe this is true! I overwrite values of sockVett but this is not my problem... please focus on that!! – user10458285 Dec 10 '18 at 07:04

0 Answers0