Last active
June 16, 2025 21:34
-
-
Save FedericoPonzi/2a37799b6c601cce6c1b to your computer and use it in GitHub Desktop.
Revisions
-
FedericoPonzi revised this gist
Apr 20, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -84,7 +84,7 @@ int main(void) { // Closing connection closesocket(Csocket); ClearWinSock(); printf("\n"); system("pause"); return (0); } -
FedericoPonzi revised this gist
Apr 20, 2020 . 1 changed file with 8 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ #include <stdio.h> #define BUFFERSIZE 512 #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 // 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; } // 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"); // server IP sad.sin_port = htons(5193); // Server port // 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"; // 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 } // Closing connection closesocket(Csocket); ClearWinSock(); printf("\n"); // Print a final linefeed -
FedericoPonzi renamed this gist
Apr 20, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
FedericoPonzi created this gist
Feb 22, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); }