Fix network protocol
jiep opened this issue · 1 comments
jiep commented
Fix network protocol
jiep commented
Use this code to broadcast round 3 and 4.
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>
#include <sys/wait.h>
#define PORT 5555
#define hostNameLength 50
#define messageLength 256
#define N 5
typedef struct client_info {
char message[messageLength];
int socket;
} client_info;
void initSocketAddress(struct sockaddr_in *name,
char *hostName,
unsigned short int port){
struct hostent *hostInfo;
name->sin_family = AF_INET;
name->sin_port = htons(port);
hostInfo = gethostbyname(hostName);
if(hostInfo == NULL) {
fprintf(stderr, "initSocketAddress - Unknown host %s\n",hostName);
exit(EXIT_FAILURE);
}
name->sin_addr = *(struct in_addr *)hostInfo->h_addr;
}
void * sendMessage(void * params) {
client_info* client_inf = (client_info*) params;
int sockfd = client_inf->socket;
printf("Sending '%s' to socket %d\n", client_inf->message, sockfd);
int nOfBytes = 0;
nOfBytes = write(sockfd, client_inf->message, strlen(client_inf->message) + 1);
if (nOfBytes < 0) {
perror("writeMessage - Could not write data\n");
} else {
printf("Sent %d bytes to socket %d!\n", nOfBytes, sockfd);
}
}
void broadcast(int* sock, int n, char* message) {
pthread_t t_id[n];
printf("Creating threads...\n");
for (int i = 0; i < n; i++) {
struct client_info* client_inf = malloc(sizeof(struct client_info));
client_inf->socket = sock[i];
memcpy(client_inf->message, message, strlen(message) + 1);
int read = pthread_create(&t_id[i], NULL, sendMessage, (void *) client_inf);
if (read < 0) {
printf("ERROR: Return Code from pthread_create() is %d\n", read);
exit(1);
} else {
printf("Thread %d created successfully!\n", i);
}
}
for (int i = 0; i < n; i++) {
pthread_join(t_id[i], NULL);
}
}
int create_client(char* hostname, int port) {
struct sockaddr_in servername;
int sock;
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock < 0) {
perror("Could not create a socket\n");
exit(EXIT_FAILURE);
}
initSocketAddress(&servername, hostname, port);
if(connect(sock, (struct sockaddr *)&servername, sizeof(servername)) < 0) {
printf("Could not connect to server %d\n", port);
exit(EXIT_FAILURE);
} else {
printf("Connected to server: %d\n", port);
}
return sock;
}
void * run_client(char* hostname, int* ports, int n, char* message) {
printf("Init client...\n");
int sock[n];
for (int i = 0; i < n; i++) {
printf("Creating client %d\n", i+1);
sock[i] = create_client(hostname, ports[i]);
}
broadcast(sock, n, (void*) message);
// return NULL;
}
int main(int argc, char *argv[]) {
int sock[N];
int PORTS[N] = {5555, 5556, 5557, 5558, 5559};
struct sockaddr_in serverName;
char hostName[hostNameLength];
char messageString[messageLength];
char buffer[1024];
if(argv[1] == NULL) {
perror("Usage: client [host name]\n");
exit(EXIT_FAILURE);
} else {
strncpy(hostName, argv[1], hostNameLength);
hostName[hostNameLength - 1] = '\0';
}
if (fork() == 0){
char MESSAGE[messageLength] = "Mensaje de prueba";
run_client(hostName, PORTS, N, MESSAGE);
int sock[N];
for (int i = 0; i < N; i++) {
printf("Creating client %d\n", i+1);
sock[i] = create_client(hostName, PORTS[i]);
}
broadcast(sock, N, NULL);
exit(0);
} else {
printf("Hello from parent!\n");
wait(NULL);
}
return 0;
}