Results 1 to 4 of 4
I want a TCP server program using posix thread that can accept request from multiple clients simultaneously. I think we should array of threads but not getting the way of ...
- 02-27-2008 #1Just Joined!
- Join Date
- Dec 2007
- Posts
- 3
TCP Server using posix thread
I want a TCP server program using posix thread that can accept request from multiple clients simultaneously. I think we should array of threads but not getting the way of using it and i m getting some problems in my code ...............................
.........
listen(sd,5);
i=-1;
while(1) {
printf("%s: waiting for data on port TCP %u\n",argv[0],SERVER_PORT);
cliLen = sizeof(cliAddr);
newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen);
if(newSd<0) {
perror("cannot accept connection ");
return ERROR;
}
i++;
p = pthread_create(&thread[i],NULL,text,(char *)argv[0]);
printf("%d %d",i,i);
}
/* while (1) */
close(sd);
}
void *text(char *arg)
{
/* init line */
memset(line,0x0,MAX_MSG);
printf("%d",getpid());
/* receive segments */
while(read_line(newSd,line)!=ERROR) {
printf("%s: received from %s:TCP%d : %s\n", arg,
inet_ntoa(cliAddr.sin_addr),
ntohs(cliAddr.sin_port), line);
/* init line */
memset(line,0x0,MAX_MSG);
printf("%d",getpid());
} /* while(read_line) */
........
it closes the earlier connection when new request from client comes ..........
plz help ....
its urgent .....
thanx ..............
- 02-27-2008 #2
There are probably several problems with this, but the most obvious one is that all your threads are going to use the same location for variable newSd.
The traditional way of handling this is to fork() when a connection has been accept()ed. You can coordinate (synchronize) between various processes, and share data between them, in several ways.
Using POSIX threads for this is advisable only if there are severe performance constraints. The main reason for this is that debugging can be a real pain; one thread can stomp on unauthorized data, and the bug will first appear in an entirely different thread in an unrelocated portion of the code.--
Bill
Old age and treachery will overcome youth and skill.
- 02-27-2008 #3Just Joined!
- Join Date
- Dec 2007
- Posts
- 3
actually we are doing a college project and our guide insisted us to do this assignment using posix thread ..........
we have already implemented the program using fork successfully ...
thanx for ur reply .......
any more advice will be good .......
- 02-27-2008 #4
If your instructor has told you that you must do this with POSIX threads, then this assignment will be a baptism by fire. He's a good man, your instructor. Or woman. Whatever.
If you don't yet have the Addison-Wesley book Programming with POSIX Threads by David R. Butenhof, I suggest you get it. It's far superior to the copy I have of the O'Reilly book on the same subject.
Then take a weekend and read pretty much the whole thing from cover to cover. This will give you the ammunition you need to use POSIX threads with confidence and accuracy.
And, since it's a classroom assignment and rules prohibit us from helping you with classroom assignments, I'm outta here!
Good luck. (You won't need it if you read the book and work hard!)--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote