| | |
Linux multi-threaded socket program steps/structure
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2009
Posts: 22
Reputation:
Solved Threads: 1
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):
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:
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
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):
c++ Syntax (Toggle Plain Text)
... /* 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:
c++ Syntax (Toggle Plain Text)
... 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
![]() |
Similar Threads
- Memory Allocation Error, writing past end of heap! (C++)
- Share: Best 101 Free Computer Software For Your Daily Use (Network Security)
- C++ Performance Tips (C++)
- Multi Thread Help (Java)
- Socket program unit testing (C++)
- Race condition in multi threaded application (Java)
- Using OpenGL in Visual C++: Part I (Game Development)
- sparc assembler under linux (Assembly)
- Linux or just my incompitence? (IT Professionals' Lounge)
Other Threads in the C++ Forum
- Previous Thread: invalid use of nonstatic member
- Next Thread: problems flushing input stream
Views: 996 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





