VB.net basis: send and receive messages using UDP Source: www.9cbs.net [2004-10-27 00:06:57] Author: YidingHe Editor: liyalin
Imports System.Netimports System.ThreadingImports System.Textimports System.Net.Sockets Syst.sockTs
Module Module1
DIM portnumber as integer = 1984 'listening port number
DIM CMD As String = "CHAT:" 'prompt
DIM Listener as socket 'listens for Socket
DIM TLISTENER AS THREAD 'Listening Thread
DIM PROMPTED AS BOOLEAN = false 'Sign Variables for Tooth Synchronization
Sub main ()
Welcome () 'Welcome information
StartListener () 'begins to listen
STARTCHATTING () 'Ready to let users send messages
End Sub
Private Sub Welcome ()
DIM TXTMESSAGE AS STRING = VBCRLF & "Welcome! I am a console application for lanting." & Vbcrlf
Console.writeLine (TXTMESSAGE)
End Sub
Private sub startlistener ()
Dim Ready as Boolean = FALSE
DIM LocalPoint As IpendPoint
DIM MSG AS String
While Not Ready 'asks the user to listen to the slogan. Users can enter them directly, indicating that the default is selected.
Msg = getparams ("=== now, Enter the Local Port You 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 exception
Console.writeline ("※※ ※※※" & 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 = 1984dim RemotePoint as iPndPoint
Dim Ready as Boolean = FALSE
DIM MSG AS String
While Not Ready 'asks the user to ask the target host and port that sends a message. Users can enter them directly, indicating that the default is selected.
Msg = getparams ("--- Enter the name of the one you want to chat with (" & transotehost & "):")
IF not msg = "" "THEN Remotehost = MSG
Msg = getparams ("--- Enter the port number there guide at (" & remotEport & "):")
IF msg = "" "THEN MSG = Remoteport
Try
Remoteport = Int (MSG)
RemotePoint = New IpendPoint (transotehost) .addresslist (0), Remoteport
Ready = TRUE
Catch exception
Console.writeline ("※※ ※※※" & 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 '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) 'issued 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 prompter
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) 'Receive
PROMPTED = FALSE
Msg = encoding.Unicode.getstring (bytes, 0, numget) 'uses Unicode encodings as sending messages
Console.WriteLine (VBCRLF & ">>>>>>>>>" & MSG & VBCRLF)
IF not prompter
Console.write (cmd)
PROMPTED = TRUE
END IF
End while
End Sub
Private sub dresshelp ()
Console.writeline ("")
Console.writeline ("===================================================== ============================ ")
Console.writeline ("this program is very simple" "to exit program")
Console.writeline ("===================================================== ============================ ")
Console.writeline ("")
End Sub
End module