一个文件传送的代码,帮我看看吧....

gracejty 2004-01-06 01:51:31
发送端:
Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Dim sendsocket As New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
Dim ipendpoint As New Net.IPEndPoint(Net.IPAddress.Parse("192.168.10.9"), 8888)
Dim dlgopenfile As New OpenFileDialog()
Dim strfile As String
If dlgopenfile.ShowDialog = DialogResult.OK Then
strfile = dlgopenfile.FileName
End If
Dim fs As New IO.FileStream(strfile, IO.FileMode.OpenOrCreate, IO.FileAccess.Read)
Dim fssize(fs.Length - 1) As Byte
Dim strread As New IO.BinaryReader(fs)
strread.Read(fssize, 0, fssize.Length - 1)
sendsocket.Connect(ipendpoint)
sendsocket.Send(fssize)
Label1.Text = fs.Length()
fs.Close()
sendsocket.Shutdown(Net.Sockets.SocketShutdown.Send)
sendsocket.Close()
End Sub
End Class

接收端:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim receivesocket As New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dlgsavefile As New SaveFileDialog()
Dim strfile As String
If dlgsavefile.ShowDialog = DialogResult.OK Then
strfile = dlgsavefile.FileName
End If
Dim recfs As New IO.FileStream(strfile, IO.FileMode.OpenOrCreate)
Dim recbyte(229888) As Byte//注意阿,这个地方是出问题的关键,
该句指定收到229888字节,多者不收,怎么让接收的文件和发送的文件一样大阿.我郁闷死了 ,都想了好几天了阿.....跟我讲讲吧,小妹这里谢过大家了...请给我具体代码好吗?希望帮我调试一下.
Dim hostsocket As Net.Sockets.Socket = receivesocket.Accept()
Dim newfilestr As New IO.BinaryWriter(recfs)
hostsocket.Receive(recbyte)
newfilestr.Write(recbyte, 0, recbyte.Length - 1)
recfs.Close()
hostsocket.Shutdown(Net.Sockets.SocketShutdown.Receive)
hostsocket.Close()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim hostipendpiont As New Net.IPEndPoint(Net.IPAddress.Parse("192.168.10.9"), 8888)
receivesocket.Bind(hostipendpiont)
receivesocket.Listen(2)
End Sub
End Class
...全文
32 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
combot 2004-03-29
  • 打赏
  • 举报
回复

发送端:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sendsocket As New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
Dim ipendpoint As New Net.IPEndPoint(Net.IPAddress.Parse("127.0.0.1"), 8888)
Dim dlgopenfile As New OpenFileDialog
Dim strfile As String
If dlgopenfile.ShowDialog = DialogResult.OK Then
strfile = dlgopenfile.FileName
End If
Dim fs As New IO.FileStream(strfile, IO.FileMode.OpenOrCreate, IO.FileAccess.Read)
Dim fssize(fs.Length) As Byte 'removed -1
Dim strread As New IO.BinaryReader(fs)
strread.Read(fssize, 0, fssize.Length - 1)
sendsocket.Connect(ipendpoint)
sendsocket.Send(fssize)
Label1.Text = fs.Length()
fs.Close()
sendsocket.Shutdown(Net.Sockets.SocketShutdown.Send)
sendsocket.Close()
End Sub

接收端:

Const FILE_BLOCK As Integer = 1024 * 4 'added new code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dlgsavefile As New SaveFileDialog
Dim strfile As String
If dlgsavefile.ShowDialog = DialogResult.OK Then
strfile = dlgsavefile.FileName
End If
Dim recfs As New IO.FileStream(strfile, IO.FileMode.OpenOrCreate)
Dim recbyte(FILE_BLOCK -1) As Byte
Dim hostsocket As Net.Sockets.Socket = receivesocket.Accept()
Dim newfilestr As New IO.BinaryWriter(recfs)
Dim got As Integer = 0
'use loop to read block by block
Do
got = hostsocket.Receive(recbyte)
newfilestr.Write(recbyte, 0, got)
Loop While (got = FILE_BLOCK)

recfs.Close()
hostsocket.Shutdown(Net.Sockets.SocketShutdown.Receive)
hostsocket.Close()

End Sub
gracejty 2004-01-06
  • 打赏
  • 举报
回复
Dim recbyte(229888) As Byte//注意阿,这个地方是出问题的关键,
该句指定收到229888字节,多者不收,怎么让接收的文件和发送的文件一样大阿.我郁闷死了 ,都想了好几天了阿.....跟我讲讲吧,小妹这里谢过大家了...请给我具体代码好吗?希望帮我调试一下.
xiao88yan 2004-01-06
  • 打赏
  • 举报
回复
发送端的这句
strread.Read(fssize, 0, fssize.Length - 1)
是不是应该
strread.Read(fssize, 0, fssize.Length)

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧