Re: how to use: struct sockaddr
Re: how to use: struct sockaddr
- Subject: Re: how to use: struct sockaddr
- From: Jeff Heyob <email@hidden>
- Date: Thu, 16 Dec 2004 23:53:27 -0500
Message: 5
Date: Thu, 16 Dec 2004 08:17:30 -0700
From: Ryan M Joseph <email@hidden>
Subject: how to use: struct sockaddr
To: email@hidden
Message-ID: <email@hidden>
Content-Type: text/plain; charset="us-ascii"
I have a simple question that has me very confused. I'm trying to use
CFSocket to set up a client/server over the internet but I can not
understand how the sockaddr struct can be used to describe and IP
address. Is is not intended to do this? do I need to use some other
form of address then an IP? Or is there a function I can not find which
maybe builds the struct for you based off an IP string? Thanks for your
help.
Hi Ryan,
I've worked with 'sockaddr' and 'sockaddr_in' and settled on the latter.
The 'struct sockaddr_in' is found in <netinet/in.h>
I use it in the following code to create a socket to listen for
incoming connection requests.
Incoming connection requests are accepted within the ServerCallBackProc.
Notice that the 'struct sockaddr_in' is used to create an address in
the form of a CFDataRef that is passed to CFSocketSetAddress.
bool Network::OpenListener(UInt16 listenPort)
{
CFSocketRef socket; // this variable should be in class declaration
for persistence.
CFRunLoopSourceRef runLoopRef; // this variable should be in class
declaration for persistence.
CFOptionFlags flags = kCFSocketAcceptCallBack;
CFSocketContext context = {0,this,nil,nil,nil};
CFSocketError err = kCFSocketError;
CFDataRef address;
UInt16 yes = 1;
struct sockaddr_in addrs = {8,AF_INET,listenPort,{0}};
socket =
CFSocketCreate(nil,PF_INET,SOCK_STREAM,IPPROTO_TCP,flags,ServerCallBackP
roc,&context);
if(socket != nil)
{
RetainCt(this);
setsockopt(CFSocketGetNative(socket),SOL_SOCKET,SO_REUSEADDR,&yes,sizeof
(yes));
runLoopRef = CFSocketCreateRunLoopSource(nil, socket, 0);
if(runLoopRef)
{
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopRef,
kCFRunLoopCommonModes);
address = CFDataCreateWithBytesNoCopy(nil,(UInt8*)&addrs,
sizeof(addrs), kCFAllocatorNull);
if(address)
{
err = CFSocketSetAddress(socket, address);
CFRelease(address);
if(err == kCFSocketSuccess)
{
return true;
}
}
}
}
return false;
}
Best Regards,
Jeff
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden