I have an application with multiple threads, one thread is creating 4 tcp connections, then this thread creates another thread that will handle the receive function for these 4 connections using poll or anything else, and the original thread (the first one) start sending messages to these 4 connections(round-robin). it's like below pseudo code,
main()
{
pthread_create( &thread1, NULL, sending_thread, (void*) args);
}
void *sending_thread( void *ptr )
{
int i=0;
int *connections;
connections = create_connections();
pthread_create( &thread1, NULL, receiving_thread, (void*)&connections);
while(1) {
send(connections[i], "test");
i++;
if (i > 3)
i=0;
}
}
void *receiving_thread( void *ptr )
{
char *msg;
msg = receive(connections[i]);
save_received_msg_to_disk(msg);
}
My question is How can I check my connections and bring up the disconnected one? for example, let's say connection1 went down, do I need to create another connection with the same fd, which is connection[1] in this case? or is there other ways to handle this case?
Environment is C/pthread in Linux