Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
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.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
Look at NSThread, the threading capabilities have been massively expanded in 10.5, but they still work in 10.4
 

x704

macrumors regular
Apr 15, 2006
118
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.