'Multithreaded socket programming : sharing variable
Hi I want to implement a ring of sockets .For example IP address 127.0.0.1 listens to 127.0.0.3 and this one listens to 127.0.0.2. They are supposed to share some information and pass it such as a number and each time a client receives the data, increments the number and passes it to the next one.
Example : at the beginning counter = 1
(127.0.0.1)--(send : counter ++)--> (127.0.0.2)
(127.0.0.2)--(send : counter ++)--> (127.0.0.3)
(127.0.0.3)--(send : counter ++)--> (127.0.0.1)
#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <semaphore.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#define MESSAGE_LENGTH 256ssssssssssssssssssssssssssssssssssssssssss
sem_t semaphore;
typedef struct{
int number;
char* message;
char* address;
}PC_info;
void* routine_server(void* args){
PC_info* arg = (PC_info*)args;
printf("[%d]\t:MyAddress is %s\t%s\n",arg->number,arg->address,arg->message);
}
void* routine_client(void* args){
PC_info* arg = (PC_info*)args;
printf("[%d]\t:I listen to %s\t%s\n",arg->number,arg->address,arg->message);
}
PC_info** allocate_threads_args(int n,const char* address[n],const char* message[n]){
PC_info** PCs_info = (PC_info**)malloc(n * sizeof(PC_info*));
if(PCs_info == NULL){
perror("SYSTEM : cannot allocate so many clients");
exit(0);
}
for(int i = 0;i < n;i++){
PCs_info[i] = (PC_info*)malloc(sizeof(PC_info));
PCs_info[i]->address = (char*)malloc(sizeof(char) * MESSAGE_LENGTH);
PCs_info[i]->message = (char*)malloc(sizeof(char) * MESSAGE_LENGTH);
memcpy(PCs_info[i]->address,address[i],sizeof(char) * MESSAGE_LENGTH);
memcpy(PCs_info[i]->message,message[i],sizeof(char) * MESSAGE_LENGTH);
PCs_info[i]->number= i+1;
}
return PCs_info;
}
int main(){
const char* address[3] ={"127.0.0.3",
"127.0.0.1",
"127.0.0.2"};
const char* address_client[3] ={"127.0.0.1",
"127.0.0.2",
"127.0.0.3"};
const char* message[3] = {"client 1...",
"client 2...",
"client 3..."};
int n = 3;
PC_info** server_info = allocate_threads_args(n,address,message);
PC_info** client_info = allocate_threads_args(n,address_client,message);
// for(int i = 0;i < n;i++){
// printf("[%d]\t:\t%s\t%s\n",clients[i]->number,clients[i]->address,clients[i]->message);
// }
pthread_t* thread = (pthread_t*)malloc(sizeof(pthread_t)* (n * 2));
for(int i = 0;i < n;i++){
if(pthread_create(&thread[i],NULL,&routine_server,(void*)server_info[i]) != 0){
perror("SYSTEM : cannot create thread\n");
exit(1);
}
if(pthread_create(&thread[n+i],NULL,&routine_client,(void*)client_info[i]) != 0){
perror("SYSTEM : cannot create thread\n");
exit(1);
}
}
for(int i = 0;i < 2 * n;i++){
if(pthread_join(thread[i],NULL) != 0){
perror("SYSTEM : cannot join thread\n");
exit(2);
}
}
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|