一个文件传送的代码,帮我看看吧....
发送端:
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