// C includes #include #include #include #include #include #include #include #include #include #include // C++ includes #include #include using namespace std; struct message { uint8_t type; uint16_t length; char recipient_name[32]; char sender_name[32]; char *message; }__attribute__((packed)); int read_message(struct message* m, int fd){ if(67 != recv(fd, m, 67, MSG_WAITALL)) return 1; m->message = (char*)malloc(m->length); if(m->length != recv(fd, m->message, m->length, MSG_WAITALL)) return 2; return 0; } void send_message(struct message* m, int fd){ write(fd, m, 67); write(fd, m->message, m->length); } vector client_fds; /* Client should send a length of filename, then filename */ void handle_client(int client_fd, struct sockaddr_in client_address){ struct message m; client_fds.push_back(client_fd); while(1){ if(read_message(&m, client_fd)) break; printf("Received message from %s to %s (%d bytes): %s\n", m.sender_name, m.recipient_name, m.length, m.message); for(int cfd : client_fds) if(cfd != client_fd) send_message(&m, cfd); free(m.message); } printf("Closing connection with client %d\n", client_fd); // Remove from list of clients before we close for(int i = 0; i < client_fds.size(); i++){ if(client_fds[i] == client_fd){ client_fds.erase(client_fds.begin() + i); // Could this cause a race condition? } } 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; } while(1){ int client_fd; 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 thread client_thread(handle_client, client_fd, client_address); client_thread.detach(); } return 0; }