Socket auf IP referenzieren?



  • Hallo,

    Also meine Frage wäre: "Ist es möglich einen Socket optional den IP Parameter zu übergeben?"
    Also in der Art:
    Erstelle den Socket auf 192.168.1.1 auf Port 999

    Mein Problem ist nämlich, dass ich ein IPCop System habe, welches 4 NICs hat und daher auch 4 verschiedene IP Adressen. Und ich will 4 verschiedene Sockets - je einer pro IP Adresse - auf verschiedene Ports binden.

    Codeauszug:

    srv.sin_addr.s_addr = INADDR_ANY;
            srv.sin_port = htons( (unsigned short int) atol(argv[1]));
            srv.sin_family = AF_INET;
    
            if (bind(s,(struct sockaddr*) &srv, sizeof(srv)) == -1)
            {
                    perror("bind() failed");
                    return 3;
            }
    

    Danke für die Hilfe!

    LG



  • Ja. Genau dazu ist bind da.
    Anstelle INADDR_ANY einfach die entsprechende IP Adresse verwenden (und mit inet_addr(...) in den zahlenwert umwandlen).



  • simon.gysi schrieb:

    Ja. Genau dazu ist bind da.
    Anstelle INADDR_ANY einfach die entsprechende IP Adresse verwenden (und mit inet_addr(...) in den zahlenwert umwandlen).

    Ja, genau so hatte ich das auch gedacht und auch gelöst, aber es kann keine Connection hergestellt werden. Also hier die Codebeispiele:

    void blue_listen ()
    {
            int s, c;
            struct sockaddr_in srv, cli;
            socklen_t cli_size;
    
            s = socket(AF_INET, SOCK_STREAM, 0);
    
            srv.sin_addr.s_addr = INADDR_ANY;
            srv.sin_port = htons( (unsigned short int) 9997);
            srv.sin_family = AF_INET;
    
            if (bind(s,(struct sockaddr*) &srv, sizeof(srv)) == -1)
            {
                    perror("bind() failed");
                    return 3;
            }
    
            if (listen(s, 3) == -1)
            {
                    perror("listen() failed");
                    return 4;
            }
    
            for(;;)
            {
    
                    cli_size = sizeof(cli);
                    c = accept(s, (struct sockaddr *) &cli, &cli_size);
    
                    // Client connected
                    close(c);
            }
    }
    

    telnet localhost 9997 - funktioniert prima!

    void blue_listen ()
    {
            int s, c;
            struct sockaddr_in srv, cli;
            socklen_t cli_size;
    
            s = socket(AF_INET, SOCK_STREAM, 0);
    
            srv.sin_addr.s_addr = inet_addr("10.0.0.3");
            srv.sin_port = htons( (unsigned short int) 9997);
            srv.sin_family = AF_INET;
    
            if (bind(s,(struct sockaddr*) &srv, sizeof(srv)) == -1)
            {
                    perror("bind() failed");
                    return 3;
            }
    
            if (listen(s, 3) == -1)
            {
                    perror("listen() failed");
                    return 4;
            }
    
            for(;;)
            {
    
                    cli_size = sizeof(cli);
                    c = accept(s, (struct sockaddr *) &cli, &cli_size);
    
                    // Client connected
                    close(c);
            }
    }
    

    telnet localhost 9997
    - keine connection

    ifconfig eth0
    => ip: 10.0.0.3

    LG



  • telnet localhost 9997
    - keine connection

    ifconfig eth0
    => ip: 10.0.0.3

    Ja, weil localhost = 127.0.0.1 ist.
    Du musst also so connecten: telnet 10.0.0.0 9997

    Simon



  • auch telnet auf 10.0.0.0 9997
    sowie telnet auf 10.0.0.3 9997 funktioniert nicht.

    Und eigentlich sollte ja telnet
    10.0.0.3 9997 funktionieren, da der Socket auf diese IP referenziert wird.

    lg



  • kichwieder schrieb:

    auch telnet auf 10.0.0.0 9997

    War 10.0.0.0 war ein Schreibfehler meinerseits; muss natürlich 10.0.0.3 heissen.
    Naja, scheint ja auch nicht zu funktionieren.
    Simon


Anmelden zum Antworten