⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 清华大学出版社出版的 移动应用开发宝典 张大威(2008)的附书源代码
💻 VB
字号:
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class Form1

    Private Delegate Sub AppendToListBoxDelegate(ByVal newItem As String)



    Private Sub btnShare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim target As IPAddress
        Try
            target = IPAddress.Parse(Me.txtRecipient.Text)
        Catch
            MessageBox.Show("Invalid IP address")
            Return
        End Try
        Dim p As New Prospect
        p.Name = Me.txtName.Text
        p.Company = Me.txtCompany.Text
        p.Number = Me.txtNumber.Text
        Dim clientSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Try
            clientSocket.Connect(New IPEndPoint(target, &H94C0))
        Catch exception1 As SocketException
            MessageBox.Show(("Exception trying to connect: " & exception1.ToString))
            Return
        End Try
        Dim ns As New NetworkStream(clientSocket, True)
        Dim sw As New StreamWriter(ns)
        Dim sr As New StreamReader(ns)
        Try
            Try
                sw.WriteLine("PROSPECT")
                sw.WriteLine(p.ToString)
                Select Case sr.ReadLine
                    Case "OK"
                        MyBase.Invoke(New AppendToListBoxDelegate(AddressOf Me.AppendToListBox), New Object() {(target.ToString & " Sent successfully")})
                        Exit Select
                    Case "ERROR"
                        GoTo Label_013E
                End Select
                Return
Label_013E:
                MyBase.Invoke(New AppendToListBoxDelegate(AddressOf Me.AppendToListBox), New Object() {(target.ToString & " Failed to send")})
            Catch ie As IOException
                MessageBox.Show(("Exception trying to send: " & ie.ToString))
            Catch exception3 As SocketException
                MessageBox.Show(("Exception trying to send: " & exception3.ToString))
            End Try
        Finally
            ns.Close()
        End Try

    End Sub
    Private Sub btnFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If (Me.openFileDialog1.ShowDialog = DialogResult.OK) Then
            Dim tc As New TcpClient
            tc.Connect(New IPEndPoint(IPAddress.Parse(Me.txtRecipient.Text), &H94C0))
            If tc.Client.Connected Then
                Try
                    Try
                        Dim header As Byte() = Encoding.Unicode.GetBytes("FILE" & ChrW(13) & ChrW(10))
                        tc.Client.Send(header)
                        Dim filename As String = Path.GetFileName(Me.openFileDialog1.FileName)
                        header = Encoding.Unicode.GetBytes((filename & ChrW(13) & ChrW(10)))
                        tc.Client.Send(header)
                        Dim fi As New FileInfo(Me.openFileDialog1.FileName)
                        Dim buffer As Byte() = BitConverter.GetBytes(fi.Length)
                        tc.Client.Send(buffer)
                        Form1.SendFile(tc.Client, Me.openFileDialog1.FileName)
                    Catch se As SocketException
                        MessageBox.Show((se.NativeErrorCode.ToString & ": " & se.ToString))
                    End Try
                Finally
                    tc.Close()
                End Try
            End If
        End If

    End Sub
    Private Sub btnIP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim ihe As IPHostEntry = Dns.GetHostEntry(Me.txtHost.Text)
        If (ihe.AddressList.Length > 0) Then
            Me.txtRecipient.Text = ihe.AddressList(0).ToString
        Else
            Me.txtRecipient.Text = ""
        End If

    End Sub

    Private Sub ListenerThread()
        Dim listener As New TcpListener(New IPEndPoint(IPAddress.Any, &H94C0))
        listener.Start()

        Try
            While True


                Dim incomingClient As TcpClient
                incomingClient = listener.AcceptTcpClient

                Dim senderAddress As IPAddress = DirectCast(incomingClient.Client.RemoteEndPoint, IPEndPoint).Address
                Dim ns As NetworkStream = incomingClient.GetStream
                Try

                    Dim sr As New StreamReader(ns, Encoding.Unicode)
                    Dim sw As New StreamWriter(ns, Encoding.Unicode)
                    Select Case sr.ReadLine
                        Case "PROSPECT"
                            Dim fields As String() = sr.ReadLine.Split(New Char() {","c})
                            If (fields.Length = 3) Then
                                sw.WriteLine("OK")
                            Else
                                sw.WriteLine("ERROR")
                            End If
                            Dim receivedProspect As New Prospect
                            receivedProspect.Name = fields(0)
                            receivedProspect.Company = fields(1)
                            receivedProspect.Number = fields(2)
                            Invoke(New AppendToListBoxDelegate(AddressOf Me.AppendToListBox), New Object() {(senderAddress.ToString & " " & receivedProspect.ToString)})
                            Exit Select

                        Case "FILE"
                            Dim filename As String = sr.ReadLine
                            Dim len As Byte() = New Byte(8 - 1) {}
                            ns.Read(len, 0, 8)
                            Dim fileSize As Long = BitConverter.ToInt64(len, 0)
                            Dim fs As New FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), filename), FileMode.CreateNew)
                            Dim buffer As Byte() = New Byte(&H100 - 1) {}
                            Dim bytesread As Integer = ns.Read(buffer, 0, buffer.Length)
                            Dim totalbytesread As Integer = bytesread
                            Try
                                Do While (totalbytesread < fileSize)
                                    fs.Write(buffer, 0, bytesread)
                                    bytesread = ns.Read(buffer, 0, buffer.Length)
                                    totalbytesread = (totalbytesread + bytesread)
                                Loop
                                fs.Close()
                                sw.WriteLine("OK")
                                Invoke(New AppendToListBoxDelegate(AddressOf Me.AppendToListBox), New Object() {(senderAddress.ToString & " " & filename)})
                            Catch
                                sw.WriteLine("ERROR")
                            End Try
                            Exit Select
                    End Select
                Catch
                    Invoke(New AppendToListBoxDelegate(AddressOf Me.AppendToListBox), New Object() {(senderAddress.ToString & " Failed")})
                Finally
                    ns.Close()
        End Try

            End While
        Finally
            listener.Stop()
        End Try
    End Sub

    Private Shared Sub SendFile(ByVal s As Socket, ByVal filename As String)
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim buffer As Byte() = New Byte(&H100 - 1) {}
        Dim bytesRead As Integer = fs.Read(buffer, 0, buffer.Length)
        Do While (bytesRead > 0)
            s.Send(buffer, bytesRead, SocketFlags.None)
            bytesRead = fs.Read(buffer, 0, buffer.Length)
        Loop
        fs.Close()
    End Sub

    Private Sub AppendToListBox(ByVal newItem As String)
        Me.listBox1.Items.Add(newItem)
    End Sub












End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -