socket programming using threads

Hi,
1)i wrote a program which acts lik a client gateway serving clients request and sending probe msg`s to server to chech if its active..
2)both moduels interact when run independently my problem is that when i tr to run them as two different threads in a single program(client_gateway prog to interact with client and sending probes to server) its not woking...

kindly help

Without seeing your code there is not much we can do to help you solve your problem.

its almost 200 lines of code...
i just wanted an idea how to run 2 diff threads parallely..
both are socket programs which interact with other systems..

regards
kulkarni

Until you post your code you're wasting your time. We can barely tell what you're even trying to do, let alone why it's not working. Reduce it to something you can post, or just post it as is.

Please also describe in more detail what "not working" means. I've heard that description used for anything from "file not found" to "the server's on fire".

void* send_probe(void *ptr)
{
int create_socket[20],*flag;
struct sockaddr_in address[30];
flag=(int *)malloc(sizeof(int));
struct NODE * temp=(struct NODE *)malloc(sizeof(struct NODE));
temp=head;
int i=0,n=0,a;

  while\(temp!=NULL\)
  \{
        n\+\+;
        temp=temp->next;
  \}
        
        temp=head;

        i=0;

  // 1st open all the sockets and connect to server thn with sock desc. so can send probes in loop
        while\(temp!=NULL\)
  \{     

        create\_socket[i]=socket\(AF\_INET,SOCK_STREAM,0\);
        
        address[i].sin\_family=AF_INET;
        address[i].sin_port=htons\(5000\+i\);
        inet\_pton\(AF\_INET,temp->ip,&address[i].sin_addr\);

        if\(connect\(create_socket[i],\(struct sockaddr *\)&address[i],sizeof\(address[i]\)\)==0\)
        printf\("The connection was accepted with server %s\\n",temp->ip\);

        i\+\+;
        temp=temp->next;
  \}

  


        for\(a=0;a<6;a\+\+\)        /*send few trial probes */
  \{
        printf\("waiting to send\\n"\);
        for\(i=0;i<n;i\+\+\)
        \{    *flag=i;
              printf\("flag value=%d\\n",*flag\);    
          send\(create_socket[i],\(char*\)flag,sizeof\(flag\),0\);
          printf\("probe %d sent\\n",i\);
        \}
        sleep\(0.001\);// send probes after every certain period
  \}

}

void * recv_probe_ack(void* ptr)
{

  int create\_socket[20],new_socket[20],addrlen[20],*flag=NULL;
  
  struct sockaddr_in address[20];
  flag=\(int *\)malloc\(sizeof\(int\)\);
  int i=0,n,a;
  struct NODE * temp=\(struct NODE *\)malloc\(sizeof\(struct NODE\)\);
  temp=head;
  flag=\(int *\)malloc\(sizeof\(int\)\);


  // first open and bind all the sockets from the linked list then with socket disc\`s can recv probes frm it in loop

  while\(temp!=NULL\)

{
if((create_socket [i]= socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("\nThe socket was created\n");

  //binding socket with the address
   address[i].sin_family = AF_INET;
  address[i].sin\_addr.s_addr = INADDR_ANY;
  address[i].sin_port = htons\(6000\+i\);

  if \(bind\(create_socket[i],\(struct sockaddr *\)&address[i],sizeof\(address[i]\)\) == 0\)
  printf\("Binding Socket\\n"\);

  // listen to the socket for requests
    listen\(create_socket[i],500\);
    addrlen [i]= sizeof\(struct sockaddr_in\);

  // accept from the queue of requests
    new_socket [i]= accept\(create_socket[i],\(struct sockaddr *\)&address[i],&addrlen[i]\);

     if \(new_socket [i]> 0\)
      printf\("The Client %s is Connected...\\n",
      inet\_ntoa\(address[i].sin_addr\)\);// network byte format to string 

  temp=temp->next;
  i\+\+;

}//while

  n=0;
  temp=head;
  
  // keep track of number of nodes in linked list. to open so many sockets
  while\(temp!=NULL\)
  \{
        temp=temp->next;
        n\+\+;
        
  \}
  


  for\(a=0;a<6;a\+\+\)        //while\(1\) this used to put infinte loop later 

{
i=0;

  while\(i<n\)
  \{

         recv\(new_socket[i],\(char *\)flag,sizeof\(flag\),0\);
        printf\("\\nflag=%d\\n",*flag\);
        i\+\+;
  \}

}// for

}// fun

void main()
{

  struct NODE * temp1=\(struct NODE *\)malloc\(sizeof\(struct NODE\)\);
  head=\(struct NODE *\)malloc\(sizeof\(struct NODE\)\);

  head->ip="127.0.0.1";
  head->port=5000;
  head->next=temp1;
  temp1->ip="127.0.0.1";
  temp1->port=5001;
  temp1->next=NULL;
  
 pthread_t thread1, thread2;
 char *message1 = "Thread 1";
 char *message2 = "Thread 2";
 int  iret1, iret2;

/* Create independent threads each of which will execute function */
  
 iret1 = pthread_create\( &thread1, NULL,send_probe, \(void*\) message1\);

 iret2 = pthread_create\( &thread2, NULL,recv\_probe_ack, \(void*\) message2\);

 pthread_join\( thread1, NULL\);
 pthread_join\( thread2, NULL\);

}

---------- Post updated at 01:15 AM ---------- Previous update was at 01:11 AM ----------

the above is jus one module which sends probes to server and receives ack`s...
the prob here is tht...
IN SEND_PROBE:

the 1st for loop goes well in all the means
in sec iteration the socket s opend n binded but halts and terminates