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
generalHandler.m
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.
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.