我一直在尝试通过 TCP 套接字发送一个二进制文件,我的文件长度是 5Mo 我收到的只有 1Mo,收到的文件包含很多丢失的部分,我不明白为什么,我试图拆分二进制文件进入 1Mo 的 5 部分,我发送了第一部分(1 Mo),收到的文件只有 255!请问有人可以解释为什么吗?有没有一种有效的方法来发送这种类型的文件?
这是我正在使用的代码,它包含三个部分:浏览二进制文件/打开套接字以发送文件/发送文件:
private void senduimage_Click(object sender, EventArgs e)
{
string inpath = "C:\\Documents and Settings\\Bureau\\uImage";
FileStream fs = new FileStream(inpath, FileMode.Open, FileAccess.Read);
if (!user.clientSocket_NewSocket.Connected)
{
Socket clientSocket_NewSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
user.clientSocket_NewSocket = clientSocket_NewSocket;
try
{
System.IAsyncResult _NewSocket = user.clientSocket_NewSocket.BeginConnect(ip_address, NewSocket.Transceiver_TCP_Port, null, null);
bool successNewSocket = _NewSocket.AsyncWaitHandle.WaitOne(2000, true);
}
byte[] buff = null;
BinaryReader br = new BinaryReader(fs);
string inpath = "C:\\Documents and Settings\\Bureau\\uImage"
long numbytes = new FileInfo(inpath).Length;
buff = br.ReadBytes((int)numbytes);
user.clientSocket_NewSocket.Send(buff);
}
}
欢迎任何帮助,谢谢:)
我已经尝试过您向我提出的发送到文件,但它给了我一个例外,说这个操作不能用未连接的套接字完成,这是我使用的代码:
01 private void senduimage_Click(object sender, EventArgs e)
02 {
03
06
07 if (!user.clientSocket_NewSocket.Connected)
08 {
09
10 Socket clientSocket_NewSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
11 user.clientSocket_NewSocket = clientSocket_NewSocket;
12
13 }
14
15 System.IAsyncResult _NewSocket = user.clientSocket_NewSocket.BeginConnect(ip_address, NewSocket.Transceiver_TCP_Port, null, null);
16 bool successNewSocket = _NewSocket.AsyncWaitHandle.WaitOne(2000, true);
17 if (successNewSocket)
18 {
19 string fileName = "C:\\Documents and Settings\\Bureau\\uImage";
20 user.clientSocket_NewSocket.SendFile(fileName);
21 user.clientSocket_NewSocket.Shutdown(SocketShutdown.Both);
22 user.clientSocket_NewSocket.Close();
23 }
请问有什么建议吗?