|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help regarding pthread and socket
Dear all,
I am beginner in "pthreads and sockets".Problem i am facing is i am not able to "accept" any connection at the server when i use the "pthread library". The socket descriptor "accept" returns is -1. Please let me know if there is any solution to the above problem. rgds, rajat |
|
#2
|
|||
|
|||
|
you'll have to show some code
|
|
#3
|
|||
|
|||
|
/**server code***/
#include<pthread.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<string.h> #include<sys/ipc.h> #include<sys/sem.h> #define READY 1 #define NOTREADY 0 #define MAX_THREAD 5 #define MAX_BUFFER 100 #define MAX_NAME_SIZE 100 #define MAX_CLIENTS 5 #define REGISTER 1 #define DATA 2 #define PORT 3000 #define _REENTRANT int main() { int sockfd,fd,thread_no=0; pthread_t thread[MAX_THREAD],tell_thread; struct sockaddr_in sockcli,sockser; int slot_no=0,socklen; int flag=READY; sockser.sin_family=PF_INET; sockser.sin_port=PORT; sockser.sin_addr.s_addr=INADDR_ANY; sockfd=socket(PF_INET,SOCK_STREAM,0); if(bind(sockfd,(struct sockaddr *)&sockser,sizeof(sockser))<0) { perror("Bind Failed\n"); exit(0); } listen(sockfd,5); pthread_mutex_init(&mutex_ob,NULL); while(1)/** Accept fails here when linked with pthread library**/ { fd=accept(sockfd,(struct sockaddr *)&sockcli,&socklen); printf("Socket fd=%d\n",fd); if(slot_no!=-1) { printf("i am here\n"); if(write(fd,&flag,sizeof(flag))<0) perror("write failed"); pthread_create(&thread[thread_no++],NULL,rec_data,(void *)&socket_fd[slot_no]); } else { flag=NOTREADY; write(fd,&flag,sizeof(flag)); } } } |
|
#4
|
|||
|
|||
|
|
|
#5
|
|||
|
|||
|
write system call seems ok. The above program works pecfectly on a standalone PC loaded with linux. But in a network the "accept function" fails.
Could any one tell me why this may happen..? |
|
#6
|
|||
|
|||
|
I could find a solution to the above problem... But i can provide a reason for why it works..... I made a change in the accept function
by calling "accept(fd,0,0);"... Now it gives a socket descriptor.. |
![]() |
| Viewing: Dev Shed Forums > Other > Dev Shed Lounge > Help regarding pthread and socket |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|