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
1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
2Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmMain))
3Me.txtChatLog = New System.Windows.Forms.TextBox()
4Me.pelSaid = New System.Windows.Forms.Panel()
5Me.btnSend = New System.Windows.Forms.Button()
6Me.txtSaid = New System.Windows.Forms.TextBox()
7Me.sckServer = New AxMSWinsockLib.AxWinsock()
8Me.pelSaid.SuspendLayout()
9CType(Me.sckServer, System.ComponentModel.ISupportInitialize).BeginInit()
10Me.SuspendLayout()
11'
12'txtChatLog
13'
14Me.txtChatLog.Dock = System.Windows.Forms.DockStyle.Top
15Me.txtChatLog.Multiline = True
16Me.txtChatLog.Name = "txtChatLog"
17Me.txtChatLog.ReadOnly = True
18Me.txtChatLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
19Me.txtChatLog.Size = New System.Drawing.Size(392, 328)
20Me.txtChatLog.TabIndex = 0
21Me.txtChatLog.TabStop = False
22Me.txtChatLog.Text = ""
23'
24'pelSaid
25'
26Me.pelSaid.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSend, Me.txtSaid})
27Me.pelSaid.Dock = System.Windows.Forms.DockStyle.Bottom
28Me.pelSaid.Location = New System.Drawing.Point(0, 332)
29Me.pelSaid.Name = "pelSaid"
30Me.pelSaid.Size = New System.Drawing.Size(392, 34)
31Me.pelSaid.TabIndex = 1
32'
33'btnSend
34'
35Me.btnSend.Location = New System.Drawing.Point(296, 5)
36Me.btnSend.Name = "btnSend"
37Me.btnSend.Size = New System.Drawing.Size(80, 24)
38Me.btnSend.TabIndex = 0
39Me.btnSend.Text = "Send"
40'
41'txtSaid
42'
43Me.txtSaid.Location = New System.Drawing.Point(8, 6)
44Me.txtSaid.Name = "txtSaid"
45Me.txtSaid.Size = New System.Drawing.Size(280, 21)
46Me.txtSaid.TabIndex = 0
47Me.txtSaid.Text = ""
48'
49'sckServer
50'
51Me.sckServer.Enabled = True
52Me.sckServer.Location = New System.Drawing.Point(16, 8)
53Me.sckServer.Name = "sckServer"
54Me.sckServer.OcxState = CType(resources.GetObject("sckServer.OcxState"), System.Windows.Forms.AxHost.State)
55Me.sckServer.Size = New System.Drawing.Size(28, 28)
56Me.sckServer.TabIndex = 2
57'
58'frmMain
59'
60Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
61Me.ClientSize = New System.Drawing.Size(392, 366)
62Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.sckServer, Me.pelSaid, Me.txtChatLog})
63Me.MaximizeBox = False
64Me.MaximumSize = New System.Drawing.Size(400, 400)
65Me.MinimumSize = New System.Drawing.Size(400, 400)
66Me.Name = "frmMain"
67Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
68Me.Text = "UDPChat-Server"
69Me.pelSaid.ResumeLayout(False)
70CType(Me.sckServer, System.ComponentModel.ISupportInitialize).EndInit()
71Me.ResumeLayout(False)
72
73End Sub
74
75#End Region
76
77#Region "Winsock Event"
78
79Private Sub sckServer_ConnectionRequest(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles sckServer.ConnectionRequest
80txtChatLog.AppendText("Request of Connecting from " & e.requestID & vbCrLf)
81txtChatLog.AppendText("Request Accepted...." & vbCrLf)
82sckServer.Accept(e.requestID)
83sckServer.SendData("Connection Accepted...." & vbCrLf)
84End Sub
85
86Private Sub sckServer_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles sckServer.DataArrival
87Dim objData As Object
88sckServer.GetData(objData, vbString)
89txtChatLog.AppendText("Client: " & CStr(objData) & vbCrLf)
90End Sub
91
92#End Region
93
94Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
95sckServer.RemoteHost = "127.0.0.1"
96sckServer.RemotePort = 8000
97sckServer.Bind(1000)
98txtChatLog.AppendText("Binded to localhost at 1000 and Remote port 8000" & vbCrLf)
99End Sub
100
101Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
102If txtSaid.Text.Trim = "" Then
103MsgBox("Please input the chat the contents", MsgBoxStyle.OKOnly, "UDPChat-Server")
104Exit Sub
105End If
106
107Try
108txtChatLog.AppendText("Server: " & txtSaid.Text & vbCrLf)
109sckServer.SendData(CType(txtSaid.Text, Object))
110txtSaid.Text = ""
111Catch
112MsgBox("Error Occured " & Err.Description & vbCrLf & Err.Number, MsgBoxStyle.OKOnly, "UDPChat-Server")
113End Try
114End Sub
115
116Private Sub txtSaid_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSaid.KeyPress
117If (e.KeyChar = Chr(13)) Then
118btnSend_Click(sender, New System.EventArgs())
119End If
120End Sub
121End Class
122
123
124
125
126The UDPChat-Client Source(VB.NET)
127
128
129
130
131'I use Winsock Control SP5(VB6)
132
133Public Class frmMain
134Inherits System.Windows.Forms.Form
135
136#Region " Windows 窗体设计器生成的代码 "
137
138Public Sub New()
139MyBase.New()
140
141'该调用是 Windows 窗体设计器所必需的。
142InitializeComponent()
143
144'在 InitializeComponent() 调用之后添加任何初始化
145
146End Sub
147
148'窗体重写处置以清理组件列表。
149Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
150If disposing Then
151If Not (components Is Nothing) Then
152components.Dispose()
153End If
154End If
155MyBase.Dispose(disposing)
156End Sub
157
158'Windows 窗体设计器所必需的
159Private components As System.ComponentModel.IContainer
160
161'注意:以下过程是 Windows 窗体设计器所必需的
162'可以使用 Windows 窗体设计器修改此过程。
163'不要使用代码编辑器修改它。
164Friend WithEvents pelSaid As System.Windows.Forms.Panel
165Friend WithEvents btnSend As System.Windows.Forms.Button
166Friend WithEvents txtSaid As System.Windows.Forms.TextBox
167Friend WithEvents txtChatLog As System.Windows.Forms.TextBox
168Friend WithEvents sckClient As AxMSWinsockLib.AxWinsock
169<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
170Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmMain))
171Me.pelSaid = New System.Windows.Forms.Panel()
172Me.btnSend = New System.Windows.Forms.Button()
173Me.txtSaid = New System.Windows.Forms.TextBox()
174Me.txtChatLog = New System.Windows.Forms.TextBox()
175Me.sckClient = New AxMSWinsockLib.AxWinsock()
176Me.pelSaid.SuspendLayout()
177CType(Me.sckClient, System.ComponentModel.ISupportInitialize).BeginInit()
178Me.SuspendLayout()
179'
180'pelSaid
181'
182Me.pelSaid.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSend, Me.txtSaid})
183Me.pelSaid.Dock = System.Windows.Forms.DockStyle.Bottom
184Me.pelSaid.Location = New System.Drawing.Point(0, 332)
185Me.pelSaid.Name = "pelSaid"
186Me.pelSaid.Size = New System.Drawing.Size(392, 34)
187Me.pelSaid.TabIndex = 2
188'
189'btnSend
190'
191Me.btnSend.Location = New System.Drawing.Point(296, 5)
192Me.btnSend.Name = "btnSend"
193Me.btnSend.Size = New System.Drawing.Size(80, 24)
194Me.btnSend.TabIndex = 0
195Me.btnSend.Text = "Send"
196'
197'txtSaid
198'
199Me.txtSaid.Location = New System.Drawing.Point(8, 6)
200Me.txtSaid.Name = "txtSaid"
201Me.txtSaid.Size = New System.Drawing.Size(280, 21)
202Me.txtSaid.TabIndex = 0
203Me.txtSaid.Text = ""
204'
205'txtChatLog
206'
207Me.txtChatLog.Dock = System.Windows.Forms.DockStyle.Top
208Me.txtChatLog.Multiline = True
209Me.txtChatLog.Name = "txtChatLog"
210Me.txtChatLog.ReadOnly = True
211Me.txtChatLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
212Me.txtChatLog.Size = New System.Drawing.Size(392, 328)
213Me.txtChatLog.TabIndex = 3
214Me.txtChatLog.TabStop = False
215Me.txtChatLog.Text = ""
216'
217'sckClient
218'
219Me.sckClient.Enabled = True
220Me.sckClient.Location = New System.Drawing.Point(16, 8)
221Me.sckClient.Name = "sckClient"
222Me.sckClient.OcxState = CType(resources.GetObject("sckClient.OcxState"), System.Windows.Forms.AxHost.State)
223Me.sckClient.Size = New System.Drawing.Size(28, 28)
224Me.sckClient.TabIndex = 4
225'
226'frmMain
227'
228Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
229Me.ClientSize = New System.Drawing.Size(392, 366)
230Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.sckClient, Me.txtChatLog, Me.pelSaid})
231Me.MaximizeBox = False
232Me.MaximumSize = New System.Drawing.Size(400, 400)
233Me.MinimumSize = New System.Drawing.Size(400, 400)
234Me.Name = "frmMain"
235Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
236Me.Text = "UDPChat-Client"
237Me.pelSaid.ResumeLayout(False)
238CType(Me.sckClient, System.ComponentModel.ISupportInitialize).EndInit()
239Me.ResumeLayout(False)
240
241End Sub
242
243#End Region
244
245#Region "Winsock Event"
246
247Private Sub sckClient_ConnectEvent(ByVal sender As Object, ByVal e As System.EventArgs) Handles sckClient.ConnectEvent
248txtChatLog.AppendText("Connected to " & sckClient.RemoteHost & vbCrLf)
249txtChatLog.AppendText("Socket State is " & sckClient.SocketHandle & vbCrLf)
250End Sub
251
252Private Sub sckClient_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles sckClient.DataArrival
253Dim objData As Object
254sckClient.GetData(objData, vbString)
255txtChatLog.AppendText("Server: " & CStr(objData) & vbCrLf)
256End Sub
257
258#End Region
259
260Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
261sckClient.RemoteHost = "127.0.0.1"
262sckClient.RemotePort = 1000
263sckClient.Bind(8000)
264txtChatLog.AppendText("Bind to local Port 8000 and remote port 1000" & vbCrLf)
265End Sub
266
267Private Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
268If txtSaid.Text.Trim = "" Then
269MsgBox("Please input the chat the contents", MsgBoxStyle.OKOnly, "UDPChat-Client")
270Exit Sub
271End If
272Try
273txtChatLog.AppendText("Client: " & txtSaid.Text & vbCrLf)
274sckClient.SendData(CType(txtSaid.Text, Object))
275txtSaid.Text = ""
276Catch
277MsgBox("Error Occured " & Err.Description & vbCrLf & Err.Number, MsgBoxStyle.OKOnly, "UDPChat-Client")
278End Try
279End Sub
280
281Private Sub txtSaid_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSaid.KeyPress
282If (e.KeyChar = Chr(13)) Then
283btnSend_Click(sender, New System.EventArgs())
284End If
285End Sub
286End Class</system.diagnostics.debuggerstepthrough()></system.diagnostics.debuggerstepthrough()>