1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h>
#define PORT 8080
int main() { int server_socket, client_socket; struct sockaddr_in server_addr, client_addr; char buffer[1024];
server_socket = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_port = htons(PORT);
bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)); listen(server_socket, 5);
while (1) { socklen_t addr_len = sizeof(client_addr); client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &addr_len); recv(client_socket, buffer, sizeof(buffer), 0); send(client_socket, buffer, strlen(buffer), 0); close(client_socket); }
close(server_socket); return 0; }
|