#include #include #include #include #include #include #include #define PORT 8080 #define BUF_SIZE 100 #define BACKLOG 4 char buf[BUF_SIZE]; int numRead; void echo(int cfd) { while ((numRead = read(cfd, buf, BUF_SIZE)) > 0) { printf("%s","Message received from a client:"); puts(buf); if (write(cfd, buf, numRead) != numRead) return; int main() { int sfd; int cfd; int b; struct sockaddr_in sa; FILE *client; fd_set readfds, masterfds; int nready; int i; int maxfd; if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); return 1; bzero(&sa, sizeof sa); if (listen(sfd, BACKLOG) == -1) sa.sin_family = AF_INET; sa.sin_port = htons(PORT); if (INADDR_ANY) sa.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sfd, (struct sockaddr *)&sa, sizeof sa) < 0) { perror("bind"); return 2; for (;;) { cfd = accept(sfd, NULL, NULL); if (cfd == -1) } return 1; if (fork() == 0) { close(sfd); echo(cfd); close(cfd); exit(0); close(cfd); } } } } return 1; } }