/* Netlab 2009 - group 1 - assignment 3 * Johan IJsveld * Rick van der Zwet */ #include #include #include #include #include #include #include #include #include #define LISTEN_BACKLOG 0 #define BUF_SIZE 1000 #include int main() { int conn_fd; char buf[BUF_SIZE]; ssize_t recv_size; int retno; /* Create a tcp/ip socket */ int mysocket = socket(PF_INET, SOCK_STREAM, 0); struct sockaddr_in my_addr; struct sockaddr_in their_addr; int sin_size; // ip(7) Create listen port, NOTE addresses in byte-order BYTEORDER(3) my_addr.sin_family = AF_INET; // By default my_addr.sin_port = htons(12345); my_addr.sin_addr.s_addr = htonl(INADDR_ANY); memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero); // get connected retno = bind(mysocket, (struct sockaddr *) &my_addr, sizeof(struct sockaddr_in)); retno = listen(mysocket, LISTEN_BACKLOG); sin_size = sizeof(struct sockaddr_in); // Make it a server, e.g. never ending while(1) { conn_fd = accept(mysocket, (struct sockaddr *)&their_addr, &sin_size); if (fork() == 0) { // child will help to echo the stuff while(1) { retno = recv(conn_fd, &buf, BUF_SIZE, 0); printf("%s\n", buf); if (retno <= 0) break; retno = send(conn_fd, &buf, retno, 0); if (retno <= 0) break; } close(conn_fd); } } close(mysocket); return EX_OK; }