[英]c# socket send details
im trying to send file over a socket,but i would like also to send the file's name for example. 我试图通过套接字发送文件,但是我也想发送文件名。
this is the send code: 这是发送代码:
int count = 0;
int size;
private int SendVarData(Socket s, byte[] data)
{
total = 0;
int size = data.Length;
int dataleft = size;
int sent;
datasize = BitConverter.GetBytes(size);
sent = s.Send(datasize);
sent = s.Send(data, total, dataleft, SocketFlags.None);
total += sent;
dataleft -= sent;
// MessageBox.Show("D");
return total;
}
this is the call 这是电话
byte[] arr = File.ReadAllBytes(file);
SendVarData(sc, arr);//sc is a socket ofcourse.
i would like to send also the file name as i said. 我也想像我说的那样发送文件名。 should i send it once before the file send itself?
我应该在文件发送之前发送一次吗? or there's another efficient way to send both?
或者还有另一种有效的发送方式?
this is the receive code: 这是接收代码:
private byte[] ReceiveVarData(Socket s)
{
int total = 0;
int recv;
byte[] datasize = new byte[4];
recv = s.Receive(datasize, 0, 4, 0);
size = BitConverter.ToInt32(datasize, 0);
int dataleft = size;
//MessageBox.Show(size.ToString());
byte[] data = new byte[size];
while (total < size)
{
recv = s.Receive(data, total, dataleft, 0);
if (recv == 0)
{
break;
}
total += recv;
dataleft -= recv;
}
return data;
}
any help would be apperciated. 任何帮助将不胜感激。
I don't think it matters whether you send it before or after the file. 我认为无论在文件发送之前还是之后发送都无关紧要。 You need a protocol, eg, first send length of file name, then file name, then length of file and file itself, or other way around.
您需要一个协议,例如,首先发送文件名的长度,然后发送文件名,然后发送文件和文件本身的长度,或者以其他方式发送。 On the receiving side you can differentiate the messages now-because you know length of each chunk (assuming the two length variables are always 4 byte integers say).
在接收方,您现在就可以区分消息了,因为您知道每个块的长度(假设两个长度变量总是4个字节整数)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.