#include #include #include #include #include #include #include #include #include void listen_for_client(int skt){ int client_fd; struct sockaddr_in client_address; char client_message[8]; char *cm = client_message; printf("Client message[64] = %x, needs to be %x\n", client_message[64], client_message[64] - 0x32); // client_message[48] = 0x7a; socklen_t address_size = sizeof(struct sockaddr_in); printf("Listening on socket %d\n", skt); client_fd = accept(skt, (struct sockaddr *)(&client_address), &address_size); // step 4 ssize_t readlen = read(client_fd, cm, 200); printf("Client sent: %s\n", client_message); printf("Connection made from address %s, fd %d\n", inet_ntoa(client_address.sin_addr), client_fd); close(client_fd); } int main(int argc, char ** argv){ struct sockaddr_in sad; sad.sin_port = htons(5143); 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; } if(argc > 1 && !strcmp(argv[1], "admin")){ puts("This is an administration only feature"); puts("The secret password is gerbil"); } printf("We can't print this twice!\n"); while(1){ listen_for_client(skt); } return 0; }