#include #include #include #include #include #define SERVER_PORT 5432 #define MAX_PENDING 5 #define MAX_LINE 256 int main() { struct sockaddr_in sin; char buf[MAX_LINE]; int len; int serverSocket, clientSocket; /* build address data structure */ bzero((char *)&sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(SERVER_PORT); /* setup passive open */ if ((serverSocket = socket(PF_INET, SOCK_STREAM, 0)) < 0) { perror("server: socket"); exit(1); } if ((bind(serverSocket, (struct sockaddr *)&sin, sizeof(sin))) < 0) { perror("server: bind"); exit(1); } listen(serverSocket, MAX_PENDING); /* wait for connection, then receive and print text */ while (1) { if ((clientSocket = accept(serverSocket, (struct sockaddr *)&sin, &len)) < 0) { perror("server: accept"); exit(1); } while (len = recv(clientSocket, buf, sizeof(buf), 0)) { fputs(buf, stdout); } close(clientSocket); } }