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 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.0.1"), 8888) receivesocket.Bind(hostipendpiont) '建立远程计算机的的socket receivesocket.Listen(2)'监听socket End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim recfs As New IO.FileStream("p.doc", IO.FileMode.OpenOrCreate) '接收数据并将其保存到一个新的文件中 Dim recbyte(229888) As Byte Dim hostsocket As Net.Sockets.Socket = receivesocket.Accept() '同意和发送端计算机建立连接 Dim newfilestr As New IO.BinaryWriter(recfs)'流写 hostsocket.Receive(recbyte) 'recfs.Write(recbyte, 0, recbyte.Length - 1) newfilestr.Write(recbyte, 0, recbyte.Length - 1) recfs.Close() hostsocket.Shutdown(Net.Sockets.SocketShutdown.Receive) hostsocket.Close() End Sub End Class |