PDA

View Full Version : Do I need multithreading for this?




Soulstorm
Mar 7, 2008, 04:37 AM
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
#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
#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.



Eraserhead
Mar 7, 2008, 05:48 AM
Look at NSThread, the threading capabilities have been massively expanded in 10.5, but they still work in 10.4

Amdahl
Mar 7, 2008, 11:17 AM
The other option is to use non-blocking IO.

x704
Mar 7, 2008, 07:40 PM
You might check out poll()... see
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.

Catfish_Man
Mar 7, 2008, 09:45 PM
Check out http://cocoadev.com/index.pl?SocketClasses for some prebuilt nonblocking socket classes.