问题描述
我尝试将文件从CLIENT发送到SERVER,但是即使到达的文件小于8KB或更大,到达的文件也总是计数为8KB?例如,"MYFile.exe"最大为15KB,但仅保存为8KB.任何想法,为什么它已损坏发送完整的文件,而即时消息我没有收到任何错误消息.下面是对文件传输很重要的代码?
提前谢谢!
客户:
I try to send a file from CLIENT to the SERVER, but the arrived file counts always 8KB even if it is smaller than 8KB or larger? ''MYFile.exe'' for example is 15KB great but it is saved as only 8KB. Any Ideas why it has broken to send the full file, while im not receiving any errormessages. Here below is the code which is important for file transfer?
Thanks in advance!
CLIENT:
Imports System.Net.Sockets
Imports System.Text
私人子客户()
试试
将tcpClient设为新的System.Net.Sockets.TcpClient()
tcpClient.Connect("127.0.0.1",8000)
Dim networkStream作为NetworkStream = TcpClient.GetStream()
昏暗的sendBytes为[Byte]()= IO.File.ReadAllByte("C:\ spl \ myFile.exe)
networkStream.Write(sendBytes,0,sendBytes.Length)
异常捕获
结束尝试
结束子
伺服器:
Private Sub Client()
Try
Dim tcpClient As New System.Net.Sockets.TcpClient()
tcpClient.Connect("127.0.0.1", 8000)
Dim networkStream As NetworkStream = TcpClient.GetStream()
Dim sendBytes As [Byte]() = IO.File.ReadAllByte("C:\spl\myFile.exe)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Catch ex As Exception
End Try
End Sub
SERVER:
Imports System.Net.Sockets
Imports System.Text
专用子服务器()
Const portNumber为整数= 8000
将tcpListener调暗为新的TcpListener(portNumber)
tcpListener.Start()
试试
将tcpClient变暗为TcpClient = TcpListener.AcceptTcpClient()
Dim networkStream作为NetworkStream = TcpClient.GetStream()
昏暗的字节(tcpClient.ReceiveBufferSize)作为字节
networkStream.Read(bytes,0,CInt(tcpClient.ReceiveBufferSize))
IO.File.WriteAllBytes("C:\ spl \ RECEIVEDFILE.exe",字节)
tcpClient.Close()
tcpListener.Stop()
异常捕获
结束尝试
End Sub
Private Sub Server()
Const portNumber As Integer = 8000
Dim tcpListener As New TcpListener(portNumber)
tcpListener.Start()
Try
Dim tcpClient As TcpClient = TcpListener.AcceptTcpClient()
Dim networkStream As NetworkStream = TcpClient.GetStream()
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
IO.File.WriteAllBytes("C:\spl\RECEIVEDFILE.exe", bytes)
tcpClient.Close()
tcpListener.Stop()
Catch ex As Exception
End Try
End Sub
推荐答案
这篇关于TCP服务器/客户端-到达的文件计数始终为8Kb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!