VB.net基礎:使用UDP發(fā)送和接收消息
2024-07-10 13:00:58
供稿:網(wǎng)友
imports system.net
imports system.threading
imports system.text
imports system.net.sockets
module module1
dim portnumber as integer = 1984 '偵聽端口號
dim cmd as string = "chat:" '提示符
dim listener as socket '偵聽socket
dim tlistener as thread '偵聽線程
dim prompted as boolean = false '用于線程間同步的標志變量
sub main()
welcome() '歡迎信息
startlistener() '開始偵聽
startchatting() '準備好讓用戶發(fā)送消息
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 not ready '向用戶詢問偵聽端口號。用戶可以直接回車,表示選擇默認的。
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 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 not ready '向用戶詢問發(fā)送消息的目標主機和端口。用戶可以直接回車,表示選擇默認的。
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 '用戶現(xiàn)在可以開始發(fā)送消息
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) '發(fā)出消息
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() '偵聽線程
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) '接收
prompted = false
msg = encoding.unicode.getstring(bytes, 0, numget) '與發(fā)送消息一樣使用unicode編碼
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, you can type ""exit"" to exit program.")
console.writeline("========================================================================")
console.writeline("")
end sub
end module