Dev Shed Lounge
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOtherDev Shed Lounge

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 25th, 2003, 01:52 AM
rajatnaik rajatnaik is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 4 rajatnaik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old July 25th, 2003, 02:00 AM
ygfperson ygfperson is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 10 ygfperson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
you'll have to show some code

Reply With Quote
  #3  
Old July 25th, 2003, 02:28 AM
rajatnaik rajatnaik is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 4 rajatnaik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
/**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));

}
}
}

Reply With Quote
  #4  
Old July 25th, 2003, 05:48 PM
ygfperson ygfperson is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 10 ygfperson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
http://compsoc.dur.ac.uk/cgi-bin/man2html?write+2

You're using the write command wrongly.

Reply With Quote
  #5  
Old July 27th, 2003, 11:59 PM
rajatnaik rajatnaik is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 4 rajatnaik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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..?

Reply With Quote
  #6  
Old July 28th, 2003, 01:27 AM
rajatnaik rajatnaik is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 4 rajatnaik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile

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..

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherDev Shed Lounge > Help regarding pthread and socket


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway