create UDP "listener" with pthread

This is a discussion on create UDP "listener" with pthread within the C Programming forums, part of the General Programming Boards category; hello, i'm trying to write a UDP client for a basic "chat" service, basically i want the client to receive ...

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

    create UDP "listener" with pthread

    hello,
    i'm trying to write a UDP client for a basic "chat" service, basically i want the client to receive and print messages sent to it from the server, however still allow the user to enter text and send messages to other "rooms'. i create a thread with pthread_create() and i can receive one message but it seems as though the thread never returns. heres a simplified main()

    Code:
    int main(int argc, char* argv[])
    {
    	/*
    	 *server setup and client initialisation
    	 */
    	pthread_create(&thrid, 0, receiveThread, &server);
    	
    	while(1) {
    		
    		if(typeFlag) {
    			printf("Enter text: ");
    			fgets(buf, MSG_SIZE, stdin);
    			//typeFlag = 0;
    		}
    		
    		/**
    		 *do a bunch of stuff based on incoming messages
    		 */
    	}
    	
    	pthread_join(thrid, NULL);
    	close(server.socket);
        return 0;
    }
    and the "listener" thread, basically receives and prints incoming messages
    Code:
    void* receiveThread(void* arg)
    {
    	char buf[MSG_SIZE];
    	Server* server = (Server*)arg;
    	receiveMessage(server, buf);
    	printf("buf: %s", buf);
    	return NULL;
    }
    so basically i want the thread to print the mesage and return and go back to the fgets() so user can input more commands etc, but still "automatically" print any mesage which comes to it. but currently it stops after the printf("buf") and receives no more messages until i press return. so if anyone could give me any pointers how i can acheive what i've (attempted) to describe it would be most appreciated,
    cheers,

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Perhaps you created a detached thread?
    pthread_attr_setdetachstate

    You need to make sure you have a JOINABLE thread. Perhaps use the attribute when you create 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Receive a TCP message on a UDP socket?
    By Yarin in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2009, 12:21 AM
  2. Using select() for client UDP
    By jazzman83 in forum C Programming
    Replies: 2
    Last Post: 04-03-2007, 05:31 AM
  3. Create new combo boxes based on items selected in preview combo box
    By RealityFusion in forum Windows Programming
    Replies: 2
    Last Post: 01-10-2007, 08:50 AM
  4. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  5. The Pthread Hell
    By matott in forum Linux Programming
    Replies: 1
    Last Post: 04-10-2005, 05:59 AM

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