CFSocket
CFSocket
- Subject: CFSocket
- From: "sunil prajapati" <email@hidden>
- Date: Wed, 24 Oct 2007 20:01:31 +0530
Hi Guys,
I have written client and server application in Carbon using CFSocketCreateWithNative.
I m able to control multiple clients from my server.
Now I m facing the problem when any client get disconnected from my server,I unable to identify which client has disconnected.
My server side code is as follows:
struct CLIENTCONNETED
{
CFSocketRef CFChildSocket;
}clientconnected[100];
struct sockaddr_in tcpip_sa;
tcpip_socket=socket(AF_INET,SOCK_STREAM,0);
if (tcpip_socket<0)
{
printf("Error: Opening Socket\n");
}
tcpip_sa.sin_len = sizeof(struct sockaddr_in);
tcpip_sa.sin_family = AF_INET;
tcpip_sa.sin_addr.s_addr = htonl(INADDR_ANY);
tcpip_sa.sin_port = htons(DataSet.TCPportNum);
memset(tcpip_sa.sin_zero,0,sizeof(tcpip_sa.sin_zero));
bind(tcpip_socket, (struct sockaddr *) &tcpip_sa,sizeof(tcpip_sa));
if(bind <0)
{
printf("Error:Binding Sockets.\n");
}
listen(tcpip_socket,50);
CFSocketRef socket = CFSocketCreateWithNative(NULL,
(CFSocketNativeHandle )tcpip_socket,
kCFSocketAcceptCallBack,
acceptConnection,
NULL);
CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL, socket, 0);
CFRunLoopRef loop = CFRunLoopGetCurrent();
CFRunLoopAddSource(loop, source, kCFRunLoopDefaultMode);
CFRunLoopRun();
CFRelease(source);
void acceptConnection(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
CFSocketContext context;
int *clientnum;
clientnum=calloc(1,sizeof(int));
*clientnum=gcount;
memset(&context, 0, sizeof(context));
context.info
= clientnum;
clientconnected[gcount].CFChildSocket = CFSocketCreateWithNative(NULL,
*(CFSocketNativeHandle *)data,
kCFSocketDataCallBack ,
receiveData,
&context);
CFRunLoopSourceRef childSource = CFSocketCreateRunLoopSource(NULL, clientconnected[gcount].CFChildSocket, 0);
CFRunLoopRef loop = CFRunLoopGetCurrent();
CFRunLoopAddSource(loop, childSource, kCFRunLoopDefaultMode);
CFRelease(childSource);
}
void receiveData(CFSocketRef child, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
CFDataRef newData;
newData = (CFDataRef * )data;
if ( CFDataGetLength(newData) == 0 )
{
printf("receive data is null\n");
}
else
{
printf("Data received\n");
}
}
Please help me how to identify which client has been disconnected from my server?
Any helpful link or sample code very much helpful for me.
Thanks,
Sunil.
_______________________________________________
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