Do I need multithreading for this?

Discussion in 'Mac Programming' started by Soulstorm, Mar 7, 2008.

  1. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #1
    As few of you may have noticed, I want to build an IRC chat program just for the sake of it. I want it very simple.

    So far, I have managed to establish a connection into an IRC server, and accept the first messages. However, I did that using a command line. Now I want to use a GUI with it. So I made a Cocoa program, and I have a connected a button from the Cocoa program into a function that does what the main() function from my other program does.

    GeneralHandler.h
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    
    #import <Cocoa/Cocoa.h>
    #define PORT 6667 // the port client will be connecting to 
    
    #define MAXDATASIZE 2000 // max number of bytes we can get at once 
    
    
    @interface GeneralHandler : NSObject {
    	int sockfd;
    	struct hostent *he;
    	struct sockaddr_in their_addr;
    	char buf[MAXDATASIZE];
    	
    	IBOutlet NSTextView* textView;
    	IBOutlet NSTextField* userCommandTextField;
    }
    
    
    - (IBAction)send:(id)sender;
    - (IBAction)connect:(id)sender;
    @end
    
    generalHandler.m
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    
    #import <Cocoa/Cocoa.h>
    #define PORT 6667 // the port client will be connecting to 
    
    #define MAXDATASIZE 2000 // max number of bytes we can get at once 
    
    
    @interface GeneralHandler : NSObject {
    	int sockfd;
    	struct hostent *he;
    	struct sockaddr_in their_addr;
    	char buf[MAXDATASIZE];
    	
    	IBOutlet NSTextView* textView;
    	IBOutlet NSTextField* userCommandTextField;
    }
    
    
    - (IBAction)send:(id)sender;
    - (IBAction)connect:(id)sender;
    @end
    
    When I press the "connect" button, the program establishes a connection. However, the program freezes as it is doing that! Then it's entering in the loop where the server is waiting for my message, and the program never unfreezes. The only thing that works is the console, where it displays the messages I receive from the server. The GUI part of the program is frozen as if it's waiting for something (shows spining ball).

    I suspect it's because until the connection is terminated, I can't do anything else.

    How can I avoid that? I believe I must look into multithreading, but I don't know where to look.
     
  2. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #2
    Look at NSThread, the threading capabilities have been massively expanded in 10.5, but they still work in 10.4
     
  3. macrumors 65816

    Joined:
    Jul 28, 2004
    #3
    The other option is to use non-blocking IO.
     
  4. macrumors regular

    x704

    Joined:
    Apr 15, 2006
    #4
    You might check out poll()... see
    PHP:
    man poll 
    Eventually I'll use it for handling more than one server connections; you could use it to check to see if the connection has anything waiting to happen, if not then update the window and check again.

    Basically poll() looks for a change in state. So if you have a file with two programs writing to it, the programs can use poll() to see if anything has changed and update accordingly. poll() also works on TCP connections... check it out.
     
  5. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #5

Share This Page