Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-10-2009, 07:48 PM   #1 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Default 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 handleNewConnectionFromAddresseer inputStreamNSInputStream *)readStream outputStreamNSOutputStream *)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!
freshking is offline   Reply With Quote
Old 09-11-2009, 05:24 AM   #2 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Default

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?
freshking is offline   Reply With Quote
Old 09-11-2009, 12:08 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,558
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

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.
smithdale87 is offline   Reply With Quote
Reply

Bookmarks

Tags
connection, identifier, network, read, wifi

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» 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
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:07 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0