当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > VB.net基础:使用UDP发送和接收消息

ASP.NET
FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用方法
.NET 常用功能和代码小结
在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出
asp.net IList查询数据后格式化数据再绑定控件
asp.net sql存储过程
asp.net 简单实现禁用或启用页面中的某一类型的控件
asp.net(c#)获取内容第一张图片地址的函数
The remote procedure call failed and did not execute的解决办法
ASP.NET 在线文件管理
asp.net 读取并修改config文件实现代码
ASP.NET Cookie 操作实现
asp.net Silverlight中的模式窗体
Silverlight中动态获取Web Service地址
asp.net Silverlight应用程序中获取载体aspx页面参数
asp.net 水晶报表隔行换色实现方法
asp.net 获取Gridview隐藏列的值
手动把asp.net的类生成dll文件的方法
asp.net 使用ObjectDataSource控件在ASP.NET中实现Ajax真分页
动态指定任意类型的ObjectDataSource对象的查询参数
asp.net Md5的用法小结

ASP.NET 中的 VB.net基础:使用UDP发送和接收消息


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 42 ::
收藏到网摘: n/a

Imports System.NetImports System.ThreadingImports System.TextImports 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