Skip to content

Instantly share code, notes, and snippets.

@FedericoPonzi
Last active June 16, 2025 21:34
Show Gist options
  • Save FedericoPonzi/2a37799b6c601cce6c1b to your computer and use it in GitHub Desktop.
Save FedericoPonzi/2a37799b6c601cce6c1b to your computer and use it in GitHub Desktop.

Revisions

  1. FedericoPonzi revised this gist Apr 20, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion socket_portable.c
    Original file line number Diff line number Diff line change
    @@ -84,7 +84,7 @@ int main(void) {
    // Closing connection
    closesocket(Csocket);
    ClearWinSock();
    printf("\n"); // Print a final linefeed
    printf("\n");
    system("pause");
    return (0);
    }
  2. FedericoPonzi revised this gist Apr 20, 2020. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions socket_portable.c
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    #include <stdio.h>

    #define BUFFERSIZE 512
    #define PROTOPORT 5193 // Numero di porta di default
    #define PROTOPORT 5193 // Default port number
    # pragma comment(lib,"ws2_32.lib") //Winsock Library
    void ClearWinSock() {
    #if defined WIN32
    @@ -32,7 +32,7 @@ int main(void) {
    }
    #endif

    // CREAZIONE DELLA SOCKET
    // Socket creation
    int Csocket;
    Csocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (Csocket < 0) {
    @@ -41,22 +41,22 @@ int main(void) {
    ClearWinSock();
    return 0;
    }
    // COSTRUZIONE DELL’INDIRIZZO DEL SERVER
    // Server address construction
    struct sockaddr_in sad;
    memset(&sad, 0, sizeof(sad));
    sad.sin_family = AF_INET;
    sad.sin_addr.s_addr = inet_addr("192.168.0.100"); // IP del server
    sad.sin_addr.s_addr = inet_addr("192.168.0.100"); // server IP
    sad.sin_port = htons(5193); // Server port
    // CONNESSIONE AL SERVER
    // Connection to the server
    if (connect(Csocket, (struct sockaddr *) &sad, sizeof(sad)) < 0) {
    ErrorHandler("Failed to connect.\n");
    closesocket(Csocket);
    ClearWinSock();
    return 0;
    }

    char* inputString = "prova"; // Stringa da inviare
    int stringLen = strlen(inputString); // Determina la lunghezza
    char* inputString = "prova"; // String to send
    int stringLen = strlen(inputString);

    if (send(Csocket, inputString, stringLen, 0) != stringLen) {
    ErrorHandler("send() sent a different number of bytes than expected");
    @@ -81,7 +81,7 @@ int main(void) {
    buf[bytesRcvd] = '\0'; // Add \0 so printf knows where to stop
    printf("%s", buf); // Print the echo buffer
    }
    // CHIUSURA DELLA CONNESSIONE
    // Closing connection
    closesocket(Csocket);
    ClearWinSock();
    printf("\n"); // Print a final linefeed
  3. FedericoPonzi renamed this gist Apr 20, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. FedericoPonzi created this gist Feb 22, 2015.
    90 changes: 90 additions & 0 deletions socket_portable
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    // As seen on http://www.di.uniba.it/~reti/LabProRete/Interazione(TCP)Client-Server_Portabile.pdf
    #if defined WIN32
    #include <winsock.h>
    #else
    #define closesocket close
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #endif
    #include <stdio.h>

    #define BUFFERSIZE 512
    #define PROTOPORT 5193 // Numero di porta di default
    # pragma comment(lib,"ws2_32.lib") //Winsock Library
    void ClearWinSock() {
    #if defined WIN32
    WSACleanup();
    #endif
    }

    void ErrorHandler(char *errorMessage) {
    printf(errorMessage);
    }

    int main(void) {
    #if defined WIN32
    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2 ,2), &wsaData);
    if (iResult != 0) {
    printf("error at WSASturtup\n");
    return 0;
    }
    #endif

    // CREAZIONE DELLA SOCKET
    int Csocket;
    Csocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (Csocket < 0) {
    ErrorHandler("socket creation failed.\n");
    closesocket(Csocket);
    ClearWinSock();
    return 0;
    }
    // COSTRUZIONE DELL’INDIRIZZO DEL SERVER
    struct sockaddr_in sad;
    memset(&sad, 0, sizeof(sad));
    sad.sin_family = AF_INET;
    sad.sin_addr.s_addr = inet_addr("192.168.0.100"); // IP del server
    sad.sin_port = htons(5193); // Server port
    // CONNESSIONE AL SERVER
    if (connect(Csocket, (struct sockaddr *) &sad, sizeof(sad)) < 0) {
    ErrorHandler("Failed to connect.\n");
    closesocket(Csocket);
    ClearWinSock();
    return 0;
    }

    char* inputString = "prova"; // Stringa da inviare
    int stringLen = strlen(inputString); // Determina la lunghezza

    if (send(Csocket, inputString, stringLen, 0) != stringLen) {
    ErrorHandler("send() sent a different number of bytes than expected");
    closesocket(Csocket);
    ClearWinSock();
    return 0;
    }

    int bytesRcvd;
    int totalBytesRcvd = 0;
    char buf[BUFFERSIZE]; // buffer for data from the server
    printf("Received: "); // Setup to print the echoed string

    while (totalBytesRcvd < stringLen) {
    if ((bytesRcvd = recv(Csocket, buf, BUFFERSIZE - 1, 0)) <= 0) {
    ErrorHandler("recv() failed or connection closed prematurely");
    closesocket(Csocket);
    ClearWinSock();
    return 0;
    }
    totalBytesRcvd += bytesRcvd; // Keep tally of total bytes
    buf[bytesRcvd] = '\0'; // Add \0 so printf knows where to stop
    printf("%s", buf); // Print the echo buffer
    }
    // CHIUSURA DELLA CONNESSIONE
    closesocket(Csocket);
    ClearWinSock();
    printf("\n"); // Print a final linefeed
    system("pause");
    return (0);
    }