Skip to content

Instantly share code, notes, and snippets.

@anthonyraf
Forked from FedericoPonzi/socket_portable.c
Created May 14, 2022 15:51
Show Gist options
  • Save anthonyraf/01e8a46962473fe50075d8598ff5e64d to your computer and use it in GitHub Desktop.
Save anthonyraf/01e8a46962473fe50075d8598ff5e64d to your computer and use it in GitHub Desktop.
C sockets portable in windows/linux example
// 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 // Default port number
# 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
// Socket creation
int Csocket;
Csocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (Csocket < 0) {
ErrorHandler("socket creation failed.\n");
closesocket(Csocket);
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");
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
}
// Closing connection
closesocket(Csocket);
ClearWinSock();
printf("\n");
system("pause");
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment