unable to create pthreads for chat ap

This is a discussion on unable to create pthreads for chat ap within the Networking/Device Communication forums, part of the General Programming Boards category; hello, i have built a basic chat application, which at this point accepts a connection from a user, but once ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    9

    unable to create pthreads for chat ap

    hello,
    i have built a basic chat application, which at this point accepts a connection from a user, but once the user is connected the pthreads are not created properly and exits the client program

    the error message shown is '1Ex'

    here is the client code, i have highlighted where the problem is
    Code:
    void *threadWrite(void *arg)
    {
    	struct threadWriteArgs* toWrite = (struct threadWriteArgs*) arg;
    	int sock = toWrite->sock;
    	int n = toWrite->n;
    	struct sockaddr_in server = toWrite->server;
    	struct hostent *hp = toWrite->hp;
    	int serverPort = toWrite->serverPort;
    	size_t length;char buffer[256];
    
    	while(1)
    		{
    		bcopy((char *)hp->h_addr, (char *)&server.sin_addr, hp->h_length);
       		server.sin_port = serverPort;
       		length=sizeof(struct sockaddr_in);
       		printf("Please enter the message: ");
       		bzero(buffer,256);
       		fgets(buffer,255,stdin);
       		n=sendto(sock,buffer, strlen(buffer), 0, &server, length);
      			if (n < 0) error("Sendto");
    		}
    	pthread_exit((void*) 27);
    }
    
    void *threadWait(void *arg)
    {
    	struct threadWaitArgs* toWait = (struct threadWaitArgs*) arg;
    	int sock = toWait->sock;
    	int n = toWait->n;
    	struct sockaddr_in from=toWait->from;
    	size_t length = sizeof(struct sockaddr_in);
    	char buffer[256];
    
    	while(1)
    		{
    		n = recvfrom(sock,buffer,256,0,&from, &length);
       		if (n < 0) error("recvfrom");
      			// write(1,"Got an ack: ",12);
       			write(1,buffer,n);
    		}
    }
    
    void join(int sock,int n,struct sockaddr_in server, struct sockaddr_in from , struct hostent *hp, int serverPort )
    {
    	size_t length;
    	char buffer[256];
    	bcopy((char *)hp->h_addr, (char *)&server.sin_addr, hp->h_length);
       	server.sin_port = serverPort;
       	length = sizeof(struct sockaddr_in);
       	printf("Please enter join plus your nickname: ");
       	bzero(buffer, 256);
       	fgets(buffer, 255, stdin);
       	n = sendto(sock, buffer, strlen(buffer), 0, &server, length);
    
       	if (n < 0) error("Sendto");
       		n = recvfrom(sock, buffer, 256, 0, &from, &length);
       	if (n < 0) error("recvfrom");
      		 write(1, buffer, n);
    
    //the write message is printed out before the user is kicked off
    
    	pthread_t waitingThread;
    	pthread_t writingThread;
    
    	threadWaitArgs toWait;
    		toWait.sock = sock;
    		toWait.n =n;
    		toWait.from = from;
    
    	threadWriteArgs toWrite;
    	toWrite.sock = sock;
    	toWrite.n = n;
    	toWrite.server = server;
    	toWrite.from=from;
    	toWrite.hp = hp;
    	toWrite.serverPort = serverPort;
    
    	int waitingResult = pthread_create(
    		&waitingThread,
    		NULL,
    		&threadWait,
    		(void*) &toWait);
    
    	int writingResult = pthread_create(
    		&writingThread,
    		NULL,
    		&threadWrite,
    		(void*) &toWrite);
    
    	if(waitingResult !=0)
    	{
    		printf("Could not create waiting thread\n");
    		exit(1);
    	}
    
    	if(writingResult !=0)
    	{
    		printf("Could not create writing thread\n");
    		exit(1);
    	}
    }
    the code compiles, so i am not sure why the threads arent being created.

    also, i should note that 'join' is the first method to be called

    many thanks,
    Fiona
    Last edited by Fillis52; 11-01-2010 at 11:55 AM. Reason: formatting

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    SourceForge.net: Indentation - cpwiki
    Your code is presently too unreadable to spend too much time on it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    That's not a standard socket error message that I've ever seen. From where is it originating?

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    68

    while loops

    Well, looking at your code for 20 seconds, I noticed that you have while loops that will never exit.

    Do your threads get created?

    or do you get the "Could not create writing thread" ?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > threadWaitArgs toWait;
    This is a local variable.
    It goes out of scope as soon as the function exits.

    > pthread_create(...&toWait);
    This has a pointer to that local variable.
    By the time it gets to look at it, it's probably gone.

    If you're still at the stage where normal sequential programming and debugging is still quite hard, or you have to think about it a lot, then thread programming really isn't for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    this is the first exercise in a C programming course i am on, and they have so far taught us nothing but the basics so i am on a steep learning curve.

    as for the while loops, they are meant to never end as the threads need to continually be waiting for a message from other chat clients or waiting to send a message.

    I understand threadWaitArgs toWait and threadWriteArgs toWrite are local variables, but as they are created in the same function in which they are called, i thought they would still exist. Unless the point exists at the time of creating the thread, then it is lost when trying to be passed into the thread function.

    The messages to be displayed when a thread is not able to be created are not shown, so i assume they do get created.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Well yes, they exist at the point the threads are created.
    But they will NOT exist for the lifetime of the threads.

    How many times is join() likely to be called?
    If it's just once, then making the thread data variables static would work.
    If not, then the best way out is to malloc them, and pass the pointer to the created thread.

    Also, since you're new at this, are threads an absolute part of the question?
    Because there are other ways of doing this which are not so mind-numbingly complex as threads.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    join() is called once in the main method, and not at all by any other methods. so I will make the variables static and see the effect.

    also, it was hinted at to use threads, which is why that was the path i chose

    thanks for everyone's help by the way, i really appriciate it

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    ok, when changed to
    Code:
    static struct threadWaitArgs{
    	int sock;
    	int n;
    	struct sockaddr_in from;
    }threadWaitArgs;
    
    static struct threadWriteArgs{
    	int sock;
    	int n;
    	struct sockaddr_in server;
    	struct sockaddr_in from;
    	struct hostent *hp;
    	int serverPort;
    }threadWriteArgs;
    get compiler errors as follows:

    cc1: warnings being treated as errors
    client_udp.c: In function 'join':
    client_udp.c:147: warning: statement with no effect
    client_udp.c:147: error: expected ';' before 'toWait'
    client_udp.c:148: error: 'toWait' undeclared (first use in this function)
    client_udp.c:148: error: (Each undeclared identifier is reported only once
    client_udp.c:148: error: for each function it appears in.)
    client_udp.c:152: warning: statement with no effect
    client_udp.c:152: error: expected ';' before 'toWrite'
    client_udp.c:153: error: 'toWrite' undeclared (first use in this function)

    but when change to
    Code:
    static threadWaitArgs toWait;
    	toWait.sock = sock;
    	toWait.n =n;
    	toWait.from = from;
    
    static threadWriteArgs toWrite;
    	toWrite.sock = sock;
    	toWrite.n = n;
    	toWrite.server = server;
    	toWrite.from=from;
    	toWrite.hp = hp;
    	toWrite.serverPort = serverPort;
    get error from debugger as follows:
    13[New Thread 0xb7ffbb90 (LWP 11643)]
    [New Thread 0xb75fab90 (LWP 11644)]
    [Thread 0xb75fab90 (LWP 11644) exited]
    [Thread 0xb7ffbb90 (LWP 11643) exited]

  10. #10
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    68

    exiting

    You should make sure you have code to wait on those threads to complete -- I didnt see that. This would cause your program to exit
    This is merely psudo code to explain why your program might be exiting
    Code:
    int main(){
        thread a, b;
        createp_thread(a);
        createp_thread(b);
    return 0;

    Do you see how your program will exit? creating the threads is not enough, you need to wait for the threads to complete, then exit the program. Otherwise, in the case above, your program will create the threads, and then exit.

    I forget the function to wait for the thread to complete, and since you have your threads runnning for ever, they never will. So, try this

    Code:
    volitile bool arunning, b running;
    int main(){
        thread a, b;
      arunning=brunning=false;
        if(createp_thread(a) == 0) arunning =true;
        if(createp_thread(b)== 0) brunning =true;
    while(arunning && brunning ) {}// this will prevent your program from exiting if the threads are created properly. 
    return 0;
    Then your threads need to set the booleans to false to make the program exit. Now, this is by far the worst method, but you have to start somewhere :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threads other than pthreads?
    By dayalsoap in forum C Programming
    Replies: 10
    Last Post: 05-28-2010, 01:56 AM
  2. Unable to create a simple function
    By garg29om in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 04:17 PM
  3. "mi_cmd_var_create: unable to create variable object"
    By jiapei100 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2010, 03:24 AM
  4. Pthreads problem
    By maluyk in forum C Programming
    Replies: 4
    Last Post: 04-27-2007, 09:33 AM
  5. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21