如何解决将文件从 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 举报,一经查实,本站将立刻删除。