sending file in rpc
-
2008年6月25日 4:10
Hi Frenz,
I have coded a program for sending file in rpc..... connection gets established and i could send a text file but .exe files couldnt be sent using that code... couldnt find whats the error.. i am new to this field plz help.... this program is basic for tis sem project... thanks in advance.. need your valuable suggestions!!!! if posible sample code...
-chithira
server code:
/* file: hellos.c */
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include<fstream.h>
#include<afx.h>
#include "..\hello.h"#define FNAME "c:\\mynotepad.exe"
//global variable
CFile destFile;
int fileLength;
int BUFFER_SIZE=1024;
//end
void main()
{
RPC_STATUS status;
unsigned char * pszProtocolSequence = (unsigned char *)"ncacn_np";
unsigned char * pszSecurity = NULL;
unsigned char * pszEndpoint = (unsigned char *)"file://pipe//hello";
unsigned int cMinCalls = 20;
unsigned int cMaxCalls = 50;
unsigned int fDontWait = FALSE;
status = RpcServerUseProtseqEp(pszProtocolSequence,
cMaxCalls,
pszEndpoint,
pszSecurity);
if (status)
{
exit(status);
}
status = RpcServerRegisterIf(hello_v1_0_s_ifspec,
NULL,
NULL);
if (status)
{
exit(status);
}
system ("CLS");
printf ("+---------------------------------------------------------+\n");
printf ("+ RPC Server +\n");
printf ("+---------------------------------------------------------+\n");status = RpcServerListen(cMinCalls,
cMaxCalls,
fDontWait);
if (status)
{
exit(status);
}
} // end main()
/******************************************************/
/* MIDL allocate and free */
/******************************************************/
void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
{
return(malloc(len));
}
void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
{
free(ptr);
}
void dest_open_file(int filesize)
{
fileLength = filesize;
}
int write_file(unsigned char * pszString,int partoffile)
{
CString psz;
destFile.Open(FNAME, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite| CFile::typeBinary);
destFile.SeekToEnd();
destFile.Write(pszString,partoffile);
destFile.Close();
return TRUE;}
void dest_close_file()
{
MessageBox(NULL,"File Received Successfully","Success",MB_SETFOREGROUND);
}
/* add this function to hellop.c */
void Shutdown(void)
{
RPC_STATUS status;
status = RpcMgmtStopServerListening(NULL);
if (status)
{
exit(status);
}
status = RpcServerUnregisterIf(NULL, NULL, FALSE);
if (status)
{
exit(status);
}
} //end Shutdown
client code :
/* file: helloc.c */
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include<conio.h>
#include<iostream.h>
#include <afx.h>
#include "..\hello.h"#define FNAME "C:\\notepad1.exe"
//Global variable
CFile fp,df;
unsigned char * pszString = NULL;
int fileLength ;
int BUFFER_SIZE=1024;
int m=1;
//end//Function used the send the contents.
void send_fileCont()
{
BOOL flag = TRUE;
int Left_Data;//variables for montioring
unsigned char* sendData = NULL;
Left_Data = fp.GetLength();
sendData = (unsigned char *) malloc(BUFFER_SIZE);int i=0;
do //loop for sending whole data
{
int sendThisTime, doneSoFar, buffOffset;
if (flag)
{
if(!(sendThisTime = fp.Read( sendData, BUFFER_SIZE )))//reading each part of file
{
MessageBox(NULL,"Error Reading File","Reading Error",MB_SETFOREGROUND);//Error reading file
exit(0);
}flag = FALSE;
}buffOffset = 0;
do//loop for sending each 512 bytes data
{
flag = write_file(sendData, sendThisTime);doneSoFar =BUFFER_SIZE; // test for errors and get out if they occurred
memset(sendData,0,BUFFER_SIZE);
buffOffset += doneSoFar;//incrementing buffer to point to next part of data
sendThisTime -= doneSoFar;//to point to next 512 bytes
Left_Data -= doneSoFar;//to find remaining data
}while ( sendThisTime > 0);
if((Left_Data < 512))//for reallocating BUFFER_SIZE
{
BUFFER_SIZE = Left_Data;
}i++;
}while ( Left_Data > 0 );//&& flag==1 );
free(sendData);
}
void open_file()
{
if(! (fp.Open(FNAME, CFile::modeRead | CFile::typeBinary)))
{
MessageBox(NULL,"Error Opening Source File","Open Error",MB_SETFOREGROUND);//Error handling for invaild file
exit(0);
}
fileLength = fp.GetLength();//getting file size
}void close_file()
{
fp.Close();
}void main()
{
RPC_STATUS status;
unsigned char * pszUuid = NULL;
unsigned char * pszProtocolSequence = (unsigned char*)"ncacn_np";
unsigned char * pszNetworkAddress = (unsigned char*)"192.168.45.122";
unsigned char * pszEndpoint = (unsigned char*)"\\pipe\\hello";
unsigned char * pszOptions = NULL;
unsigned char * pszStringBinding = NULL;
unsigned long ulCode;
status = RpcStringBindingCompose(pszUuid,
pszProtocolSequence,
pszNetworkAddress,
pszEndpoint,
pszOptions,
&pszStringBinding);
if (status)
{
exit(status);
}
status = RpcBindingFromStringBinding(pszStringBinding,
&hello_IfHandle);
if (status)
{
exit(status);
}
RpcTryExcept
{
system ("CLS");
printf ("+---------------------------------------------------------+\n");
printf ("+ RPC Client +\n");
printf ("+---------------------------------------------------------+\n");
open_file();//Open the file in server for writing.
dest_open_file(fileLength);send_fileCont();
//Close the server opened file.
dest_close_file();close_file();
Shutdown();
}
RpcExcept(1)
{
ulCode = RpcExceptionCode();
printf("Runtime reported exception 0x%lx = %ld\n", ulCode, ulCode);
}
RpcEndExcept
status = RpcStringFree(&pszStringBinding);
if (status)
{
exit(status);
}
status = RpcBindingFree(&hello_IfHandle);
if (status)
{
exit(status);
}
exit(0);
} // end main()/******************************************************/
/* MIDL allocate and free */
/******************************************************/
void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
{
return(malloc(len));
}
void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
{
free(ptr);
}
全部回复
-
2008年6月27日 14:36所有者:
Hi Frenz,
Unfortunately, this forum is dedicated to Windows HPC Server postings. You should use the MSDN forums and select an RPC, COM, or OS fourm instead. We regret that we don't have the time to respond to all postings outside of the primary topic and we do appreciate your interest in Microsoft platform technologies.
Phil
http://channel9.msdn.com/shows/the_hpc_show- 已标记为答案 Phil-PenningtonMicrosoft Employee, Owner 2008年6月27日 14:36