VB.net Basics: Sending and receiving messages using UDP

Source: Internet
Author: User
Tags exit integer readline socket thread port number
Imports System.Net

Imports System.Threading

Imports System.Text

Imports System.Net.Sockets



Module Module1



Dim portnumber as Integer = 1984 ' Listening port number

Dim CMD as String = "Chat:" Prompt

Dim listener as socket ' listening socket

Dim Tlistener as thread ' listening thread

Dim prompted as Boolean = False ' flags variable for synchronization between threads



Sub Main ()

Welcome () ' Welcome information

Startlistener () ' Starts listening

Startchatting () ' Ready for users to send messages

End Sub



Private Sub Welcome ()

Dim txtmessage as String = vbCrLf & welcome! I am a console application for LAN chatting. "& VbCrLf

Console.WriteLine (Txtmessage)

End Sub



Private Sub Startlistener ()

Dim ready as Boolean = False

Dim Localpoint as IPEndPoint

Dim msg as String



While the not ready ' asks the user for a listening port number. The user can enter directly, indicating that the default is selected.

msg = Getparams ("===now, enter the local port" and "Want to Listen" ("& PortNumber &"): ")

If msg = "" Then msg = portnumber

Try

PortNumber = Int (msg)

Localpoint = New IPEndPoint (dns.gethostbyname (dns.gethostname). AddressList (0), portnumber)

Listener = New Socket (addressfamily.internetwork, Sockettype.dgram, PROTOCOLTYPE.UDP)

Listener. Bind (Localpoint)

Ready = True

Catch ex as Exception

Console.WriteLine ("※※※error※※※" & VbCrLf & Ex. Message & VbCrLf)

End Try

End While



Tlistener = New Thread (AddressOf thrlistener)

Tlistener.start ()

End Sub



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 the not ready ' asks the user for the destination host and port where the message is sent. The user can enter directly, indicating that the default is selected.

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 ' can do. '

Console.WriteLine ()



Dim sender as New udpclient

Dim message as String = Prompt ()

While True ' users can now start sending messages

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) ' Send a message

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

End Function



Private Function Prompt () as String

If not prompted Then

Console.Write (CMD)

prompted = True

End If

Return Console.ReadLine

End Function





Private Sub Thrlistener () ' Listening thread

Dim bytes (4096) as Byte

Dim Numget as Integer

Dim MSG as String



While True

Debug.WriteLine ("Waiting for a Message ...")

Numget = listener. Receive (bytes) ' Received

prompted = False

Msg = Encoding.Unicode.GetString (bytes, 0, Numget) ' uses Unicode encoding as send message

Console.WriteLine (VbCrLf & ">>>>>>>>>" & MSG & VbCrLf)

If not prompted Then

Console.Write (CMD)

prompted = True

End If

End While

End Sub



Private Sub ShowHelp ()

Console.WriteLine ("")

Console.WriteLine ("========================================================================")

Console.WriteLine ("This program is very simple, with can type" "Exit" "to Exit program.")

Console.WriteLine ("========================================================================")

Console.WriteLine ("")

End Sub



End Module






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.