给你一个简单的事例
服务器端
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
TcpServer.Stop()
System.Environment.Exit(System.Environment.ExitCode)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ServerIp As IPHostEntry = Dns.GetHostByAddress("127.0.0.1")
TcpServer = New TcpListener(ServerIp.AddressList(0), 6000)
TcpServer.Start()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FS As New FileStream("Qing.txt", FileMode.Append)
Dim Writer As New BinaryWriter(FS)
Dim Netstream As Socket = TcpServer.AcceptSocket
Dim ServerStream As New NetworkStream(Netstream)
Dim Reader As New BinaryReader(ServerStream)
Writer.Write(Reader.ReadBytes(ServerStream.Length))
Reader.Close()
ServerStream.Close()
Writer.Close()
FS.Close()
End Sub
客户端 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim T1 As New Thread(AddressOf RunClient)
T1.Start()
End Sub
Private Sub RunClient()
Dim Opened As New OpenFileDialog
Opened.Filter = "所有文件(*.*)|*.*"
Dim Result As DialogResult = Opened.ShowDialog
If Result = DialogResult.Cancel Then
Exit Sub
End If
Dim Fs As FileStream
Dim Reader As BinaryReader
Dim NetStream As NetworkStream
Dim TCPClient As New TcpClient
Dim Writer As BinaryWriter
Try
TCPClient.Connect("ncqingchuan", 6000)
NetStream = TCPClient.GetStream
Fs = New FileStream(Opened.FileName, FileMode.Open)
Reader = New BinaryReader(Fs)
Writer = New BinaryWriter(NetStream)
Writer.Write(Reader.ReadBytes(Fs.Length))
Catch ex As Net.Sockets.SocketException
MessageBox.Show(ex.Message, "测试", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Catch ev As System.ArgumentNullException
MessageBox.Show(ev.Message, "测试", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Catch ec As NullReferenceException
MessageBox.Show(ec.Message, "测试", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Finally
Reader.Close()
Writer.Close()
Fs.Close()
NetStream.Close()
End Try
End Sub
这只是个大体饿框架,具体的细节问题,比如文件大小啊,文件名啊这些还需要补充代码