Created
November 23, 2023 05:12
-
-
Save Hypro999/f8a90cfd6b78b7b52cbbd76586223c8c to your computer and use it in GitHub Desktop.
Socket programming using just BSD style sockets vs Core Framework. Written mostly using ChatGPT (with some tweaking).
This file contains 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 characters
/* compile with: gcc -o server_bsd server_bsd.c */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <arpa/inet.h> | |
#include <sys/socket.h> | |
#define PORT 8000 | |
int main() { | |
int server_socket, client_socket; | |
struct sockaddr_in server_addr, client_addr; | |
socklen_t client_addr_len; | |
// Create a socket | |
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) { | |
perror("Socket creation failed"); | |
exit(EXIT_FAILURE); | |
} | |
// Initialize server address struct | |
memset(&server_addr, 0, sizeof(server_addr)); | |
server_addr.sin_family = AF_INET; | |
server_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); // Listen on localhost | |
server_addr.sin_port = htons(PORT); | |
// Bind the socket to the server address | |
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { | |
perror("Binding failed"); | |
close(server_socket); | |
exit(EXIT_FAILURE); | |
} | |
// Listen for incoming connections | |
if (listen(server_socket, 5) == -1) { | |
perror("Listening failed"); | |
close(server_socket); | |
exit(EXIT_FAILURE); | |
} | |
printf("Server listening on port %d...\n", PORT); | |
// Accept incoming connections | |
client_addr_len = sizeof(client_addr); | |
if ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len)) == -1) { | |
perror("Accepting connection failed"); | |
close(server_socket); | |
exit(EXIT_FAILURE); | |
} | |
printf("Connection accepted from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); | |
// Close sockets | |
close(client_socket); | |
close(server_socket); | |
return 0; | |
} |
This file contains 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 characters
/* compile with: gcc -o server_corefoundation server_corefoundation.c -framework CoreFoundation */ | |
#include <CoreFoundation/CoreFoundation.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <stdio.h> | |
#define PORT 8000 | |
void AcceptCallback(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) { | |
// Handle incoming connections | |
CFSocketNativeHandle sock = *(CFSocketNativeHandle *)data; | |
struct sockaddr_in peerAddress; | |
socklen_t peerAddressLength = sizeof(peerAddress); | |
if (getpeername(sock, (struct sockaddr *)&peerAddress, &peerAddressLength) == 0) { | |
char ipAddress[INET_ADDRSTRLEN]; | |
inet_ntop(AF_INET, &peerAddress.sin_addr, ipAddress, sizeof(ipAddress)); | |
printf("Connection accepted from %s:%d\n", ipAddress, ntohs(peerAddress.sin_port)); | |
} | |
// Close the accepted connection | |
close(sock); | |
// Stop the run loop after receiving one connection | |
CFRunLoopStop(CFRunLoopGetCurrent()); | |
} | |
int main() { | |
CFSocketRef socket; | |
struct sockaddr_in addr; | |
memset(&addr, 0, sizeof(addr)); | |
addr.sin_len = sizeof(addr); | |
addr.sin_family = AF_INET; | |
addr.sin_port = htons(PORT); | |
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
CFSocketContext context = { 0, NULL, NULL, NULL, NULL }; | |
socket = CFSocketCreate(NULL, AF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, (CFSocketCallBack)AcceptCallback, &context); | |
if (!socket) { | |
printf("Failed to create socket\n"); | |
return -1; | |
} | |
CFDataRef address = CFDataCreate(NULL, (UInt8 *)&addr, sizeof(addr)); | |
if (address == NULL) { | |
printf("Failed to create address data\n"); | |
CFRelease(socket); | |
return -1; | |
} | |
CFSocketError err = CFSocketSetAddress(socket, address); | |
if (err != kCFSocketSuccess) { | |
printf("Failed to set socket address\n"); | |
CFRelease(address); | |
CFRelease(socket); | |
return -1; | |
} | |
CFRunLoopSourceRef runLoopSource = CFSocketCreateRunLoopSource(NULL, socket, 0); | |
if (runLoopSource == NULL) { | |
printf("Failed to create run loop source\n"); | |
CFRelease(socket); | |
CFRelease(address); | |
return -1; | |
} | |
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes); | |
CFRelease(runLoopSource); | |
CFRelease(socket); | |
CFRelease(address); | |
printf("Server listening on port %d...\n", PORT); | |
// Run the run loop to start listening for connections | |
CFRunLoopRun(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment