How Can I Add Support for Multiple Client Connections to my TCP Server?

  • Thread starter Thread starter James889
  • Start date Start date
  • Tags Tags
    Multiple
AI Thread Summary
The discussion focuses on implementing a TCP server that can handle multiple client connections using pthreads. The initial suggestion of calling the accept function in a separate thread is deemed inappropriate since accept is a blocking call. Instead, a more effective approach is proposed, where the main loop of the server listens for incoming connections and creates a new thread for each accepted connection using pthread_create. This method allows the server to handle multiple clients concurrently. Additionally, the option of using asynchronous non-blocking operations is mentioned as an alternative or complementary approach to multithreading for managing client connections.
James889
Messages
190
Reaction score
1
Hi,

I have a simple tcp server, to which i would like to add support for multiple client connections via pthreads. This is what i had in mind, calling accept in a separate thread.

I this similar to what you would do?

Code:
   .
   .
   .
int sock = socket(PF_INET,SOCK_STREAM,0);
int sock_fd[10];
struct sockaddr_in client_addr[10];
int size = sizeof(&client_addr);

void *Thread1(void *arg){


   for(i=0;i<9;i++){
  sock_fd[i] = accept(sock,(struct sockaddr *)&client_addr[i],size);

}
 
Technology news on Phys.org
No.

Normally accept is blocking until new incoming connection.
So, simplest way would be like this:

Code:
void* ConnectionThread(void* pv)
{
    int fd=(int)pv;
    //...
    close(fd);
    return 0;
}

void main_loop(unsigned short port)
{
    struct sockaddr_in loacal_addr;
    local_addr.sin_family=AF_INET;
    local_addr.sin_port=((port>>8)|(port<<8));
    local_addr.sin_addr.s_addr=INADDR_ANY;

    int lfd=socket(AF_INET,SOCK_STREAM,0);
    if(0!=bind(lfd,(struct sockaddr*)&local_addr,sizeof(local_addr)){
        perror("bind");
    };
    listen(lfd,5);
    while(true){
        struct sockaddr_in remote_addr;
        socklen_t slen=sizeof(remote_addr);
        int afd=accept(lfd,(struct sockaddr*)&remote_addr,&slen);
        if(afd==-1){
            perror("accept");
        }else{
            if(0!=pthread_create(0,0,ConnectionThread,(void*)afd)){
                close(afd);
            };
        };
    };
}
Also there is an option of using asynchronous non-blocking operations instead of/ or in mix with multithreading
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top