Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 19

Discussione: client.c

  1. #1

    client.c

    Perchè non riesco a compilare questo sorgente con Bloodshed Dev-C++?
    Eppure mi è stato fornito dal mio prof.!

    /* client.c - code for example client program that uses TCP */
    #include <windows.h>
    #define closesocket close
    #include <sys/types.h>
    #include <winsock.h>
    #include <stdio.h>
    #include <string.h>

    #define PROTOPORT 5193 /* default protocol port number */

    extern int errno;
    char localhost[] = "localhost"; /* default host name */
    /*------------------------------------------------------------------------
    * Program: client
    *
    * Purpose: allocate a socket, connect to a server, and print all output
    *
    * Syntax: client [ host [port] ]
    *
    * host - name of a computer on which server is executing
    * port - protocol port number server is using
    *
    * Note: Both arguments are optional. If no host name is specified,
    * the client uses "localhost"; if no protocol port is
    * specified, the client uses the default given by PROTOPORT.
    *
    *------------------------------------------------------------------------
    */
    main(argc, argv)
    int argc;
    char *argv[];
    {
    struct hostent *ptrh; /* pointer to a host table entry */
    struct protoent *ptrp; /* pointer to a protocol table entry */
    struct sockaddr_in sad; /* structure to hold an IP address */
    int sd; /* socket descriptor */
    int port; /* protocol port number */
    char *host; /* pointer to host name */
    int n; /* number of characters read */
    char buf[1000]; /* buffer for data from the server */
    #ifdef WIN32
    WSADATA wsaData;
    WSAStartup(0x0101, &wsaData);
    #endif
    memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
    sad.sin_family = AF_INET; /* set family to Internet */

    /* Check command-line argument for protocol port and extract */
    /* port number if one is specified. Otherwise, use the default */
    /* port value given by constant PROTOPORT */

    if (argc > 2) { /* if protocol port specified */
    port = atoi(argv[2]); /* convert to binary */
    } else {
    port = PROTOPORT; /* use default port number */
    }
    if (port > 0) /* test for legal value */
    sad.sin_port = htons((u_short)port);
    else { /* print error message and exit */
    fprintf(stderr,"bad port number %s\n",argv[2]);
    exit(1);
    }

    /* Check host argument and assign host name. */

    if (argc > 1) {
    host = argv[1]; /* if host argument specified */
    } else {
    host = localhost;
    }

    /* Convert host name to equivalent IP address and copy to sad. */

    ptrh = gethostbyname(host);
    if ( ((char *)ptrh) == NULL ) {
    fprintf(stderr,"invalid host: %s\n", host);
    exit(1);
    }
    memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length);

    /* Map TCP transport protocol name to protocol number. */

    if ( ((int)(ptrp = getprotobyname("tcp"))) == 0) {
    fprintf(stderr, "cannot map \"tcp\" to protocol number");
    exit(1);
    }

    /* Create a socket. */

    sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
    if (sd < 0) {
    fprintf(stderr, "socket creation failed\n");
    exit(1);
    }

    /* Connect the socket to the specified server. */

    if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
    fprintf(stderr,"connect failed\n");
    exit(1);
    }

    /* Repeatedly read data from socket and write to user's screen. */

    n = recv(sd, buf, sizeof(buf), 0);
    while (n > 0) {
    printf("%s",buf);
    n = recv(sd, buf, sizeof(buf), 0);
    }

    /* Close the socket. */

    closesocket(sd);

    /* Terminate the client program gracefully. */
    /* getch(); */
    exit(0);
    }


    Se non mi sbaglio devo dare delle direttive di compilazione ma non ricordo quali!

    Qualcuno può aiutarmi?

    Ciao

  2. #2
    Questo è il log di compilazione!


    Compilatore: Default compiler
    Esecuzione di g++.exe...
    g++.exe "D:\Documents and Settings\magnus\Desktop\client1.cpp" -o "D:\Documents and Settings\magnus\Desktop\client1.exe" -I"f:\Dev-Cpp\include" -I"f:\Dev-Cpp\include\c++" -I"f:\Dev-Cpp\include" -L"f:\Dev-Cpp\lib"
    D:/Documents and Settings/magnus/Desktop/client1.cpp:29: `argc' was not
    declared in this scope
    D:/Documents and Settings/magnus/Desktop/client1.cpp:29: `argv' was not
    declared in this scope
    D:/Documents and Settings/magnus/Desktop/client1.cpp:30: ISO C++ forbids
    declaration of `main' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:30: initializer list being
    treated as compound expression
    D:/Documents and Settings/magnus/Desktop/client1.cpp:30: syntax error before `
    int'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:31: array size missing in
    `argv'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:32: parse error before `{'
    token
    D:/Documents and Settings/magnus/Desktop/client1.cpp:45: ISO C++ forbids
    declaration of `WSAStartup' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:45: `int WSAStartup'
    redeclared as different kind of symbol
    f:/Dev-Cpp/include/winsock2.h:490: previous declaration of `int
    WSAStartup(short unsigned int, WSADATA*)'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:45: initializer list being
    treated as compound expression
    D:/Documents and Settings/magnus/Desktop/client1.cpp:45: invalid conversion
    from `WSADATA*' to `int'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:47: ISO C++ forbids
    declaration of `memset' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:47: `int memset'
    redeclared as different kind of symbol
    f:/Dev-Cpp/include/string.h:56: previous declaration of `void* memset(void*,
    int, unsigned int)'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:47: initializer list being
    treated as compound expression
    D:/Documents and Settings/magnus/Desktop/client1.cpp:48: syntax error before `.
    ' token
    D:/Documents and Settings/magnus/Desktop/client1.cpp:63: ISO C++ forbids
    declaration of `exit' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:63: `int exit' redeclared
    as different kind of symbol
    f:/Dev-Cpp/include/stdlib.h:336: previous declaration of `void exit(int)'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:64: parse error before `}'
    token
    D:/Documents and Settings/magnus/Desktop/client1.cpp:76: ISO C++ forbids
    declaration of `ptrh' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:76: invalid conversion
    from `hostent*' to `int'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:77: parse error before `if
    '
    D:/Documents and Settings/magnus/Desktop/client1.cpp:79: ISO C++ forbids
    declaration of `exit' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:79: redefinition of `int
    exit'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:63: `int exit' previously
    defined here
    D:/Documents and Settings/magnus/Desktop/client1.cpp:80: parse error before `}'
    token
    D:/Documents and Settings/magnus/Desktop/client1.cpp:81: base operand of `->'
    is not a pointer
    D:/Documents and Settings/magnus/Desktop/client1.cpp:81: base operand of `->'
    is not a pointer
    D:/Documents and Settings/magnus/Desktop/client1.cpp:81: ISO C++ forbids
    declaration of `memcpy' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:81: `int memcpy'
    redeclared as different kind of symbol
    f:/Dev-Cpp/include/string.h:54: previous declaration of `void* memcpy(void*,
    const void*, unsigned int)'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:81: initializer list being
    treated as compound expression
    D:/Documents and Settings/magnus/Desktop/client1.cpp:85: parse error before `if
    '
    D:/Documents and Settings/magnus/Desktop/client1.cpp:87: ISO C++ forbids

    declaration of `exit' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:87: redefinition of `int
    exit'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:79: `int exit' previously
    defined here
    D:/Documents and Settings/magnus/Desktop/client1.cpp:88: parse error before `}'
    token
    D:/Documents and Settings/magnus/Desktop/client1.cpp:92: ISO C++ forbids
    declaration of `sd' with no type

    D:/Documents and Settings/magnus/Desktop/client1.cpp:92: redefinition of `int

    sd'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:36: `int sd' previously
    declared here
    D:/Documents and Settings/magnus/Desktop/client1.cpp:93: parse error before `if
    '

    D:/Documents and Settings/magnus/Desktop/client1.cpp:95: ISO C++ forbids
    declaration of `exit' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:95: redefinition of `int
    exit'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:87: `int exit' previously
    defined here
    D:/Documents and Settings/magnus/Desktop/client1.cpp:96: parse error before `}'

    token

    D:/Documents and Settings/magnus/Desktop/client1.cpp:102: ISO C++ forbids
    declaration of `exit' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:102: redefinition of `int
    exit'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:95: `int exit' previously
    defined here

    D:/Documents and Settings/magnus/Desktop/client1.cpp:103: parse error before `}
    ' token
    D:/Documents and Settings/magnus/Desktop/client1.cpp:106: ISO C++ forbids
    declaration of `n' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:106: redefinition of `int
    n'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:39: `int n' previously
    declared here
    D:/Documents and Settings/magnus/Desktop/client1.cpp:107: ISO C++ forbids
    declaration of `printf' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:107: `int printf'
    redeclared as different kind of symbol
    f:/Dev-Cpp/include/stdio.h:213: previous declaration of `int printf(const
    char*, ...)'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:107: initializer list
    being treated as compound expression
    D:/Documents and Settings/magnus/Desktop/client1.cpp:107: invalid conversion

    from `char*' to `int'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:109: parse error before `
    while'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:110: ISO C++ forbids
    declaration of `scanf' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:110: `int scanf'
    redeclared as different kind of symbol
    f:/Dev-Cpp/include/stdio.h:233: previous declaration of `int scanf(const char*,
    ...)'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:110: initializer list
    being treated as compound expression
    D:/Documents and Settings/magnus/Desktop/client1.cpp:110: invalid conversion
    from `char*' to `int'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:111: ISO C++ forbids
    declaration of `send' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:111: `int send' redeclared
    as different kind of symbol
    f:/Dev-Cpp/include/winsock2.h:479: previous declaration of `int send(unsigned
    int, const char*, int, int)'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:111: initializer list
    being treated as compound expression
    D:/Documents and Settings/magnus/Desktop/client1.cpp:112: ISO C++ forbids
    declaration of `sprintf' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:112: `int sprintf'
    redeclared as different kind of symbol
    f:/Dev-Cpp/include/stdio.h:214: previous declaration of `int sprintf(char*,
    const char*, ...)'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:112: initializer list

    being treated as compound expression
    D:/Documents and Settings/magnus/Desktop/client1.cpp:112: invalid conversion
    from `const char*' to `int'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:113: ISO C++ forbids
    declaration of `m' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:113: redefinition of `int
    m'

    D:/Documents and Settings/magnus/Desktop/client1.cpp:42: `int m' previously
    declared here

    D:/Documents and Settings/magnus/Desktop/client1.cpp:114: parse error before `

    if'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:123: ISO C++ forbids
    declaration of `exit' with no type
    D:/Documents and Settings/magnus/Desktop/client1.cpp:123: redefinition of `int
    exit'
    D:/Documents and Settings/magnus/Desktop/client1.cpp:102: `int exit' previously
    defined here
    D:/Documents and Settings/magnus/Desktop/client1.cpp:124: parse error before `}
    ' token

    Esecuzione terminata

  3. #3
    Prova a salvare il file come "C++ Source" e non come "C Source", dovrebbe funzionare.


    P.S. Anche tu al corso di Reti di Calcolatori, giusto?



    La luce è più veloce del suono,ecco xchè alcune persone sembrano brillanti fino a quando non parlano


  4. #4
    Ora non mi va di vederlo tutto, ma forse anche solo se cambi questo codice (stile parecchio antiquato):

    codice:
    main(argc, argv)
    int argc;
    char *argv[];
    Con questo:

    codice:
    int main(int argc, char *argv[])
    E` probabile che funzioni.

  5. #5
    Originariamente inviato da m@ximo
    Prova a salvare il file come "C++ Source" e non come "C Source", dovrebbe funzionare.


    P.S. Anche tu al corso di Reti di Calcolatori, giusto?



    Si

    Segui ad Agnano?

  6. #6
    Putroppo anche come C++ source file non funziona!

  7. #7
    magnus seguo anche io il tuo stesso corso, e lo stesso codice mi funziona perfettamente con Dev-C++ (ovviamente dopo aver fatto la modifica che ti ho detto), prova a scaricarti questa versione di C++.
    Se non la trovi posso darti il link, fammi sapere.

    Max
    La luce è più veloce del suono,ecco xchè alcune persone sembrano brillanti fino a quando non parlano


  8. #8
    Boh!
    Ma scusa anche in laboratorio non funzionava così...

    Andava aggiunta una stringa per compilarlo...

    Mi ricordo bene.

    Era come se mancasse qualche libreria da linkare o giù di lì...

    Sicuro che ti funzioni comunque?

    Una domanda: ma tu utilizzi il compilatore della BloodShed?

    Ciao.

  9. #9
    Originariamente inviato da r0x
    Ora non mi va di vederlo tutto, ma forse anche solo se cambi questo codice (stile parecchio antiquato):

    codice:
    main(argc, argv)
    int argc;
    char *argv[];
    Con questo:

    codice:
    int main(int argc, char *argv[])
    E` probabile che funzioni.
    Cambiando quelle righe di codice da questi errori:

    Compilatore: Default compiler
    Esecuzione di g++.exe...
    g++.exe "F:\Dev-Cpp\sorgenti\client.cpp" -o "F:\Dev-Cpp\sorgenti\client.exe" -I"f:\Dev-Cpp\include" -I"f:\Dev-Cpp\include\c++" -I"f:\Dev-Cpp\include" -L"f:\Dev-Cpp\lib"
    F:/Dev-Cpp/sorgenti/client.cpp: In function `int main(int, char**)':
    F:/Dev-Cpp/sorgenti/client.cpp:111: `close' undeclared (first use this
    function)
    F:/Dev-Cpp/sorgenti/client.cpp:111: (Each undeclared identifier is reported
    only once for each function it appears in.)

    Esecuzione terminata

    Nessuno sa darmi una mano?

    Ciao.

  10. #10
    Forse quelle righe da inserire per il compilatore servivano ad attivare delle funzioni particolari per il compilatore evitando questi errori...

    Aiuto!!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.