Small Sockets Help
Small Sockets Help
- Subject: Small Sockets Help
- From: Sam Goldman <email@hidden>
- Date: Sun, 21 Oct 2001 16:54:44 -0700
As you might remember, I was having some trouble getting a client/server app
to work. I decided to use small sockets because it sounded like it would
work for me. I have done alright in using it, but am having some problems.
My biggest problem is that when I set up my server, it doesn't accept any
connections. Here's the code I use for setting it up:
#import "ChatServer.h"
#import "BufferedSocket.h"
#import "MutableDataBufferExtension.h"
@implementation ChatServer
- (void)awakeFromNib
{
socket = [[BufferedSocket alloc] init];
[socket listenOnPort:4908];
[NSThread detachNewThreadSelector:@selector(threadListen:) toTarget:self
withObject:nil];
}
- (void)threadListen:(id)dummy
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
NS_DURING
while(1)
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
BufferedSocket *listenSocket;
listenSocket = [socket appectConnectionAndKeepListening];
[NSThread detachNewThreadSelector:@selector(threadCommunicate:)
toTarget:self withObject:listenSocket];
[myPool release];
}
NS_HANDLER
NSLog([NSString stringWithFormat: @"Exception in threadListen :
%@",[localException reason]]);
NS_ENDHANDLER
[myPool release];
}
- (void)threadCommunicate:(BufferedSocket *)sock;
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
NSMutableData *message;
NSString *strMsg;
NS_DURING
while(1)
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
message = [sock readData];
strMsg = [NSString stringWith
Data:message];
[myPool release];
}
NS_HANDLER
NSLog([NSString stringWithFormat: @"Exception in threadCommunicate:
%@",[localException reason]]);
NS_ENDHANDLER
[myPool release];
}
@end
Here's the code I use for connecting to it:
#import "Controller.h"
#import "BufferedSocket.h"
@implementation Controller
- (void)awakeFromNib
{
socket = [[BufferedSocket alloc] init];
[socket connectToHostName:@"localhost" port:4908];
}
- (IBAction)send:(id)sender
{
NSString *message = [messageTextView string];
NS_DURING
[socket writeString:message];
NS_HANDLER
NSLog([NSString stringWithFormat: @"Exception in send :
%@",[localException reason]]);
NS_ENDHANDLER
}
@end
The above code is practically copied and pasted from the BufferedSocket
example which works beautifully. What happens is that the server doesn't
accept the connection and the app actually fails with code 255.
Here's the output:
2001-10-21 16:51:12.270 CocoaChat[1021] An uncaught exception was raised
2001-10-21 16:51:12.284 CocoaChat[1021] Socket: Connect failed: Connection
refused
2001-10-21 16:51:12.297 CocoaChat[1021] *** Uncaught exception: <Socket:
Connect failed> Socket: Connect failed: Connection refused
CocoaChat.app has exited with status 255.
If anyone is familiar with Small Sockets, I would appreciate any help. Also,
I know that Tom Waters is working on an app using it (from this list) and
I'm pretty interested in that (seeing how I looked forward to FileView).