好了界面工作已经完成了 接下来我们需要输入代码了 选择Form 窗体 进入代码编辑器中 首先我们需要进行声明 Imports System Net Sockets Imports System Net Imports System IO Public Class Form Inherits System Windows Forms Form 进入Button _Click事件中 Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button Click Dim mysocket As New Socket(AddressFamily InterNeork SocketType Stream ProtocolType Tcp) 声明socket Dim myipendponit As New IPEndPoint(IPAddress Parse( ) ) 建立一个终结点 OpenFileDialog Filter = 文本文件( txt) * txt OpenFileDialog InitialDirectory = c:\\ If OpenFileDialog ShowDialog() = DialogResult OK Then Dim fs As New IO FileStream(OpenFileDialog FileName _ IO FileMode OpenOrCreate IO FileAccess Read) 你所要传输的文件 Dim ssize(fs Length ) As Byte Dim br As New BinaryReader(fs) 处理要传输的文件 br Read(ssize ssize Length ) mysocket Connect(myipendponit) 连接到远程计算机 mysocket Send(ssize) 发送文件 Label Text = fs Length() fs Close() mysocket Shutdown(Net Sockets SocketShutdown Send) 关闭已发送连接 mysocket Close() 关闭本机socket End If End Sub 进入Form _Load事件中 Private Sub Form _Load(ByVal sender As System Object ByVal e As System EventArgs) Handles MyBase Load Dim window As New Form () window Show() End Sub 选择Form 窗体 进入代码编辑器中 首先我们需要进行声明 Imports System Net Sockets Imports System Net Imports System IO Public Class Form Inherits System Windows Forms Form Dim receivesocket As New Socket(AddressFamily InterNeork SocketType Stream ProtocolType Tcp) 进入Form _Load事件中 Private Sub Form _Load(ByVal sender As System Object ByVal e As System EventArgs) Handles MyBase Load Dim hostipendpoint As New IPEndPoint(IPAddress Parse( ) ) receivesocket Bind(hostipendpoint) 建立远程计算机的的socket receivesocket Listen( ) 监听socket End Sub 进入Button _Click事件中 Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button Click SaveFileDialog Filter = 文本文件( txt) * txt SaveFileDialog FileName = 接收的文件 txt SaveFileDialog InitialDirectory = c:\\Mytext If SaveFileDialog ShowDialog() = DialogResult OK Then Dim fs As New IO FileStream(SaveFileDialog FileName _ IO FileMode OpenOrCreate) 接收数据并将其保存到一个新的文件 Dim rebyte( ) As Byte Dim myhostsocket As Socket = receivesocket Accept() 发送端计算机建立连接 Dim wr As New BinaryWriter(fs) 流写 myhostsocket Receive(rebyte) wr Write(rebyte rebyte Length ) fs Close() myhostsocket Shutdown(SocketShutdown Receive) myhostsocket Close() Label Text = SaveFileDialog FileName 读取已保存的文件 Dim Readw As StreamReader Readw = File OpenText(SaveFileDialog FileName) 设置指针到开始位置 Readw BaseStream Seek( SeekOrigin Begin) Readw BaseStream Position = While (Readw Peek() > ) TextBox Text += Readw ReadLine() & vbCrLf End While Readw Close() End If End Sub 好了代码输入完毕 接下来我们来运行程序测试一下 程序启动后会弹出Form 窗体(发送端)和Form 窗体(接收端) 我们选择Form 窗体(发送端)点击发送按钮 随意选择一个文本文件即可 看一看效果 如图