#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Error creating socket");
return 1;
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(8080);
if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("Error binding socket");
return 1;
}
if (listen(sockfd, 5) < 0) {
perror("Error listening on socket");
return 1;
}
while (1) {
int newsockfd = accept(sockfd, NULL, NULL);
if (newsockfd < 0) {
perror("Error accepting connection");
continue;
}
char buf[100];
int n = read(newsockfd, buf, sizeof(buf));
if (n < 0) {
perror("Error reading from socket");
continue;
}
write(newsockfd, buf, n);
close(newsockfd);
}
close(sockfd);
return 0;
}