09-10-2009, 07:48 PM
#1 (permalink )
Registered Member
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
WiFi connection - reading name
Hi,
im exploring the
WiTap sample code and there is on thing i would like to do: I would like to read out the name of your network partner, once connect.
Here is a code snipped it think you can read it out from:
Quote:
- (void)handleNewConnectionFromAddress: (NSData *)addr inputStream: (NSInputStream *)istr outputStream: (NSOutputStream *)ostr {
// if the delegate implements the delegate method, call it
if (self.delegate && [self.delegate respondsToSelector: @selector(didAcceptConnectionForServer: inputStream: outputStream: )]) {
[self.delegate didAcceptConnectionForServer: self inputStream: istr outputStream: ostr];
}
}
Quote:
// This function is called by CFSocket when a new connection comes in.
// We gather some data here, and convert the function call to a method
// invocation on TCPServer.
static void TCPServerAcceptCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) {
TCPServer *server = (TCPServer *)info;
if (kCFSocketAcceptCallBack == type) {
// for an AcceptCallBack, the data parameter is a pointer to a CFSocketNativeHandle
CFSocketNativeHandle nativeSocketHandle = *(CFSocketNativeHandle *)data;
uint8_t name[SOCK_MAXADDRLEN];
socklen_t namelen = sizeof(name);
NSData *peer = nil;
if (0 == getpeername(nativeSocketHandle, (struct sockaddr *)name, &namelen)) {
peer = [NSData dataWithBytes:name length:namelen];
}
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
CFStreamCreatePairWithSocket(kCFAllocatorDefault, nativeSocketHandle, &readStream, &writeStream);
if (readStream && writeStream) {
CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
[server handleNewConnectionFromAddress eer inputStream NSInputStream *)readStream outputStream NSOutputStream *)writeStream];
} else {
// on any failure, need to destroy the CFSocketNativeHandle
// since we are not going to use it any more
close(nativeSocketHandle);
}
if (readStream) CFRelease(readStream);
if (writeStream) CFRelease(writeStream);
}
}
Would be great if somebody could help my figure out how to do that.
Thanks for taking your time!
09-11-2009, 05:24 AM
#2 (permalink )
Registered Member
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
I tried this:
Quote:
- (void)handleNewConnectionFromAddress: (NSData *)addr inputStream: (NSInputStream *)istr outputStream: (NSOutputStream *)ostr {
if (self.delegate && [self.delegate respondsToSelector: @selector(didAcceptConnectionForServer:inputStream : outputStream: )]) {
[self.delegate didAcceptConnectionForServer: self inputStream: istr outputStream: ostr];
NSString *output = [[NSString alloc] initWithData: addr encoding: NSUTF8StringEncoding];
NSLog(@"NAME: %@", output);
}
}
but im only getting this as a result:
Quote:
2009-09-11 11:20:27.877 Texter[2020:20b] NAME: (null)
Why is that?
09-11-2009, 12:08 PM
#3 (permalink )
Registered Member
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,558
According to the NSString class reference, this method
"Returns nil if the initialization fails for some reason (for example if data does not represent valid data for encoding)."
So you can check if your "addr" is nil. If not, then it more than likely can't be converted to UTF8.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 327
21 members and 306 guests
@sandris , ADY , dacapo , Dani77 , djohnson , dre , HDshot , HemiMG , JasonR , MarkC , mer10 , nibeck , prchn4christ , ryandb2 , spiderguy84 , themathminister , timle8n1 , tomtom100 , vogueestylee , vvenkatachallam
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,883
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, vvenkatachallam