socket programming using threads


 
Thread Tools Search this Thread
Special Forums IP Networking socket programming using threads
# 1  
Old 02-25-2011
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
# 2  
Old 02-25-2011
Without seeing your code there is not much we can do to help you solve your problem.
# 3  
Old 02-25-2011
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
# 4  
Old 02-25-2011
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".
# 5  
Old 02-28-2011
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Need Help in Programming using Threads.

Hi , i am very new to threads.. need help to build the following program. Please if possible provide me the program. The program should be in C language working in Linux operating systems. Q) Execute a program using any thread library to create the number of threads specified by the user; each... (2 Replies)
Discussion started by: rajeshgauns
2 Replies

2. UNIX for Advanced & Expert Users

socket programming

can we send udp message to a destination ip address .. without having an ip address configured in our machine using recvfrom ? (2 Replies)
Discussion started by: Gopi Krishna P
2 Replies

3. UNIX for Advanced & Expert Users

Passing socket struct between kernel threads - module programming

I write kernel module with kernel threads using linux/kthread.h on 2.6.* kernel I tried to passing data between two kernel threads with data argument of kthread_run( fun, data , NAME ); but this is not work I dont know why. I tried many possibility and nothing works. So I thought that... (0 Replies)
Discussion started by: marcintom
0 Replies

4. Programming

Socket programming in C

Hi, i know how to use socket for TCP applications, i also know how to use RAW socket, but i would like to use socket just over IP and bellow TCP, do you know how to do that ? i don't want to reimplement IP :) thanks for your answer. (2 Replies)
Discussion started by: nameless`
2 Replies

5. UNIX for Advanced & Expert Users

socket programming

Hi, I am trying to connect to more than 60 servers to perform some actions remotely. for this I am using ssh2. But its taking lot of time. Though i am running the command in background, still its taking long time to execute.. Any one let me know can we use sockets instead of ssh2 for... (3 Replies)
Discussion started by: pvamsikr
3 Replies

6. IP Networking

socket programming

Hello Everyone Iam working on tcp/ip programming.with some time interval server has to send data.client has to close the connection and to open the connection between the time interval.this is the scenario when iam closing the connection in client side the connection terminates.how to... (1 Reply)
Discussion started by: sureshvaikuntam
1 Replies

7. Programming

Socket programming

Hello!:) I'm trying to do some socket programming based on the following situation: I have a directory service named Casino that will hold all the information regarding the gamers that will try to connect to it in order to play a game(for example (Blackjack).Once they make the login they are... (4 Replies)
Discussion started by: maracumbigo
4 Replies

8. IP Networking

socket programming

my system is a stand alone system... i want to try doing socket porgramming..ihave heard that this is usually done during testing... how can i do that....? (6 Replies)
Discussion started by: damn_bkb
6 Replies

9. Programming

Socket Programming socket

Hello, I actually try to make client-server program. I'm using SCO OpenServer Release 5.0.0 and when I try to compile my code (by TELNET) I've got this error : I'm just using this simple code : and I get the same error if I use : If someone can help me, Thanks (2 Replies)
Discussion started by: soshell
2 Replies

10. Programming

Socket Programming

Dear Reader, Is there any way to check up socket status other than 'netstatus ' Thanks in advance, (1 Reply)
Discussion started by: joseph_shibu
1 Replies
Login or Register to Ask a Question