site_archiver@lists.apple.com Delivered-To: macnetworkprog@lists.apple.com Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=LKCI0XT/dACdI2BXyRzBegzsbGl2uGDHI40V1fCjywmw1xtLYzHoQyGN+shA8yNuRhKrm7osQndaFcLcbS7YaU/nn3b5IqVppKJyHAGFJfoTOP+MyMlGQluGRiHkZNNbe8QVrS5CzpfscuVyYDevk2huBsETWK8LGkoxyK5qQBY= Hi Here i am giving the source code sever and client using has been created using CFSocket ====================== Server code ========================== struct sockaddr_in a = {0, AF_INET, DataSet.TCPportNum, 0}; CFDataRef d = CFDataCreate(NULL, (UInt8 *)&a, sizeof(struct sockaddr_in)); CFSocketSignature signature = {AF_INET, SOCK_STREAM, 0, d}; CFSocketRef socket = CFSocketCreateWithSocketSignature(NULL, &signature, kCFSocketAcceptCallBack, acceptConnection, NULL); char *c = CFDataGetBytePtr(d); //printf("\n\n******************* %s *********************\n\n", c); CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL, socket, 0); CFRunLoopRef loop = CFRunLoopGetCurrent(); CFRunLoopAddSource(loop, source, kCFRunLoopDefaultMode); CFRunLoopRun(); CFRelease(source); printf("\nOUT OF ReceiveSendMessage_TCP()\n"); return NULL; } // This function is called when any data is received from client side void receiveData(CFSocketRef child, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) { char buffer0[1024]; //CFDataRef response; strcpy(buffer0,"Fortune Infotech"); CFDataRef response = CFDataCreate(NULL,buffer0,sizeof(buffer0)); CFDataRef newData; newData = (CFDataRef *) data; if ( CFDataGetLength(newData) == 0 ) { // A zero length data indicates the end of the data stream; the client is dead fprintf(stderr, "ClientGotData: No Data from client side.\n" ); // destroy this particular client // free up any resources available with this client }// end if datalength==0 else { char * byteptr= CFDataGetBytePtr (newData); char buffer3[100]; strcpy(buffer3, byteptr); strcat(DataSet.MESSAGE, buffer3); strcat(DataSet.MESSAGE, DataSet.ipstr); CFStringRef buff3 = CFStringCreateWithCStringNoCopy(NULL, DataSet.MESSAGE, kCFStringEncodingMacRoman, NULL); const ControlID outputmsg = {'SERV', 102}; GetControlByID(DataSet.window, &outputmsg, &DataSet.outputTxtRef); SetControlData(DataSet.outputTxtRef, kControlEntireControl, kControlEditTextCFStringTag, sizeof(CFStringRef), &buff3); printf("\nlength of newData =%d\n",CFDataGetLength(newData)); printf("\nbyteptr=%s\n",byteptr); }// end else CFSocketSendData(child, NULL, response, 0.0); CFRelease(response); //CFSocketInvalidate(child); //CFRelease(child); printf("\nOUT OF receiveData()\n"); } /*ClientInformationReceived(ClientInfo *pClientInfo) { printf("ClientName=%s",pClientInfo->MachineName); } */ int sendMessageToClient(int clientNo,char * buffer,int buffsize)//int clientNo,char * buffer,int buffsize) { //senddata(client[clientno].cfsocket CFDataRef response = CFDataCreate(NULL,buffer,buffsize); int ret_val=CFSocketSendData(CFChildSocket[clientNo], NULL, response, 0.0); //int ret_val=CFSocketSendData(CFChildSocket, NULL, response, 0.0); return ret_val; } int sendCommandToClient(int clientNo,char * buffer,int buffsize) { printf("\nEntered in sendCommandToClient"); CFDataRef response = CFDataCreate(NULL,buffer,buffsize); int ret_val=CFSocketSendData(CFChildSocket[clientNo], NULL, response, 0.0); return ret_val; } void acceptConnection(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) { printf("\nENTERED IN acceptConnection()\n"); CFSocketContext context; int *clientnum; clientnum=calloc(1,sizeof(int)); *clientnum=gcount; memset(&context, 0, sizeof(context)); context.info = clientnum; //client[gcount].child //CFSocketRef child CFChildSocket[gcount]= CFSocketCreateWithNative(NULL, *(CFSocketNativeHandle *)data, kCFSocketDataCallBack , receiveData, &context); CFRunLoopSourceRef childSource = CFSocketCreateRunLoopSource(NULL, CFChildSocket[gcount], 0); //CFRunLoopSourceRef childSource = CFSocketCreateRunLoopSource(NULL, CFChildSocket, 0); CFRunLoopRef loop = CFRunLoopGetCurrent(); CFRunLoopAddSource(loop, childSource, kCFRunLoopDefaultMode); CFRelease(childSource); gcount++;// increament the child socket counter printf("\nOUT OF acceptConnetcion()\n"); } ================== source code of client========================== CFDataRef tcp_address; CFSocketError socket_error; CFRunLoopSourceRef socket_runloop; //int length ,portnum; struct sockaddr_in tcpip_sa; //printf("enter the portnumber"); //scanf("%d",&portnum); //portnum=9107; // /*tcpip_socket = CFSocketCreate(NULL, AF_INET, SOCK_STREAM, 0, kCFSocketAcceptCallBack,&acceptconnection, NULL);*/ int tcpip_socket=socket(AF_INET,SOCK_STREAM,0); if (tcpip_socket<0) { displayErr(CFSTR("Opening Socket")); } //struct hostent *serverTcp; tcpip_sa.sin_family = AF_INET; tcpip_sa.sin_addr.s_addr = INADDR_ANY; tcpip_sa.sin_port =DataSetClient.TCPportNum;// htons(atoi(argv[1])); serverTcp = gethostbyname(ipstr); if (serverTcp == NULL) { displayErr(CFSTR("Error : No such host."));//fprintf(stderr,"ERROR, no such host\n"); exit(0); } bcopy((char *)serverTcp->h_addr, (char *)&tcpip_sa.sin_addr.s_addr, serverTcp->h_length); memset (&(tcpip_sa.sin_zero), '\0', 8); //bzero(&tcpip_sa,sizeof(tcpip_sa)); tcpip_sa.sin_len = sizeof(struct sockaddr_in); length = sizeof(struct sockaddr_in); if (connect(tcpip_socket,(struct sockaddr *)&tcpip_sa,sizeof(tcpip_sa)) < 0) displayErr(CFSTR("Error Connecting"));//error("ERROR connecting"); CFSocketRef child = CFSocketCreateWithNative(NULL, (CFSocketNativeHandle )tcpip_socket, kCFSocketDataCallBack, receiveData, NULL); int n5 = write(tcpip_socket,"hello from client",17); CFRunLoopSourceRef childSource = CFSocketCreateRunLoopSource(NULL, child, 0); CFRunLoopRef loop = CFRunLoopGetCurrent(); printf("Accept Client request\n"); CFRunLoopAddSource(loop, childSource, kCFRunLoopDefaultMode); CFRelease(childSource); return 0; } //============================== CFSocket Creation ========================================= CFDataRef tcp_address; CFSocketError socket_error; CFRunLoopSourceRef socket_runloop; //int length ,portnum; struct sockaddr_in tcpip_sa; //printf("enter the portnumber"); //scanf("%d",&portnum); //portnum=9107; // /*tcpip_socket = CFSocketCreate(NULL, AF_INET, SOCK_STREAM, 0, kCFSocketAcceptCallBack,&acceptconnection, NULL);*/ int tcpip_socket=socket(AF_INET,SOCK_STREAM,0); if (tcpip_socket<0) { displayErr(CFSTR("Opening Socket")); } //struct hostent *serverTcp; tcpip_sa.sin_family = AF_INET; tcpip_sa.sin_addr.s_addr = INADDR_ANY; tcpip_sa.sin_port =DataSetClient.TCPportNum;// htons(atoi(argv[1])); serverTcp = gethostbyname(ipstr); if (serverTcp == NULL) { displayErr(CFSTR("Error : No such host."));//fprintf(stderr,"ERROR, no such host\n"); exit(0); } bcopy((char *)serverTcp->h_addr, (char *)&tcpip_sa.sin_addr.s_addr, serverTcp->h_length); memset (&(tcpip_sa.sin_zero), '\0', 8); //bzero(&tcpip_sa,sizeof(tcpip_sa)); tcpip_sa.sin_len = sizeof(struct sockaddr_in); length = sizeof(struct sockaddr_in); if (connect(tcpip_socket,(struct sockaddr *)&tcpip_sa,sizeof(tcpip_sa)) < 0) displayErr(CFSTR("Error Connecting"));//error("ERROR connecting"); CFSocketRef child = CFSocketCreateWithNative(NULL, (CFSocketNativeHandle )tcpip_socket, kCFSocketDataCallBack, receiveData, NULL); int n5 = write(tcpip_socket,"hello from client",17); CFRunLoopSourceRef childSource = CFSocketCreateRunLoopSource(NULL, child, 0); CFRunLoopRef loop = CFRunLoopGetCurrent(); printf("Accept Client request\n"); CFRunLoopAddSource(loop, childSource, kCFRunLoopDefaultMode); CFRelease(childSource); return 0; } void receiveData(CFSocketRef child, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) { char* datarecv; CFDataRef newData; newData = (CFDataRef) data; if ( CFDataGetLength(newData) == 0 ) { // A zero length data indicates the end of the data stream; the client is dead fprintf(stderr, "ClientGotData: No Data from client side.\n" ); fprintf(stderr,"Connection broken\n"); // destroy this particular client // free up any resources available with this client }// end if datalength==0 else { datarecv=(char *)CFDataGetBytePtr(newData); printf("data received\n"); printf("%s\n",(char *)datarecv); }// end else //CFRelease(child); }// end of function _______________________________________________ Do not post admin requests to the list. They will be ignored. Macnetworkprog mailing list (Macnetworkprog@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/macnetworkprog/site_archiver%40lists.... This email sent to site_archiver@lists.apple.com
participants (1)
-
sunil prajapati