首页 > 编程 > .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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表