#include #include #include #include #include int main() { struct sockaddr_in addr; addr.sin_family = AF_INET; /* IPv4 */ addr.sin_port = htons(8000); /* Port */ addr.sin_addr.s_addr = INADDR_ANY; /* equals to 0.0.0.0 */ int sfd = socket(AF_INET, SOCK_STREAM, 0); if (sfd == -1) { perror("could not open socket"); return 1; } int opt = 1; setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); int bind_addr = bind(sfd, (struct sockaddr *)&addr, sizeof(addr)); if (bind_addr == -1) { perror("could not bind socket"); return 1; } int listen_addr = listen(sfd, 100); if (listen_addr == -1) { perror("could not listen"); return 1; } while (1) { int conn = accept(sfd, NULL, NULL); if (conn == -1) { perror("could not accept connection"); return 1; } char buf[1024]; ssize_t n = read(conn, buf, sizeof(buf)); write(conn, buf, n); close(conn); } return 0; }