TCP服务器/客户端-到达的文件计数始终为8Kb [英] TCP Server/Client - Arrived File counts always 8Kb

查看:54
本文介绍了TCP服务器/客户端-到达的文件计数始终为8Kb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将文件从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

推荐答案

您输入的字节数与读取的一样多.而您仅读取tcpClient.ReceiveBufferSize字节,那么您会期望什么?请检查:我敢打赌这个缓冲区是8KB.

通常,代码看起来不错,……不严重.您不应该真正在与读取数据的线程相同的线程中接受.在服务器端,您实际上至少需要两个单独的网络线程:一个用于侦听新连接,一个用于从网络流读取/写入网络流.客户端部分上有一个单独的网络线程.您可以在我过去的答案中找到更多的想法:
来自同一端口号的多个客户端 [
You put as many bytes as you read; and you read just the tcpClient.ReceiveBufferSize bytes, so what would you expect? Please check up: I bet this buffer is 8KB.

Generally, the code looks, well… not serious. You should not really accept in the same thread as the thread where you read data. You really need at least two separate network threads on the server side: one to listen to new connection, one to read/write from/to the network streams. And one separate network thread on the client part. You can find further ideas in my past answer:
Multple clients from same port Number[^].

There are no situations where hard-coded path names could be helpful. I hope this code is only for research…

—SA


这篇关于TCP服务器/客户端-到达的文件计数始终为8Kb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆