Sockets mit Codeblocks



  • Wie kan ich mir sockets für Codeblocks einrichten? Habe mir die Bibliothek (zumindestens hoffe ich dass es die richtige ist) happycoder-libsocket-dev installiert. Jetzt weiß ich aber nicht mehr so weiter, irgendwas muss ich ja wohl noch im Compiler einstellen? Wahrscheinlich wo sich diese Bibliothek befindet?! Nur wo? Oder muss ich in Codeblocks noch was anderes einstellen? Kann mir jemand helfen? Habe allgemein auch noch null Ahnung von sockets.

    Also laufen tuts definitiv nicht, folgender Code compiliert nicht, bzw. mit folgender Fehlermeldung:

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    
    void error(char *msg)
    {
        perror(msg);
        exit(0);
    }
    
    int main(int argc, char *argv[])
    {
        int sockfd, portno, n;
        struct sockaddr_in serv_addr;
        struct hostent *server;
    
        char buffer[256];
        if (argc < 3) {
           fprintf(stderr,"usage %s hostname port\n", argv[0]);
           exit(0);
        }
        portno = atoi(argv[2]);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0)
            error("ERROR opening socket");
        server = gethostbyname(argv[1]);
        if (server == NULL) {
            fprintf(stderr,"ERROR, no such host\n");
            exit(0);
        }
        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        bcopy((char *)server->h_addr,
             (char *)&serv_addr.sin_addr.s_addr,
             server->h_length);
        serv_addr.sin_port = htons(portno);
        if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
            error("ERROR connecting");
        printf("Please enter the message: ");
        bzero(buffer,256);
        fgets(buffer,255,stdin);
        n = write(sockfd,buffer,strlen(buffer));
        if (n < 0)
             error("ERROR writing to socket");
        bzero(buffer,256);
        n = read(sockfd,buffer,255);
        if (n < 0)
             error("ERROR reading from socket");
        printf("%s\n",buffer);
        return 0;
    }
    

    Compiling: /home/andi/C++ Projektordner/client.c
    /home/andi/C++ Projektordner/client.c: In function ‘void error(char*)’:
    /home/andi/C++ Projektordner/client.c:10: error: ‘exit’ was not declared in this scope
    /home/andi/C++ Projektordner/client.c: In function ‘int main(int, char**)’:
    /home/andi/C++ Projektordner/client.c:22: error: ‘exit’ was not declared in this scope
    /home/andi/C++ Projektordner/client.c:24: error: ‘atoi’ was not declared in this scope
    /home/andi/C++ Projektordner/client.c:27: warning: deprecated conversion from string constant to ‘char*’
    /home/andi/C++ Projektordner/client.c:31: error: ‘exit’ was not declared in this scope
    /home/andi/C++ Projektordner/client.c:33: error: ‘bzero’ was not declared in this scope
    /home/andi/C++ Projektordner/client.c:37: error: ‘bcopy’ was not declared in this scope
    /home/andi/C++ Projektordner/client.c:39: error: cannot convert ‘sockaddr_in*’ to ‘const sockaddr*’ for argument ‘2’ to ‘int connect(int, const sockaddr*, socklen_t)’
    /home/andi/C++ Projektordner/client.c:40: warning: deprecated conversion from string constant to ‘char*’
    /home/andi/C++ Projektordner/client.c:44: error: ‘strlen’ was not declared in this scope
    /home/andi/C++ Projektordner/client.c:44: error: ‘write’ was not declared in this scope
    /home/andi/C++ Projektordner/client.c:46: warning: deprecated conversion from string constant to ‘char*’
    /home/andi/C++ Projektordner/client.c:48: error: ‘read’ was not declared in this scope
    /home/andi/C++ Projektordner/client.c:50: warning: deprecated conversion from string constant to ‘char*’
    Process terminated with status 1 (0 minutes, 0 seconds)
    10 errors, 4 warnings

    Ich hoffe mir kann jemand helfen.

    Gruß
    Stromberg



  • Ohne mir jetzt Deinen Code (Includes ausgenommen) angesehen zu haben, fällt mir folgendes auf:

    • Für exit und atoi brauchst Du <stdlib.h>.
    • bzero und bcopy sind afaik veraltet, stattdessen verwendet man eher memmove und memset.
    • Für strlen brauchst Du <string.h>.
    • Für read und write brauchst Du unistd.h


  • Und error sollte ein char const *msg als Parameter nehmen.

    btw. man: bzero(3)/man: bcopy(3) sind veraltet. Nimm lieber man: memset(3)/man: memcpy(3).


Anmelden zum Antworten