#include #include #include #include #include #include #include #include #include int client_fd; void* write_to_client(void *p){ write(client_fd, p, strlen(p)); for(char *cc = p; *cc; cc++) write(client_fd, cc, 1); return 0; } int main(int argc, char ** argv){ struct sockaddr_in sad; sad.sin_port = htons(5144); sad.sin_addr.s_addr = INADDR_ANY; sad.sin_family = AF_INET; int skt = socket(AF_INET, SOCK_STREAM, 0); // Step 1 if(skt == -1){ perror("socket"); return 1; } if( bind(skt, (struct sockaddr*)(&sad), sizeof(struct sockaddr_in)) ){ // step 2 perror("bind"); return 1; } if( listen(skt, 5) ){ // step 3 perror("listen"); return 1; } while(1){ struct sockaddr_in client_address; socklen_t address_size = sizeof(struct sockaddr_in); client_fd = accept(skt, (struct sockaddr *)(&client_address), &address_size); // step 4 printf("Connection made from address %s\n", inet_ntoa(client_address.sin_addr)); pthread_t t1, t2; pthread_create(&t1, 0, write_to_client, "This is a string that's not about giraffes"); pthread_create(&t1, 0, write_to_client, "It's about raccoons instead, which are shorter than giraffes"); pthread_join(t1, 0); pthread_join(t2, 0); close(client_fd); } return 0; }