Hi

I am 'trying' to get my head around multi-threaded programs. I have a working program (single thread) that uses a socket to listen on a particular port and exchange info with a client(s). The basic program structure is this (most declarations and error-checking removed for clarity):

...
/* create a streaming socket */
simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

int sockOp = setsockopt(simpleSocket, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
 	
 /* retrieve the port number for listening */
 simplePort = atoi(argv[1]);
 	
 /* setup the address structure */
 /* user INADDR_ANY to bind all local addresses */
 bzero(&simpleServer, sizeof(simpleServer));
 simpleServer.sin_family = AF_INET;
 simpleServer.sin_addr.s_addr = htonl(INADDR_ANY);
 simpleServer.sin_port = htons(simplePort);
 	
 /* bind to the address and port with our socket */
 returnStatus = bind(simpleSocket, (struct sockaddr *)&simpleServer, sizeof(simpleServer));

/* tell the socket we are ready to accept connections */
returnStatus = listen(simpleSocket, 5);

/* ACCEPT */
while (1) {
    /* block on accept function call */
    simpleChildSocket = accept(simpleSocket, (struct sockaddr *)&clientName, (socklen_t*)&clientNameLength);

    /* handle the new connection request */
    /* receive remote polling location ID from the client */
    int rcvBytes;
    unsigned char rcvBuffer[16600];
    rcvBytes = read(simpleChildSocket, rcvBuffer, sizeof(rcvBuffer));

    .... //do some more stuff

    /* close socket */
   close(simpleChildSocket);
} //while
...

So I am trying to adapt this but it doesn't seem to listen in a loop anymore. I am basing my multi-threaded structure on examples in the book "The Definitive Guide to Linux Network Programming" which has the following structure:

...
void* thread_proc(void *arg);
...
pthread_t thread_id;

/* create a streaming socket */
simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

int sockOp = setsockopt(simpleSocket, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
 	
 /* retrieve the port number for listening */
 simplePort = atoi(argv[1]);
 	
 /* setup the address structure */
 /* user INADDR_ANY to bind all local addresses */
 bzero(&simpleServer, sizeof(simpleServer));
 simpleServer.sin_family = AF_INET;
 simpleServer.sin_addr.s_addr = htonl(INADDR_ANY);
 simpleServer.sin_port = htons(simplePort);
 	
 /* bind to the address and port with our socket */
 returnStatus = bind(simpleSocket, (struct sockaddr *)&simpleServer, sizeof(simpleServer));

/* tell the socket we are ready to accept connections */
returnStatus = listen(simpleSocket, 5);

int result;

/* ACCEPT */
while (1) {
    simpleChildSocket = accept(simpleSocket,NULL, NULL);
    result = pthread_create(&thread_id, NULL, thread_proc, (void *) simpleChildSocket);

    pthread_detach(thread_id);
    sched_yield();

} //while

void* thread_proc(void *arg) {
  int sock = (int) arg;


    /* handle the new connection request */
    /* receive remote polling location ID from the client */
    int rcvBytes;
    unsigned char rcvBuffer[16600];
    rcvBytes = read(sock, rcvBuffer, sizeof(rcvBuffer));

    ....

    /* close socket */
   close(simpleChildSocket);
} //thread_proc

I was trying to get is to work with one thread first, then add additional threads to make troubleshooting easier. If it won't work with one thread, it certainly won't work with two. Instead of finishing a loop and waiting for another connection, it just seems to hang and not accept any more connections.

What should the structure look like? I.e. When to create the thread, etc. so that after one client connects it continues to wait for and accept subsequent connections.

Thanks for looking.

Regards,
Scott

Is it possible to even have 2 or more threads running concurrently on the same port in a multi-threaded socket program?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.