局域网内共享文件的复制问题?<用户权限相关>

play100 2008-11-11 09:45:17
目前情况:
1.用WNetAddConnection2建立网络连接
2.用ifstream/ofstream以二进制的方式读写文件

问题:
在当前用户与目标机器已建立一个网络连接(没有对目标文件读操作)的前提下,在程序又新建一个网络连接(有权限)时读写文件会出错。
请问应如何修改?或用其它什么方法来实现这种局域网内文件的复制?
...全文
268 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
play100 2008-11-11
  • 打赏
  • 举报
回复
SORRY再补充下我的问题:
不要用C/S方式实现、在本地安装一个程序就可实现的这种。
另一般情况下、已经实现程序中登陆目标机器并拷贝文件;上面说的是特殊情况下,与当前会话的权限冲突,请问如何解决或有其它更好的办法没?
laolaoliu2002 2008-11-11
  • 打赏
  • 举报
回复
// sockRecv.Send(data, myFileLength); //Send the whole thing now
for(int i=0;i<=myFileLength;i+=1024)
sockRecv.Send(data+i,1024); //Send the whole thing now
if(myFileLength%1024!=0)
sockRecv.Send(data+myFileLength-myFileLength%1024,myFileLength%1024);



// sockClient.Receive(data, dataLength); //Get the whole thing
for(int i=0;i<=dataLength;i+=1024)
sockClient.Receive(data+i,1024); //Send the whole thing now
if(dataLength%1024!=0)
sockClient.Receive(data+dataLength-dataLength%1024,dataLength%1024);
laolaoliu2002 2008-11-11
  • 打赏
  • 举报
回复
void SendFile()
{
#define PORT 34000 /// Select any free port you wish

AfxSocketInit(NULL);
CSocket sockSrvr;
sockSrvr.Create(PORT); // Creates our server socket
sockSrvr.Listen(); // Start listening for the client at PORT
CSocket sockRecv;
sockSrvr.Accept(sockRecv); // Use another CSocket to accept the connection


CFile myFile;
myFile.Open("C:\\ANYFILE.EXE", CFile::modeRead | CFile::typeBinary);

int myFileLength = myFile.GetLength(); // Going to send the correct File Size

sockRecv.Send(&myFileLength, 4); // 4 bytes long

byte* data = new byte[myFileLength];

myFile.Read(data, myFileLength);

sockRecv.Send(data, myFileLength); //Send the whole thing now

myFile.Close();
delete data;

sockRecv.Close();
}

void GetFile()
{
#define PORT 34000 /// Select any free port you wish

AfxSocketInit(NULL);
CSocket sockClient;
sockClient.Create();

// "127.0.0.1" is the IP to your server, same port
sockClient.Connect("127.0.0.1", PORT);

int dataLength;
sockClient.Receive(&dataLength, 4); //Now we get the File Size first

byte* data = new byte[dataLength];
sockClient.Receive(data, dataLength); //Get the whole thing

CFile destFile("C:\\temp\\ANYFILE.EXE",
CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);

destFile.Write(data, dataLength); // Write it

destFile.Close();

delete data;
sockClient.Close();
}
laolaoliu2002 2008-11-11
  • 打赏
  • 举报
回复
用下面的函数
BOOL CopyFile(
LPCTSTR lpExistingFileName, // name of an existing file
LPCTSTR lpNewFileName, // name of new file
BOOL bFailIfExists // operation if file exists
);
laolaoliu2002 2008-11-11
  • 打赏
  • 举报
回复
感觉好像是文件被打开后锁定了。
cnzdgs 2008-11-11
  • 打赏
  • 举报
回复
出错时,获取错误码来判断错误原因,然后等待一段时间后重试,或者提示用户操作失败及出错原因。

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧