The UDPChat Source(VB.NET)

原创 2002年04月09日 09:03:00

The UDPChat-Server Source(VB.NET)

'I use Winsock Control SP5(VB6)

Public Class frmMain
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写处置以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意:以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents txtChatLog As System.Windows.Forms.TextBox
    Friend WithEvents pelSaid As System.Windows.Forms.Panel
    Friend WithEvents txtSaid As System.Windows.Forms.TextBox
    Friend WithEvents btnSend As System.Windows.Forms.Button
    Friend WithEvents sckServer As AxMSWinsockLib.AxWinsock
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmMain))
        Me.txtChatLog = New System.Windows.Forms.TextBox()
        Me.pelSaid = New System.Windows.Forms.Panel()
        Me.btnSend = New System.Windows.Forms.Button()
        Me.txtSaid = New System.Windows.Forms.TextBox()
        Me.sckServer = New AxMSWinsockLib.AxWinsock()
        Me.pelSaid.SuspendLayout()
        CType(Me.sckServer, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'txtChatLog
        '
        Me.txtChatLog.Dock = System.Windows.Forms.DockStyle.Top
        Me.txtChatLog.Multiline = True
        Me.txtChatLog.Name = "txtChatLog"
        Me.txtChatLog.ReadOnly = True
        Me.txtChatLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtChatLog.Size = New System.Drawing.Size(392, 328)
        Me.txtChatLog.TabIndex = 0
        Me.txtChatLog.TabStop = False
        Me.txtChatLog.Text = ""
        '
        'pelSaid
        '
        Me.pelSaid.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSend, Me.txtSaid})
        Me.pelSaid.Dock = System.Windows.Forms.DockStyle.Bottom
        Me.pelSaid.Location = New System.Drawing.Point(0, 332)
        Me.pelSaid.Name = "pelSaid"
        Me.pelSaid.Size = New System.Drawing.Size(392, 34)
        Me.pelSaid.TabIndex = 1
        '
        'btnSend
        '
        Me.btnSend.Location = New System.Drawing.Point(296, 5)
        Me.btnSend.Name = "btnSend"
        Me.btnSend.Size = New System.Drawing.Size(80, 24)
        Me.btnSend.TabIndex = 0
        Me.btnSend.Text = "Send"
        '
        'txtSaid
        '
        Me.txtSaid.Location = New System.Drawing.Point(8, 6)
        Me.txtSaid.Name = "txtSaid"
        Me.txtSaid.Size = New System.Drawing.Size(280, 21)
        Me.txtSaid.TabIndex = 0
        Me.txtSaid.Text = ""
        '
        'sckServer
        '
        Me.sckServer.Enabled = True
        Me.sckServer.Location = New System.Drawing.Point(16, 8)
        Me.sckServer.Name = "sckServer"
        Me.sckServer.OcxState = CType(resources.GetObject("sckServer.OcxState"), System.Windows.Forms.AxHost.State)
        Me.sckServer.Size = New System.Drawing.Size(28, 28)
        Me.sckServer.TabIndex = 2
        '
        'frmMain
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(392, 366)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.sckServer, Me.pelSaid, Me.txtChatLog})
        Me.MaximizeBox = False
        Me.MaximumSize = New System.Drawing.Size(400, 400)
        Me.MinimumSize = New System.Drawing.Size(400, 400)
        Me.Name = "frmMain"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "UDPChat-Server"
        Me.pelSaid.ResumeLayout(False)
        CType(Me.sckServer, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region "Winsock Event"

    Private Sub sckServer_ConnectionRequest(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles sckServer.ConnectionRequest
        txtChatLog.AppendText("Request of Connecting from " & e.requestID & vbCrLf)
        txtChatLog.AppendText("Request Accepted...." & vbCrLf)
        sckServer.Accept(e.requestID)
        sckServer.SendData("Connection Accepted...." & vbCrLf)
    End Sub

    Private Sub sckServer_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles sckServer.DataArrival
        Dim objData As Object
        sckServer.GetData(objData, vbString)
        txtChatLog.AppendText("Client: " & CStr(objData) & vbCrLf)
    End Sub

#End Region

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        sckServer.RemoteHost = "127.0.0.1"
        sckServer.RemotePort = 8000
        sckServer.Bind(1000)
        txtChatLog.AppendText("Binded to localhost at 1000 and Remote port 8000" & vbCrLf)
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        If txtSaid.Text.Trim = "" Then
            MsgBox("Please input the chat the contents", MsgBoxStyle.OKOnly, "UDPChat-Server")
            Exit Sub
        End If

        Try
            txtChatLog.AppendText("Server: " & txtSaid.Text & vbCrLf)
            sckServer.SendData(CType(txtSaid.Text, Object))
            txtSaid.Text = ""
        Catch
            MsgBox("Error Occured " & Err.Description & vbCrLf & Err.Number, MsgBoxStyle.OKOnly, "UDPChat-Server")
        End Try
    End Sub

    Private Sub txtSaid_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSaid.KeyPress
        If (e.KeyChar = Chr(13)) Then
            btnSend_Click(sender, New System.EventArgs())
        End If
    End Sub
End Class


The UDPChat-Client Source(VB.NET)


'I use Winsock Control SP5(VB6)

Public Class frmMain
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写处置以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意:以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents pelSaid As System.Windows.Forms.Panel
    Friend WithEvents btnSend As System.Windows.Forms.Button
    Friend WithEvents txtSaid As System.Windows.Forms.TextBox
    Friend WithEvents txtChatLog As System.Windows.Forms.TextBox
    Friend WithEvents sckClient As AxMSWinsockLib.AxWinsock
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmMain))
        Me.pelSaid = New System.Windows.Forms.Panel()
        Me.btnSend = New System.Windows.Forms.Button()
        Me.txtSaid = New System.Windows.Forms.TextBox()
        Me.txtChatLog = New System.Windows.Forms.TextBox()
        Me.sckClient = New AxMSWinsockLib.AxWinsock()
        Me.pelSaid.SuspendLayout()
        CType(Me.sckClient, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'pelSaid
        '
        Me.pelSaid.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSend, Me.txtSaid})
        Me.pelSaid.Dock = System.Windows.Forms.DockStyle.Bottom
        Me.pelSaid.Location = New System.Drawing.Point(0, 332)
        Me.pelSaid.Name = "pelSaid"
        Me.pelSaid.Size = New System.Drawing.Size(392, 34)
        Me.pelSaid.TabIndex = 2
        '
        'btnSend
        '
        Me.btnSend.Location = New System.Drawing.Point(296, 5)
        Me.btnSend.Name = "btnSend"
        Me.btnSend.Size = New System.Drawing.Size(80, 24)
        Me.btnSend.TabIndex = 0
        Me.btnSend.Text = "Send"
        '
        'txtSaid
        '
        Me.txtSaid.Location = New System.Drawing.Point(8, 6)
        Me.txtSaid.Name = "txtSaid"
        Me.txtSaid.Size = New System.Drawing.Size(280, 21)
        Me.txtSaid.TabIndex = 0
        Me.txtSaid.Text = ""
        '
        'txtChatLog
        '
        Me.txtChatLog.Dock = System.Windows.Forms.DockStyle.Top
        Me.txtChatLog.Multiline = True
        Me.txtChatLog.Name = "txtChatLog"
        Me.txtChatLog.ReadOnly = True
        Me.txtChatLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtChatLog.Size = New System.Drawing.Size(392, 328)
        Me.txtChatLog.TabIndex = 3
        Me.txtChatLog.TabStop = False
        Me.txtChatLog.Text = ""
        '
        'sckClient
        '
        Me.sckClient.Enabled = True
        Me.sckClient.Location = New System.Drawing.Point(16, 8)
        Me.sckClient.Name = "sckClient"
        Me.sckClient.OcxState = CType(resources.GetObject("sckClient.OcxState"), System.Windows.Forms.AxHost.State)
        Me.sckClient.Size = New System.Drawing.Size(28, 28)
        Me.sckClient.TabIndex = 4
        '
        'frmMain
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(392, 366)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.sckClient, Me.txtChatLog, Me.pelSaid})
        Me.MaximizeBox = False
        Me.MaximumSize = New System.Drawing.Size(400, 400)
        Me.MinimumSize = New System.Drawing.Size(400, 400)
        Me.Name = "frmMain"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "UDPChat-Client"
        Me.pelSaid.ResumeLayout(False)
        CType(Me.sckClient, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region "Winsock Event"

    Private Sub sckClient_ConnectEvent(ByVal sender As Object, ByVal e As System.EventArgs) Handles sckClient.ConnectEvent
        txtChatLog.AppendText("Connected to " & sckClient.RemoteHost & vbCrLf)
        txtChatLog.AppendText("Socket State is " & sckClient.SocketHandle & vbCrLf)
    End Sub

    Private Sub sckClient_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles sckClient.DataArrival
        Dim objData As Object
        sckClient.GetData(objData, vbString)
        txtChatLog.AppendText("Server: " & CStr(objData) & vbCrLf)
    End Sub

#End Region

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        sckClient.RemoteHost = "127.0.0.1"
        sckClient.RemotePort = 1000
        sckClient.Bind(8000)
        txtChatLog.AppendText("Bind to local Port 8000 and remote port 1000" & vbCrLf)
    End Sub

    Private Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
        If txtSaid.Text.Trim = "" Then
            MsgBox("Please input the chat the contents", MsgBoxStyle.OKOnly, "UDPChat-Client")
            Exit Sub
        End If
        Try
            txtChatLog.AppendText("Client: " & txtSaid.Text & vbCrLf)
            sckClient.SendData(CType(txtSaid.Text, Object))
            txtSaid.Text = ""
        Catch
            MsgBox("Error Occured " & Err.Description & vbCrLf & Err.Number, MsgBoxStyle.OKOnly, "UDPChat-Client")
        End Try
    End Sub

    Private Sub txtSaid_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSaid.KeyPress
        If (e.KeyChar = Chr(13)) Then
            btnSend_Click(sender, New System.EventArgs())
        End If
    End Sub
End Class

如何实现VB.NET 打印控件的使用

system.drawing.printing  命名空间 printerSettings    打印机设置类 PageSettings      页面设置类 PrintPageEventArg...
  • u014366427
  • u014366427
  • 2014年03月26日 10:22
  • 4793

vb.net合伙数据库access(一)——连接数据库

终于在花了一个四个小时的时间完成了数据库与vb.net的合伙连接的第一步连接数据库,在这个过程遇到了问题,而且也是出现了一些刚刚接触的新的词语和观念,很新鲜,不过无论怎么样,最终也是给解决了,还是有一...
  • changyinling520
  • changyinling520
  • 2016年01月05日 19:07
  • 2304

VB.NET中的类和模块

在三层的例子中,我是仿照这C#中的结构来实现的VB.NET版的三层,但是在这之中出现了一个问题,那就是好像C#中只涉及到了各种类的使用,没有用到模块(module),但是在VB中要调用一个类中的字符串...
  • tr1912
  • tr1912
  • 2015年12月20日 10:13
  • 1827

VB.net学习笔记(二十六)线程的坎坷人生

线程可以处于一个或多个状态,由ThreadState枚举表示。使用Thread类中的一些方法后状态会随之变化。枚举成员如下:         线程的生存期如下:   一、线程睡眠    ...
  • dzweather
  • dzweather
  • 2016年06月07日 17:10
  • 3105

VB.net视频打开方法

昨天就把VB.net视频学习资料准备好了,看到材料的名字,以为跟以前视频材料一样,只是一些视频,但是打开后发现什么都有,感觉好乱,不过还好看到了wmv格式的东东,还是看到了希望。先去问了一下雷哥,关于...
  • yiwangxiblog
  • yiwangxiblog
  • 2015年10月04日 20:29
  • 1084

VB.NET使用EXCEL常见操作

http://blog.163.com/shaoyu_lin/blog/static/38024599201210892447762/   首先,须在项目里引用: 添加引用->Com->Mic...
  • seucbh
  • seucbh
  • 2013年09月04日 22:58
  • 4445

VB.net学习笔记(二十一)XML流的读写

System.Xml名称空间支持XML的处理,为了生成和浏览XML文档,使用两种访问方式: 一、读写方式     (一)基于流的方式     特点:速度快,可连续读写,缺点不能作为节点、元素等进...
  • dzweather
  • dzweather
  • 2016年05月22日 12:07
  • 3933

章鱼哥出品_VB.NET 线程委托应用实例 一个简单的例子让你轻松掌握委托的用法

网上有很多关于线程和委托的问题,大家
  • zhangyubishoulin
  • zhangyubishoulin
  • 2014年10月05日 19:07
  • 5141

VB.Net内存管理与内存释放的方法

引言 托管代码的内存管理是自动的,.NET可以保证我们的托管程序在结束时全部释放,这为我们编程人员省去了不少麻烦,我们可以连想都不想怎么去管理内存,反正.NET自己会保证一切。好吧,有道理,有一定的...
  • a316212802
  • a316212802
  • 2014年12月22日 22:52
  • 5855

VB.NET 贪吃蛇 (画圆)

VB.NET 贪吃蛇小游戏效果图如下 所有代码一共300行多一点,非常简单,下面依次介绍一下1.设计思路这个游戏(1)呢,得有个地图(2),有条蛇(3),有个蛋(4),所以一共设计了4个类,然后通过...
  • wf824284257
  • wf824284257
  • 2016年12月15日 19:13
  • 419
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:The UDPChat Source(VB.NET)
举报原因:
原因补充:

(最多只允许输入30个字)