打印本文 打印本文 关闭窗口 关闭窗口
VB.net基础:使用UDP发送和接收消息
作者:武汉SEO闵涛  文章来源:敏韬网  点击数3776  更新时间:2009/4/23 19:01:17  文章录入:mintao  责任编辑:mintao
sp;  Private Sub StartChatting()

        Dim RemoteHost As String = Dns.GetHostName

        Dim RemotePort As Integer = 1984

        Dim RemotePoint As IPEndPoint

        Dim ready As Boolean = False

        Dim msg As String

 

        While Not ready ''''向用户询问发送消息的目标主机和端口。用户可以直接回车,表示选择默认的。

            msg = GetParams("---enter the name of the one you want to chat with(" & RemoteHost & "):")

            If Not msg = "" Then RemoteHost = msg

            msg = GetParams("---enter the port number that guy listening at(" & RemotePort & "):")

            If msg = "" Then msg = RemotePort

            Try

                RemotePort = Int(msg)

                RemotePoint = New IPEndPoint(Dns.GetHostByName(RemoteHost).AddressList(0), RemotePort)

                ready = True

            Catch ex As Exception

                Console.WriteLine("※※※ERROR※※※ " & vbCrLf & ex.Message & vbCrLf)

            End Try

        End While

 

        Console.WriteLine()

        Console.WriteLine("OK, now you can chat. Type ""help"" to find out what you can do.")

        Console.WriteLine()

 

        Dim sender As New UdpClient

        Dim Message As String = Prompt()

        While True      ''''用户现在可以开始发送消息

            Prompted = False

            Select Case Message.ToLower

                Case "exit"

                    Exit While

                Case "help"

                    ShowHelp()

                Case Else

                    Dim ByArr As Byte() = Encoding.Unicode.GetBytes(Message)

                    sender.Send(ByArr, ByArr.Length, RemotePoint)   ''''发出消息

            End Select

            Message = Prompt()

        End While

 

        tListener.Abort()

        End

    End Sub

 

    Private Function GetParams(ByVal Msg As String) As String

        Console.Write(Msg)

        Return Console.ReadLine

上一页  [1] [2] [3]  下一页

打印本文 打印本文 关闭窗口 关闭窗口