It looks like you're asking for the IP address from a socket before
you've given it an IP. As someone said earlier, you have to bind an IP
address to a socket - it doesn't just come out of thin air.
Luke
On Aug 4, 2009, at 11:07 AM, James Lin wrote:
I am not using CFHost...
I use the CFSocketCopyAddress() on a CFSocketRef (my server socket)
called TCPServer.
CFDataRef serverAddressData = CFSocketCopyAddress(TCPServer);
NSString *serverAddressString;
serverAddressString = [NSString stringWithFormat: @"%@", [self
addressHost:serverAddressData]];
NSLog(@"Server started at %@", serverAddressString);
and the addressHost function to return the ip address as follows:
- (NSString *) addressHost: (CFDataRef)cfaddr
{
if (cfaddr == NULL) return nil;
char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
struct sockaddr *pSockAddr = (struct sockaddr *) CFDataGetBytePtr
(cfaddr);
struct sockaddr_in *pSockAddrV4 = (struct sockaddr_in *) pSockAddr;
struct sockaddr_in6 *pSockAddrV6 = (struct sockaddr_in6 *)pSockAddr;
const void *pAddr = (pSockAddr->sa_family == AF_INET) ?
(void *)(&(pSockAddrV4->sin_addr)) :
(void *)(&(pSockAddrV6->sin6_addr));
const char *pStr = inet_ntop (pSockAddr->sa_family, pAddr, addrBuf,
sizeof(addrBuf));
if (pStr == NULL) [NSException raise:
NSInternalInconsistencyException
format: @"Cannot
convert address to string."];
return [NSString stringWithCString:pStr
encoding:NSASCIIStringEncoding];
}
The NSLog result is : Server started at 0.0.0.0
Any ideas?
Thanx in advance...
James
On 2009/8/5, at 上午 1:52, Luke the Hiesterman wrote:
Have you tried using CFHost to get your IP?
Luke
On Aug 4, 2009, at 10:49 AM, James Lin wrote:
I am trying to make the iPhone a server and a client at the same
time...
What I am trying to accomplish...
1. iPhone running my application opens a "server" socket and
listens for incoming network connection from another iPhone
running the same application.
2. The server socket has an "ip address" that i can register with
my php/mysql server.
3. Another iPhone running my same app acts as the client gets the
iPhone server's ip address from the server and make connection to
the server iPhone.
4. The client iPhone sends a string "hello, I am James" to the
server iPhone and the server iPhone reply with the user's choice
of either "Hi, Nice to meet you" or "Get lost!" strings.
ps. the port number i chose was 2048
This is all i am trying to accomplish, but i am making slow
progress and banging my head against the wall...
Any ideas/suggestions are greatly appreciated...
Thanx again...
James
On 2009/8/4, at 下午 11:50, Dave Camp wrote:
On Aug 4, 2009, at 5:43 AM, James Lin wrote:
Hi all,
Does anyone know the limitation of the iPhone simulator when it
comes to networking?
Is it crippled on the simulator?
I've tried two seperate ways of opening up a "server" socket.
By "server" socket, do you mean you are trying to connect to a
server somewhere, or that you are trying to open a low numbered
(i.e. < 1024) port locally so that you can be a server?
The simulator cannot open low numbered ports for inbound
connections but iPhones running OS 3.0 and later can.
Dave
_______________________________________________
Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/luketheh
%40apple.com
This email sent to luket...@apple.com
_______________________________________________
Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to arch...@mail-archive.com