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

首頁 > 編程 > .NET > 正文

P2P的簡單示例(VB.net版)

2024-07-10 13:07:09
字體:
來源:轉載
供稿:網友
   這是用vb.net實現的一個簡單的p2p示例。利用了udp打洞技術,分服務器端跟客戶端,服務器端負責登陸記錄用戶的ip和端口及轉發打洞消息。原理到處都有,這里就沒有貼出來。這里貼出了vb.net的代碼,供初學者交流,也歡迎高手點評……

  服務器端在啟動成功后,輸入help可以查看到服務器相關命令。

  客戶端在登陸成功后,輸入help可以查看客戶端相關命令(登陸時用戶名隨便)。

以下是服務器端:

imports system.net
imports system.net.sockets
imports system.text
imports system.threading
imports system.collections

module myudpserver

#region "全局變量"

dim serversocket as new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp)
dim ipep as ipendpoint = new ipendpoint(ipaddress.any, 11000)

dim htuserlist as new hashtable '用來保存在線用戶和用戶的"ip和端口"

dim username(0) as string
dim useripep(0) as ipendpoint
dim usertime(0) as integer

dim timerdelegate as new timercallback(addressof onlinetimeout)

#end region

#region "參數"

'以下是客戶端到服務器端的消息開頭
const loginin as string = "10" '請求登陸的消息|||消息形式:10+自己的用戶名
const loginout as string = "11" '請求登出的消息|||消息形式:11+自己的用戶名
const getulist as string = "12" '請求獲得在線用戶列表|||消息形式:12
const p2pconn as string = "13" '請求p2p連接的消息|||消息形式:13+自己的用戶名+|+對方的用戶名
const holdline as string = "14" '保持連接.|||消息開式:14+自己的用戶名

'以下是服務器到客戶端的消息開頭
const hvuser as string = "20" '用戶名已存在
const getuser as string = "21" '在線用戶列表|||消息格式:21+用戶名+ep
const makhold as string = "22" '打洞命令|||消息格式:22+ip
const loginok as string = "23" '登陸成功
const servcls as string = "24" '服務器關閉
const msgend as string = "25" '消息結束

'以下是服務器端的命名
const exitpro as string = "exit" '退出命令
const showulist as string = "showuser" '顯示在線用戶
const help as string = "help" '顯示幫助

#end region

#region "方法"

'主函數,程序入口
sub main()

'獲得服務器的ip地址
dim addresslist as system.net.ipaddress() = dns.gethostbyname(dns.gethostname()).addresslist
dim serverip as ipaddress = addresslist(0)

serversocket.bind(ipep)
console.writeline("服務器正在啟動....")
console.writeline("服務器ip:" & serverip.tostring & " 正在監聽" & ipep.port.tostring & "端口")
dim listenth as new thread(addressof listen)
listenth.start() '啟用監聽的線程
console.writeline("服務器啟動成功.....")

dim timer as new timer(timerdelegate, nothing, 0, 5000)

dim svinput as string
while true
console.write("server>")
svinput = console.readline().toupper
select case svinput
case exitpro
listenth.abort()
serversocket.close()
exit sub
case showulist
showuser()
case help
console.write("*********************************" & chr(10) & chr(13) & "exit:輸出當前程序" & chr(10) & chr(13) & "showuser:顯示當前在線用戶例表" & chr(10) & chr(13) & "help:顯示幫助" & chr(10) & chr(13) & "*********************************" & chr(10) & chr(13))
case else
console.writeline("*********************************" & chr(10) & chr(13) & "笨瓜,你輸入的不是有效的命令." & chr(10) & chr(13) & "*********************************")
end select
end while

end sub


'打印在線用戶
sub showuser()
dim hava as boolean = false
if username.length <> 0 then
dim i as integer
for i = 1 to username.length - 1
if username(i) <> "" then
hava = true
exit for
end if
next
if hava = false then
console.writeline("*********************************" & chr(10) & chr(13) & "當前沒有用戶在線" & chr(10) & chr(13) & "*********************************")
exit sub
end if
console.writeline("*********************************")
for i = 1 to username.length - 1
if username(i) <> "" then
console.writeline("用戶名:" & username(i) & " 地址:" & useripep(i).tostring)
end if
next
console.writeline("*********************************")
else
console.writeline("*********************************" & chr(10) & chr(13) & "當前沒有用戶在線" & chr(10) & chr(13) & "*********************************")
end if
end sub

'服務器監聽函數
sub listen()

while true

try
dim recv as integer = 0
dim data as [byte]() = new byte(1024) {}
dim sender as new ipendpoint(ipaddress.any, 0)
dim tempremoteep as endpoint = ctype(sender, endpoint)
recv = serversocket.receivefrom(data, tempremoteep)

'console.writeline(encoding.unicode.getstring(data))

dim msghead as string = encoding.unicode.getstring(data, 0, 4)
select case msghead
case loginin
dim loginthing as string = userlogin(data, tempremoteep, recv)
if loginthing = hvuser then
sendmsg(hvuser, tempremoteep)
elseif loginthing = loginok then
sendmsg(loginok, tempremoteep)

end if

case loginout
userloginout(data, recv)

case getulist
dim userinfo as string = getuserlist()
sendmsg(userinfo, tempremoteep)

case p2pconn
questp2pconn(data, recv)

case holdline
holdonline(data, recv)
end select

catch e as exception
'console.writeline(e.tostring)
end try
end while

end sub

'轉發p2p連接請求
private sub questp2pconn(byval data() as byte, byval recv as integer)

dim recvstr as string = encoding.unicode.getstring(data, 4, recv - 4)
dim split() as string = recvstr.split("|")

dim fromep as ipendpoint
dim toep as ipendpoint
dim i as integer
for i = 1 to username.length - 1
if username(i) = split(0) then
fromep = useripep(i)
end if
if username(i) = split(1) then
toep = useripep(i)
end if
next
dim holdbytes() as byte = encoding.unicode.getbytes(makhold & fromep.tostring)
serversocket.sendto(holdbytes, toep)
end sub

'函數.返回所有在線用戶.其格式:用戶名+|+用戶ipep+|
private function getuserlist() as string
dim userinfo as string = getuser
dim i as integer
for i = 1 to username.length - 1
if username(i) <> "" then
userinfo += username(i) & "|" & useripep(i).tostring & "|"
end if
next
return userinfo
end function

'用戶登陸,直接返回登陸是否成功的值
private function userlogin(byval data as byte(), byval userep as ipendpoint, byval recvcount as integer) as string

dim uname as string = encoding.unicode.getstring(data, 4, recvcount - 4)

dim uinfobytes() as byte

dim i as integer
dim j as integer

for i = 1 to username.length - 1
if uname = username(i) then
return hvuser
end if
next

for i = 1 to username.length - 1
if username(i) = "" then
username(i) = uname
useripep(i) = userep
usertime(i) = 60
console.write(chr(10) & chr(13) & "*********************************" & chr(10) & chr(13) & uname.trim & "上線了." & "用戶地址:" & userep.tostring & chr(10) & chr(13) & "*********************************" & chr(10) & chr(13))
console.write("server>")

uinfobytes = encoding.unicode.getbytes(loginin & username(i) & "|" & useripep(i).tostring)

for j = 1 to username.length - 1
if username(j) <> "" and username(j) <> uname then
serversocket.sendto(uinfobytes, useripep(j))
end if
next
return loginok
end if
next

dim usercount as integer = username.length

redim preserve username(usercount)
redim preserve useripep(usercount)
redim preserve usertime(usercount)

username(username.length - 1) = uname
useripep(useripep.length - 1) = userep
usertime(usertime.length - 1) = 60

console.write(chr(10) & chr(13) & "*********************************" & chr(10) & chr(13) & uname.trim & "上線了." & "用戶地址:" & userep.tostring & chr(10) & chr(13) & "*********************************" & chr(10) & chr(13))
console.write("server>")

uinfobytes = encoding.unicode.getbytes(loginin & username(username.length - 1) & "|" & useripep(username.length - 1).tostring)

for j = 1 to username.length - 1
if username(j) <> "" and username(j) <> uname then
serversocket.sendto(uinfobytes, useripep(j))
end if
next
return loginok

end function


'用戶登出
private sub userloginout(byval data as byte(), byval recvcount as integer)

dim i as integer
dim uname as string = encoding.unicode.getstring(data, 4, recvcount - 4)

for i = 1 to username.length - 1

if uname = username(i) then

dim loginoutmsg as string = loginout & username(i)

username(i) = ""
useripep(i) = nothing
usertime(i) = 0

dim j as integer
for j = 1 to username.length - 1
if username(j) <> "" then

sendmsg(loginoutmsg, useripep(j))

end if
next

console.writeline(chr(10) & chr(13) & "*********************************")
console.writeline("用戶" & uname & "下線了.")
console.writeline("*********************************")
console.write("server>")

exit for

end if

next

end sub

'保持用戶在線的過程
private sub holdonline(byval data as byte(), byval recvcount as integer)

dim uname as string = encoding.unicode.getstring(data, 4, recvcount - 4)

dim i as integer

for i = 1 to username.length - 1

if uname = username(i) then

usertime(i) = 60
exit for

end if

next

end sub

'用戶超時退出
private sub onlinetimeout(byval state as [object])

dim i as integer

for i = 1 to username.length - 1

if usertime(i) > 0 then

usertime(i) -= 5

if usertime(i) <= 0 then

dim loginoutmsg as string = loginout & username(i)

console.writeline(chr(10) & chr(13) & "*********************************")
console.writeline("用戶" & username(i) & "下線了.")
console.writeline("*********************************")
console.write("server>")

username(i) = ""
useripep(i) = nothing

dim uloginoutbytes() as byte = encoding.unicode.getbytes(loginoutmsg)

dim j as integer
for j = 1 to username.length - 1

if username(j) <> "" then
if useripep(j) is nothing then
else
serversocket.sendto(uloginoutbytes, useripep(j))
end if
end if

next

end if

end if

next

end sub

'發送消息的函數
sub sendmsg(byval msg as string, byval remoteep as ipendpoint)
dim sendbytes as [byte]() = encoding.unicode.getbytes(msg)
try

serversocket.sendto(sendbytes, remoteep)

catch e as exception
console.writeline(e.tostring())
end try
end sub

#end region

end module

以下是客戶端:

imports system.net
imports system.net.sockets
imports system.text
imports system.threading

module module1

#region "參數"

'以下是客戶端到服務器端的消息開頭
const loginin as string = "10" '請求登陸的消息|||消息形式:10+自己的用戶名
const loginout as string = "11" '請求登出的消息|||消息形式:11+自己的用戶名
const getulist as string = "12" '請求獲得在線用戶列表|||消息形式:12+自己的用戶名
const p2pconn as string = "13" '請求p2p連接的消息|||消息形式:13+自己的用戶名+對方的用戶名
const holdline as string = "14" '保持連接.|||消息開式:14+自己的用戶名

'以下是服務器到客戶端的消息開頭
const hvuser as string = "20" '用戶名已存在
const getuser as string = "21" '在線用戶列表|||消息格式:21+用戶名+ep
const makhold as string = "22" '打洞命令|||消息格式:22+ip
const loginok as string = "23" '登陸成功
const servcls as string = "24" '服務器關閉
const msgend as string = "25" '消息結束

'以下是客戶端到客戶端的消息開頭
const holdok as string = "30" '打洞成功
const chatmsg as string = "31" '聊天消息
const chtmsgend as string = "32" '聊天消息發送成功

'以下是客戶端的命名
const exitpro as string = "exit" '退出命令
const showulist as string = "showuser" '顯示在線用戶
const help as string = "help" '顯示幫助
const send as string = "send" '發送消息

#end region

#region "全局全量"
delegate sub mymethoddelegate(byref myindata as byte()) '登陸時用的事件

'dim maxtry as integer = 5
dim msgsendend as boolean = false '消息是否發送成功,若發送成功,則會返回結束消息
dim thlisten as new thread(addressof listen) '監聽的線程
dim clientsocket as new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp) '客戶端套節字的定義
dim username as string '當前用戶名
dim serverep as ipendpoint '服務器的ipep
dim holdbytes as [byte]() = encoding.unicode.getbytes(holdline & username) '和服務器保持連接連接時用到的byte數組
dim olusername() as string
dim oluserep() as ipendpoint
dim getureccount as integer
dim testhold as boolean = false
dim testchat as boolean = false

private receivedone as manualresetevent '在登陸時用來阻塞線程,等待收到數據
private senddone as manualresetevent '用來陰塞發送消息的線程.等待收到回送的確認消息
private getudone as manualresetevent '用來阻塞請求好友名單的線程,等待接收好友名單
private holddone as manualresetevent '用來阻塞打洞時的線程
private chatdone as manualresetevent '用來阻塞發送聊天消息時的線程

dim timerdelegate as new timercallback(addressof holdonline) '為保持在線狀態弄得

#end region

#region "方法"

'主函數,程序入口
sub main()
dim inputip as string
dim inputok as boolean = false

'判斷輸入的ip,并且保存服務器的ipep
while inputok <> true
console.write("請輸入服務器ip:")
inputip = console.readline()
try
serverep = new ipendpoint(ipaddress.parse(inputip), 11000)
inputok = true
catch
console.writeline("你輸入的服務器ip不正確,請重新輸入.")
inputok = false
end try
end while

dim bool as boolean = false

'判斷用戶是否登陸成功
while bool <> true

dim loginok as boolean = login()
if loginok = true then
bool = true
else
console.write("是否重試:輸入y重試,輸入任意值退出程序:")
dim tempyn as string = console.readline.toupper
if tempyn = "y" then
bool = false
else
exit sub
end if
end if

end while

console.writeline("用戶名:" & username)
holdbytes = encoding.unicode.getbytes(holdline & username)
'登陸成功后.用一個timer,每隔50秒向服務器發送消息,保持在線狀態跟在主機注冊的端口
dim timer as new timer(timerdelegate, nothing, 10000, 50000)

'請求在線名單
console.writeline("正在獲取在線名單,請稍后....")
dim getubool as boolean = false
while getubool <> true
getubool = getu()
if getubool = false then
console.write("是否重試:輸入y重試,輸入任意值退出程序:")
dim tempyn as string = console.readline.toupper
if tempyn = "y" then
bool = false
else
exit sub
end if
end if
end while

thlisten.start()

'用來處理客戶端的一些命令
dim svinput as string
while true
console.write("client>")
svinput = console.readline().toupper
select case svinput
case exitpro
exitapp()
thlisten.abort()
clientsocket.close()
exit sub
case showulist
console.writeline("*********************************")
showuserlist()
console.writeline("*********************************")
case help
console.write("*********************************" & chr(10) & chr(13) & "exit:輸出當前程序" & chr(10) & chr(13) & "showuser:顯示當前在線用戶例表" & chr(10) & chr(13) & "send:發送消息.格式:send 用戶名 消息" & chr(10) & chr(13) & "help:顯示幫助" & chr(10) & chr(13) & "*********************************" & chr(10) & chr(13))
case else
if svinput.substring(0, 4) = "send" then
dim split() as string = svinput.split(" ")
if split.length = 3 then
sendchatmsg(split(1), split(2))
else
console.writeline("*********************************" & chr(10) & chr(13) & "你輸入的命令格式不正確.send命令格式為:send 用戶名 你的消息" & chr(10) & chr(13) & "*********************************")
end if
else
console.writeline("*********************************" & chr(10) & chr(13) & "笨瓜,你輸入的不是有效的命令." & chr(10) & chr(13) & "*********************************")
end if
end select
end while

end sub

'登陸函數
private function login() as boolean

receivedone = new manualresetevent(false)
dim userbytes as [byte]()

dim userok as boolean = false

console.write("請輸入你的用戶名:")

'判斷用戶名是否符合格式
while (userok <> true)
username = console.readline.toupper
userbytes = encoding.unicode.getbytes(loginin & username)

if userbytes.length > 24 or userbytes.length < 10 then
console.writeline("用戶名不得小于6個字節,且不得大于20個字節.")
console.write("請重新輸入你的用戶名:")
else
userok = true
end if
end while
'向服務器發送客戶消息
clientsocket.sendto(userbytes, serverep)

dim data as [byte]() = new byte(1024) {}

dim comstr as string = encoding.unicode.getstring(data, 0, 4)

'異面的接收服務器回送的消息
dim dgrecv as new mymethoddelegate(addressof recvlogin)
dgrecv.begininvoke(data, nothing, nothing)

'等待服務器回送消息的時長為10秒,否則為服務器超時
receivedone.waitone(30000, true)

dim recvstr as string = encoding.unicode.getstring(data, 0, 4)

if recvstr = comstr then
console.writeline("服務器超時.登陸失敗!!")
return false
end if

if encoding.unicode.getstring(data, 0, 4) = loginok then
console.writeline("登陸成功!!")
return true
elseif encoding.unicode.getstring(data, 0, 4) = hvuser then
console.writeline("用戶名重復.登陸失敗!!")
return false
else
console.writeline("服務器未知錯誤,登陸失敗!!")
return false
end if

end function

'登出函數
private sub exitapp()

dim loginoutstr as string = loginout & username
dim sendbytes as [byte]() = encoding.unicode.getbytes(loginoutstr)
clientsocket.sendto(sendbytes, serverep)

end sub

'請求好友列表的函數
private function getu() as boolean

getudone = new manualresetevent(false)
dim getubytes as byte() = encoding.unicode.getbytes(getulist)
clientsocket.sendto(getubytes, serverep)

dim data as [byte]() = new byte(4056) {}
dim comstr as string = encoding.unicode.getstring(data, 0, 4)

dim gurecv as new mymethoddelegate(addressof recvgetu)
gurecv.begininvoke(data, nothing, nothing)

getudone.waitone(30000, true)

dim recvstr as string = encoding.unicode.getstring(data, 0, 4)

if recvstr = comstr then
console.writeline("服務器超時.或取好友名單失敗!!")
return false
end if

if encoding.unicode.getstring(data, 0, 4) = getuser then
getuserlist(data, getureccount)
console.writeline("獲取在線名單成功!!")
showuserlist()
return true
else
console.writeline("服務器未知錯誤,獲取在線名單失敗!!")
return false
end if

end function

'登陸時用來異步的接收服務器發送的消息
sub recvlogin(byref indata as byte())

clientsocket.receive(indata)
receivedone.set()

end sub

'請求好友名單時用來異步接收服務器發送的消息
sub recvgetu(byref indata as byte())

getureccount = clientsocket.receive(indata)
getudone.set()

end sub

'處理收到的在線用戶信息
private sub getuserlist(byval userinfobytes() as byte, byval reccount as integer)

dim ustr as string = encoding.unicode.getstring(userinfobytes, 4, reccount - 4)

dim splitstr() as string = nothing

splitstr = ustr.split("|")

dim ipepsplit() as string = nothing

dim i as integer = 0

dim k as integer
for k = 0 to splitstr.length - 2 step 2
redim preserve olusername(i)
redim preserve oluserep(i)

olusername(i) = splitstr(k)
ipepsplit = splitstr(k + 1).split(":")
oluserep(i) = new ipendpoint(ipaddress.parse(ipepsplit(0)), ipepsplit(1))

ipepsplit = nothing
i += 1
next

end sub

'顯示在線用戶
private sub showuserlist()
dim i as integer
for i = 0 to olusername.length - 1
if olusername(i) <> "" then
console.writeline("用戶名:" & olusername(i) & " 用戶ip:" & oluserep(i).tostring)
end if
next
end sub

'客戶程序監聽的函數
sub listen()

while true

try
dim recv as integer = 0 '收到的字節數
dim data as [byte]() = new byte(1024) {} '緩沖區大小
dim sender as new ipendpoint(ipaddress.any, 0)
dim tempremoteep as endpoint = ctype(sender, endpoint)
recv = clientsocket.receivefrom(data, tempremoteep)

dim msghead as string = encoding.unicode.getstring(data, 0, 4) '獲得消息頭的內容
select case msghead
case msgend
msgsendend = true
senddone.set()
case loginin
addonline(data, recv)
case loginout
removeonline(data, recv)
case msgend
msgsendend = true
senddone.set()
case makhold
console.writeline(chr(10) & chr(13) & "收到打洞消息.")
makehold(data, recv)
console.write("client>")
case chatmsg
showchatmsg(data, recv)
case holdok
testhold = true
holddone.set()
case chtmsgend
testchat = true
chatdone.set()
end select

catch
end try

end while
end sub

'發送聊天消息
private sub sendchatmsg(byval remoteuser as string, byval chatmsgstr as string)

if remoteuser = username then
console.writeline("豬頭,你想干什么!!!")
exit sub
end if

dim i as integer

dim remoteuep as ipendpoint
for i = 0 to olusername.length - 1
if remoteuser = olusername(i) then
remoteuep = oluserep(i)
exit for
end if
if i = olusername.length - 1 then
console.writeline("找不到你想發送的用戶.")
exit sub
end if
next
dim msgbytes() as byte = encoding.unicode.getbytes(chatmsg & username & "|" & chatmsgstr)
dim holdbytes() as byte = encoding.unicode.getbytes(p2pconn & username & "|" & remoteuser)

chatdone = new manualresetevent(false)
clientsocket.sendto(msgbytes, remoteuep)
chatdone.waitone(10000, true)
if testchat = true then
testchat = false
exit sub
end if

testhold = false
while testhold <> true
console.writeline("打洞ing.....")
holddone = new manualresetevent(false)
clientsocket.sendto(holdbytes, remoteuep)
clientsocket.sendto(holdbytes, serverep)
holddone.waitone(10000, true)
if testhold = true then
exit while
else
console.writeline("打洞超時,發送消息失敗.")
console.write("是否重試,按y重試,按任意值結束發送:")
dim yorn as string = console.readline().toupper
if yorn = "y" then
testhold = false
else
exit sub
end if
end if
end while

while testchat <> true
console.writeline("打洞成功,正在準備發送.....")
chatdone = new manualresetevent(false)
clientsocket.sendto(msgbytes, remoteuep)
chatdone.waitone(10000, true)
if testchat = true then
console.writeline("消息發送成功!!")
exit while
else
console.writeline("發送超時,發送消息失敗.")
console.write("是否重試,按y重試,按任意值結束發送:")
dim yorn as string = console.readline().toupper
if yorn = "y" then
testchat = false
else
exit sub
end if
end if
end while
testhold = false
testchat = false
end sub

'處理聊天消息
private sub showchatmsg(byval indata() as byte, byval recvcount as integer)
dim msgstr as string = encoding.unicode.getstring(indata, 4, recvcount - 4)
dim splitstr() as string = msgstr.split("|")
dim fromuname as string = splitstr(0)
dim msg as string = splitstr(1)
console.writeline(chr(10) & chr(13) & "收到來自" & fromuname & "的消息:" & msg)
console.write("client>")
dim i as integer
for i = 0 to olusername.length - 1
if olusername(i) = fromuname then
exit for
end if
next
dim tempbytes() as byte = encoding.unicode.getbytes(chtmsgend)
clientsocket.sendto(tempbytes, oluserep(i))
end sub

'處理打洞函數
private sub makehold(byval indata() as byte, byval recvcount as integer)
dim makholdstr as string = encoding.unicode.getstring(indata, 4, recvcount)
dim ipepstr() as string = makholdstr.split(":")
dim holdep as ipendpoint = new ipendpoint(ipaddress.parse(ipepstr(0)), ipepstr(1))

dim holdbytes() as byte = encoding.unicode.getbytes(holdok & username)
clientsocket.sendto(holdbytes, holdep)
console.writeline("回送打洞消息.")
end sub

'處理用戶上線的函數
private sub addonline(byval indata() as byte, byval recvcount as integer)
dim instr as string = encoding.unicode.getstring(indata, 4, recvcount - 4)
dim userinfo() as string = instr.split("|")
dim struserep() as string = userinfo(1).split(":")

dim i as integer
for i = 0 to olusername.length - 1
if olusername(i) = "" then
olusername(i) = userinfo(0)
oluserep(i) = new ipendpoint(ipaddress.parse(struserep(0)), struserep(1))
console.writeline(chr(10) & chr(13) & "用戶" & olusername(i) & "上線了. 用戶地址:" & oluserep(i).tostring)
console.write("client>")
exit sub
end if
next
redim preserve olusername(i + 1)
redim preserve oluserep(i + 1)

olusername(i + 1) = userinfo(0)
oluserep(i + 1) = new ipendpoint(ipaddress.parse(struserep(0)), struserep(1))

console.writeline(chr(10) & chr(13) & "用戶" & olusername(i + 1) & "上線了. 用戶地址:" & oluserep(i + 1).tostring)
console.write("client>")

end sub

'處理用戶下線的函數
private sub removeonline(byval indata() as byte, byval recvcount as integer)
dim offuname as string = encoding.unicode.getstring(indata, 4, recvcount - 4)

dim i as integer
for i = 0 to olusername.length - 1
if olusername(i) = offuname then
olusername(i) = ""
oluserep(i) = nothing
console.writeline(chr(10) & chr(13) & "用戶" & offuname & "下線了.")
console.write("client>")
exit sub
end if
next
end sub

'發送消息的函數
public function sendmsg(byval msg as string, byval sendtoipep as ipendpoint) as string

dim sendbytes as [byte]() = encoding.unicode.getbytes(msg)

'判斷發送的字節數是否超過了服務器緩沖區大小
if sendbytes.length > 1024 then
return "w輸入的字數太多"
end if

'判斷消息是否發送成功
while msgsendend = false

senddone = new manualresetevent(false)

try

clientsocket.sendto(sendbytes, sendtoipep)

senddone.waitone(10000, true) '阻塞線程10秒

if msgsendend = false then
console.writeline("消息發送超時")
else
exit while
end if

catch e as exception

console.writeline("發送消息失敗" & e.tostring)
exit function

end try

console.write("是否重試?按y重試,按任意鍵退出:")
dim userinput as string = console.readline.toupper

if userinput = "y" then
else
msgsendend = false
exit function
end if

end while

msgsendend = false

end function

'用保持在線狀態的函數
private sub holdonline(byval state as [object])
clientsocket.sendto(holdbytes, serverep)
end sub

#end region

end module

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鸡西市| 宁陵县| 巴南区| 周口市| 环江| 平阴县| 库伦旗| 建德市| 日照市| 金湖县| 宝鸡市| 克拉玛依市| 黎川县| 金川县| 陕西省| 宣恩县| 醴陵市| 张家界市| 贵阳市| 靖江市| 积石山| 铜陵市| 巴林左旗| 榆林市| 南阳市| 通辽市| 南城县| 富宁县| 专栏| 黑山县| 波密县| 米泉市| 广宗县| 河北省| 建水县| 讷河市| 玛曲县| 淮南市| 高邮市| 清远市| 清涧县|