国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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




發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 彰化市| 兴仁县| 古浪县| 营口市| 张家界市| 潮安县| 蓬莱市| 尖扎县| 北宁市| 晋宁县| 乌兰察布市| 石门县| 静宁县| 太白县| 金山区| 乐平市| 红安县| 东乌珠穆沁旗| 香港| 恩平市| 阳东县| 台东市| 洛浦县| 龙海市| 北安市| 定远县| 栾城县| 郴州市| 伊宁市| 怀宁县| 永德县| 磐安县| 上栗县| 辽阳县| 景东| 黎平县| 巴林右旗| 托克逊县| 云林县| 齐齐哈尔市| 遂昌县|