• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: About sockets...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: About sockets...


  • Subject: Re: About sockets...
  • From: email@hidden
  • Date: Mon, 27 May 2002 10:11:32 -0700

It is not a problem with SmallSockets.

The server isn't sending you any data until it hangs up on you, which is why that's all you see.

You have several problems with your code.

1) you send LF/CR instead of CR/LF
2) you don't have any line terminator when you send the USER command.
3) you don't ever respond to the data that you read... such as sending PONG back after receiving PING.

Here's a bit of hacking I did on your code to get you a little further along...

create a new cocoa application, and stay out of main.m! Edit MainMenu.nib and drop a TextView and a TextField in the main window. create a subclass of NSWindowController, call it IRCController, instantiate it... add an outlet called output, link it to the text view. create an action called send, attach the textfields action to that. then put the following code in IRCController.m/h

All I got from the server you had in your example, was a 30 second delay then an auth failure, but you might have more luck from there. I changed it to a local irc server and it worked just fine.


#import <Cocoa/Cocoa.h>
#import "Socket.h"

@interface Socket (ExposeFD)
- (int)socketfd;
@end
@implementation Socket (ExposeFD)
- (int)socketfd { return socketfd; }
@end

@interface IRCController : NSWindowController
{
IBOutlet NSTextView *output;
Socket *socket;
NSFileHandle *handle;
}
@end
@implementation IRCController

- (void)appendOutput:(NSString *)s color:(NSColor *)color
{
int len = [[output textStorage] length];
[output replaceCharactersInRange:NSMakeRange(len, 0) withString:s];
[output setTextColor:color range:NSMakeRange(len, [s length])];
[output scrollRangeToVisible:NSMakeRange([[output textStorage] length], 0)];
}

- (void)sendString:(NSString *)str
{
id s = [NSString stringWithFormat:@"%@\r\n", str];
[socket writeString:s];
[self appendOutput:s color:[NSColor blackColor]];
}

- (IBAction)send:(id)sender
{
[self sendString:[sender stringValue]];
[sender setStringValue:@""];
}

- (void)parseString:(NSString *)s
{
[self appendOutput:s color:[NSColor blueColor]];

// HERE is where you would parse the responses from the server and send the correct replies...
}

- (void)dataFromServer:(id)notification
{
NSData *data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];
int n = [data length];
if (n > 0) {
id stringData = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];

[self parseString:stringData];

[[notification object] readInBackgroundAndNotifyForModes:[NSArray arrayWithObjects:NSEventTrackingRunLoopMode, NSModalPanelRunLoopMode, NSDefaultRunLoopMode, nil]];
} else {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self appendOutput:@"server hung up" color:[NSColor redColor]];
}
}

- (void)awakeFromNib
{
socket = [[Socket alloc] init];
NS_DURING
[socket connectToHostName:@"irc.edisontel.it" port:6667];
NS_HANDLER
[self appendOutput:@"failed to connect to server" color:[NSColor redColor]];
return;
NS_ENDHANDLER
handle = [[NSFileHandle alloc] initWithFileDescriptor:[socket socketfd]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataFromServer:) name:NSFileHandleReadCompletionNotification object:handle];
[handle readInBackgroundAndNotifyForModes:[NSArray arrayWithObjects:NSEventTrackingRunLoopMode, NSModalPanelRunLoopMode, NSDefaultRunLoopMode, nil]];
[self sendString:@"NICK cx"];
[self sendString:@"USER dani . . :caio"];
}

@end


On Monday, May 27, 2002, at 07:12 AM, malcom wrote:

Hi again...

Heheh I think that I will send many message to the list before to make a
good client...first of all a big thanx to all people that want to help me in
this list.

Now my problem is with Small Sockets (or OmniNetwork)...

I receive a ping timedout message from the software... All seems right...
But... I receive all the data only when the other side is disconnected!

2002-05-27 16:09:31.836 clIRC[370] data: ERROR :Closing Link:
cx[email@hidden] (Ping timeout)

It's strange... I would to receive every single line of data when It's
really sended to me, and not at the end of connection... I think that there
is some thread problems with the sockets... Some time ago I have readed from
this list a similar message...but I can't found it now.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: About sockets...(part III)
      • From: malcom <email@hidden>
References: 
 >About sockets... (From: malcom <email@hidden>)

  • Prev by Date: Re: Text extracts, .webloc & NSTextView
  • Next by Date: Re: not having a new document be created on launch
  • Previous by thread: About sockets...
  • Next by thread: Re: About sockets...(part III)
  • Index(es):
    • Date
    • Thread