下载首页 | 资讯中心 | 下载分类 | 最近更新 | 排 行 榜 | 国产软件 | 国外软件 | 汉化补丁 |
文章搜索: 分类 关键字 收藏本站设为首页
您的位置:首页网页设计ASP程序 → P2P的简单示例(VB.net版) 二__教程
P2P的简单示例(VB.net版) 二__教程
日期:2007-5-20 0:37:32 人气:100     [ ]
上一页 [1] [2] [3] [4] [5] [6] 下一页
 

以下是客户端:

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
出处:本站原创 作者:佚名
 阅读排行
01.精美qq空间横幅代码
02.最酷qq个性女生网名
03.最新又有免费QQ秀啦《..
04.巧用透明FlaSh扮靓你的..
05.花之神匠代码(最新代码..
06.最新QQ空间免费导航
07.最新免费个人形象设置..
08.最新qq空间flash代码m..
09.CSS技术结合图像实现动..
10.Photoshop光影魔术师:..
11.QQ音速种子狂刷
12.最新QQ空间透明代码
13.PS实例教程:教你制作结..
14.Photoshop光影魔术师:..
15.制作背景图__教程
16.用Photoshop制作漂亮的..
17.如何获得QQ音速种子
18.≤QQ空间代码≥在日志..
19.网页浮动广告的制作代..
20.用Photoshop制作大红灯..
21.常用CSS
22.Photoshop给靓丽美女打..
 推荐文章
·Photoshop 表现技法之..
·快速将你的相片矢量化..
·PHOTOSHOP制作炽热的太..
·用Photoshop制作美丽的..
·流行杀手的娃娃工厂__..
·打造8号台球__教程
·PHOTOSHOP制作待机MM图..
·用Photoshop帮MM做纹身..
·PHOTOSHOP美眉着色绝招..
·PHOTOSHOP花露的制作_..
·PHOTOSHOP渐变工具的巧..
·PHOTOSHOP手绘奥兰多-..
·高难度抠图两种方法__..
·Photoshop高尔夫球的制..
·Photoshop打造精美玉佩..
Eqxia_COM下载站 版权所有 Copyright© 2001-2005 Www.eqxia.COM, All Rights Reserved.