Unterschied UNIX Socket/TCP Socket
https://www.tobscore.com/socket-programmierung-in-c/
Siehe C
server .c
#include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef _WIN32 #include <winsock.h> #include <io.h> #else #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <arpa/inet.h> #include <unistd.h> #endif int main(void) { #ifdef _WIN32 SOCKET sock, fd; WSADATA wsaData; WSAStartup(0x102,&wsaData); #else int sock, fd; #endif struct sockaddr_in server, client; memset(&server, 0, sizeof(server)); server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_port = htons(8080); sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); bind(sock, (struct sockaddr*)&server, sizeof( server)); listen(sock, 5); unsigned int len; fd = accept(sock, (struct sockaddr*)&client, &len); char buffer[1024]; if( (len = recv(fd, buffer, 1024, 0)) > 0) { printf("%s\n", buffer); send(sock, buffer, strlen(buffer), 0); memset(buffer, 0, 1024); } return 0; }
client.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef _WIN32 #include <winsock.h> #include <io.h> #else #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <arpa/inet.h> #include <unistd.h> #endif int main(void) { #ifdef _WIN32 SOCKET sock, fd; WSADATA wsaData; WSAStartup(0x102,&wsaData); #else int sock, fd; #endif struct sockaddr_in server; memset(&server, 0, sizeof(server)); server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr("127.0.0.1"); server.sin_port = htons(8080); sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); connect(sock, (struct sockaddr*)&server, sizeof(server)); char buffer[] = "Hello world"; send(sock, buffer, sizeof(buffer), 0); unsigned int len; if( (len = recv(sock, buffer, 1024, 0)) > 0) printf("%s\n", buffer); return 0; }
Um eine Verbindung zu verarbeiten ein multithreaded_server.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef _WIN32 #include <winsock.h> #include <io.h> #else #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <arpa/inet.h> #include <unistd.h> #endif #ifdef _WIN32 void process_thread(SOCKET s) { #else void process_thread(int s) { #endif char buffer[1024]; memset(buffer, 0, 1024); int len; while( (len = recv(s, buffer, 1024, 0)) > 0 ) printf("%s\n", buffer); } int main(void) { #ifdef _WIN32 SOCKET sock, fd; WSADATA wsaData; WSAStartup(0x102,&wsaData); #else int sock, fd; #endif struct sockaddr_in server, client; memset(&server, 0, sizeof(server)); server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_port = htons(8080); sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); bind(sock, (struct sockaddr*)&server, sizeof( server)); listen(sock, 5); unsigned int len; char buffer[1024]; #ifdef _WIN32 DWORD thread; #else pthread_t thread; #endif while(1) { fd = accept(sock, (struct sockaddr*)&client, &len); if(fd<0) continue; #ifdef _WIN32 if(!CreateThread(NULL, 0, process_thread, fd, 0, &thread)) continue; #else if(!pthread_create( &thread , NULL , process_thread , fd)) continue; #endif } return 0; }