大佬教程收集整理的这篇文章主要介绍了将文件从 C# WPF 传输到 C 客户端,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将文本文件从基于 wpf 的 C# 服务器传递到我制作的 C 客户端。
出于某种原因,在 C 部分,当我尝试写入文件时,它说
一个无效参数被传递给一个认为无效参数致命的函数。
在 fwrite
函数中。
这可能是什么原因造成的?
我检查了发送的文件大小,没问题,双方打印的大小相同,从 C# 端它说已全部发送,但在 C 端写入文件失败。
用于传输的 C# 代码:
public static voID Sendfile(Socket clIEntSocket,String fileName)
{
String folder = @"C:\Users\Roy\source\repos\GUI243\GUI243\";
String fullPath = folder + filename;
fileInfo fi = new fileInfo(fullPath);
long file_size = fi.Length;
byte[] preBuffer;
using (var memoryStream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(memoryStream))
{
writer.Write(file_sizE);
messageBox.Show(file_size.ToString());
}
preBuffer = memoryStream.ToArray();
}
clIEntSocket.Send(preBuffer); // sending size
using (BinaryReader reader = new BinaryReader(new fileStream(fullPath,fileMode.open)))
{
byte[] message = new Byte[4096];
int size_passed = 0;
do
{
reader.baseStream.Seek(size_passed,SeekOrigin.begin);
reader.Read(message,4096);
clIEntSocket.Send(messagE);
size_passed += 4096;
file_size -= message.Length;
} while (file_size > 0);
}
// clIEntSocket.Sendfile(fullPath,preBuffer,null,TransmitfiLeoptionS.UseDefaultWorkerThread);
messageBox.Show("file has been sent!");
}
}
C 客户端:
/*
============================================
General : function is responsible for receiving a length of data from the clIEnt
Parameters : sock - clIEnt socket to receive the data from
*buf - holds a pointer to the buffer that needs to update
bufsize - the length of the buffer
Return Value : returns TRUE when the data is read correctly
else,falSE when there was a socket error or no bytes are received.
============================================
*/
bool recv_raw(SOCKET sock,void* buf,int bufsizE)
{
unsigned char* pbuf = (unsigned char*)buf;
while (bufsize > 0) {
int num = recv(sock,pbuf,bufsize,0);
if (num <= 0) { return false; }
pbuf += num;
bufsize -= num;
}
return true;
}
/*
===================================================
General : receives the length of the file and updates it
Parameters : sock - clIEnt socket to receive the data from
*filesize - holds a pointer to the size of the buffer that needs to update
filesize_len - the length of the file size pointer
Return Value : returns TRUE when the size is read correctly
else,falSE when there was a socket error or no bytes are received.
===================================================
*/
bool recv_file_len(SOCKET sock,long* filesizE)
{
if (!recv_raw(sock,filesize,sizeof(*filesizE))) { return false; }
return true;
}
/*
===================================================
General : writes to the lua file the data from the file
that was received in the socket
Parameters : sock - the socket between the clIEnt and server
*f - the file to write the data received to
Return Value : returns TRUE when everything was written to the file.
returns falSE if there's no data received or detected a socket problem.
===================================================
*/
bool receive_and_write_file(SOCKET sock,file *f)
{
long filesize;//size of address
if (!recv_file_len(sock,&filesizE)) { return false; }
printf("file size (From C# clIEnt) : %ld\n",filesizE);
if (filesize > 0)
{
char buffer[BUFFER_SIZE];
do {
int num = min(filesize,BUFFER_SIZE);
if (!recv_raw(sock,buffer,num)) {
return false;
}
int offset = 0;
do
{
size_t written = fwrite(&buffer[offset],1,num - offset,f); // the Fatal error
if (written < 1) { return false; }
offset += written;
} while (offset < num);
filesize -= num;
} while (filesize > 0);
}
return true;
}
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的将文件从 C# WPF 传输到 C 客户端全部内容,希望文章能够帮你解决将文件从 C# WPF 传输到 C 客户端所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。