VB.net基础:使用UDP发送和接收消息
2024-07-10 13:00:58
供稿:网友
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() '准备好让用户发送消息
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 '向用户询问发送消息的目标主机和端口。用户可以直接回车,表示选择默认的。
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
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) '与发送消息一样使用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