标题:谁有VB.NET即时通信的制作源码?
取消只看楼主
jimewwh
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-8-13
 问题点数:0 回复次数:2 
谁有VB.NET即时通信的制作源码?
即时通信的制作源码?
搜索更多相关主题的帖子: NET 源码 通信 制作 
2007-08-13 10:28
jimewwh
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-8-13
得分:0 

大家帮我看一下问题出在哪里?


源码如下:
Imports System.Drawing
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Public Class frmMain
Inherits System.Windows.Forms.Form

#Region " Windows Form 設計工具產生的程式碼 "

Public Sub New()
MyBase.New()

'此為 Windows Form 設計工具所需的呼叫。
InitializeComponent()

'在 InitializeComponent() 呼叫之後加入所有的初始設定

End Sub

'Form 覆寫 Dispose 以清除元件清單。
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 Form 設計工具的必要項
Private components As System.ComponentModel.IContainer

'注意: 以下為 Windows Form 設計工具所需的程序
'您可以使用 Windows Form 設計工具進行修改。
'請勿使用程式碼編輯器來修改這些程序。
Friend WithEvents txtMessage As System.Windows.Forms.TextBox
Friend WithEvents txtInput As System.Windows.Forms.TextBox
Friend WithEvents btnConnect As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents txtHost As System.Windows.Forms.TextBox
Friend WithEvents txtPortSend As System.Windows.Forms.TextBox
Friend WithEvents txtPortReceive As System.Windows.Forms.TextBox
Friend WithEvents btnUDPStart As System.Windows.Forms.Button
Friend WithEvents btnShutDown As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.txtMessage = New System.Windows.Forms.TextBox
Me.txtInput = New System.Windows.Forms.TextBox
Me.btnConnect = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.txtHost = New System.Windows.Forms.TextBox
Me.txtPortSend = New System.Windows.Forms.TextBox
Me.txtPortReceive = New System.Windows.Forms.TextBox
Me.btnUDPStart = New System.Windows.Forms.Button
Me.btnShutDown = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'txtMessage
'
Me.txtMessage.Location = New System.Drawing.Point(8, 120)
Me.txtMessage.Multiline = True
Me.txtMessage.Name = "txtMessage"
Me.txtMessage.ReadOnly = True
Me.txtMessage.Size = New System.Drawing.Size(408, 296)
Me.txtMessage.TabIndex = 0
'
'txtInput
'
Me.txtInput.Location = New System.Drawing.Point(8, 424)
Me.txtInput.Multiline = True
Me.txtInput.Name = "txtInput"
Me.txtInput.Size = New System.Drawing.Size(408, 72)
Me.txtInput.TabIndex = 1
'
'btnConnect
'
Me.btnConnect.Location = New System.Drawing.Point(240, 88)
Me.btnConnect.Name = "btnConnect"
Me.btnConnect.Size = New System.Drawing.Size(168, 23)
Me.btnConnect.TabIndex = 2
Me.btnConnect.Text = "TCP Mode"
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(8, 10)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(149, 12)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Remote Host(IP or Name):"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(8, 34)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(149, 12)
Me.Label2.TabIndex = 4
Me.Label2.Text = "Port(Sending Message):"
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(8, 58)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(161, 12)
Me.Label3.TabIndex = 5
Me.Label3.Text = "Port(Receiving Message):"
'
'txtHost
'
Me.txtHost.Location = New System.Drawing.Point(168, 8)
Me.txtHost.Name = "txtHost"
Me.txtHost.Size = New System.Drawing.Size(240, 21)
Me.txtHost.TabIndex = 6
'
'txtPortSend
'
Me.txtPortSend.Location = New System.Drawing.Point(168, 32)
Me.txtPortSend.Name = "txtPortSend"
Me.txtPortSend.Size = New System.Drawing.Size(240, 21)
Me.txtPortSend.TabIndex = 7
'
'txtPortReceive
'
Me.txtPortReceive.Location = New System.Drawing.Point(168, 56)
Me.txtPortReceive.Name = "txtPortReceive"
Me.txtPortReceive.Size = New System.Drawing.Size(240, 21)
Me.txtPortReceive.TabIndex = 8
'
'btnUDPStart
'
Me.btnUDPStart.Location = New System.Drawing.Point(8, 88)
Me.btnUDPStart.Name = "btnUDPStart"
Me.btnUDPStart.Size = New System.Drawing.Size(104, 23)
Me.btnUDPStart.TabIndex = 9
Me.btnUDPStart.Text = "UDP Mode"
'
'btnShutDown
'
Me.btnShutDown.Enabled = False
Me.btnShutDown.Location = New System.Drawing.Point(112, 88)
Me.btnShutDown.Name = "btnShutDown"
Me.btnShutDown.Size = New System.Drawing.Size(104, 23)
Me.btnShutDown.TabIndex = 10
Me.btnShutDown.Text = "Shut Down"
'
'frmMain
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(424, 502)
Me.Controls.Add(Me.btnShutDown)
Me.Controls.Add(Me.btnUDPStart)
Me.Controls.Add(Me.txtPortReceive)
Me.Controls.Add(Me.txtPortSend)
Me.Controls.Add(Me.txtHost)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.txtInput)
Me.Controls.Add(Me.txtMessage)
Me.Controls.Add(Me.btnConnect)
Me.Name = "frmMain"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
Me.Text = "Chat Demo"
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub

#End Region
Private threadReceive As New Thread(AddressOf ReceiveMsg)
Private threadUDPReceive As New Thread(AddressOf ReceiveUDPMsg)
Dim socketSend As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim sListener As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)


Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
Dim server As IPHostEntry = Dns.Resolve(txtHost.Text)
Dim ipServer As New IPEndPoint(server.AddressList(0), txtPortSend.Text)
threadReceive.Start()
socketSend.Connect(ipServer)
txtMessage.AppendText("Remote Host Connected" & vbCrLf)
btnConnect.Enabled = False
End Sub

Private Sub ReceiveMsg()
Dim host As IPHostEntry = Dns.Resolve(txtHost.Text)
Dim ipHost As New IPEndPoint(host.AddressList(0), txtPortReceive.Text)
Dim sListener As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
sListener.Bind(ipHost)
sListener.Listen(10)
txtMessage.AppendText("Local Server Mode Started" & vbCrLf)
Dim sHandler As Socket = sListener.Accept
Do
Dim buffer(1024) As Byte
sHandler.Receive(buffer)
Dim msg As String = Encoding.ASCII.GetString(buffer)
txtMessage.AppendText(vbCrLf & "[" & Now & "] Received" & vbCrLf & msg & vbCrLf)
Loop
End Sub


Private Sub txtInput_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtInput.KeyUp
If e.KeyCode = Keys.Enter Then
'Dim msg As String
'msg = txtInput.Text.Trim
'socketSend.Send(Encoding.ASCII.GetBytes(msg.ToCharArray))
'txtInput.Text = ""
'txtMessage.AppendText(vbCrLf & "[" & Now & "] Sent" & vbCrLf & msg & vbCrLf)
Dim d1 As DateTime = Date.Now()
Dim server As IPHostEntry = Dns.Resolve(txtHost.Text)
Dim ipServer As New IPEndPoint(server.AddressList(0), 5555)
Dim socketSend As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)

Dim msg As String
msg = txtInput.Text
txtInput.Text = ""
txtMessage.AppendText(vbCrLf & "[" & Now & "] Sent" & vbCrLf & msg & vbCrLf)
socketSend.SendTo(Encoding.UTF8.GetBytes(msg.ToCharArray), ipServer)
Dim t As Integer = DateDiff(DateInterval.Second, d1, Date.Now())
txtMessage.AppendText(t)
End If
End Sub

Private Sub ReceiveUDPMsg()
Dim host As IPHostEntry = Dns.Resolve(Dns.GetHostName)
Dim ipHost As New IPEndPoint(host.AddressList(0), 5555)
sListener.Bind(ipHost)
Do
Dim ipRemote As New IPEndPoint(IPAddress.Any, 5555)
Dim buffer(1024) As Byte
sListener.ReceiveFrom(buffer, ipRemote)
Dim msg As String = Encoding.UTF8.GetString(buffer)
txtMessage.AppendText(vbCrLf & "[" & Now & "] Received From " & ipRemote.Address.ToString & "(" & Dns.GetHostByAddress(ipRemote.Address).HostName & ")" & ipRemote.Port & vbCrLf & msg & vbCrLf)
Loop
End Sub

Private Sub btnUDPStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUDPStart.Click
threadUDPReceive.Start()
txtMessage.AppendText("Local Host Receive Engine Stated" & vbCrLf)
btnShutDown.Enabled = True
btnUDPStart.Enabled = False
End Sub

Private Sub btnShutDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShutDown.Click
threadUDPReceive.Abort() 'must abort thread first
sListener.Close() 'and then close socket!!!!
btnShutDown.Enabled = False
btnUDPStart.Enabled = True
End Sub

Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
threadUDPReceive.Abort()
sListener.Close()

End Sub
end class

[此贴子已经被作者于2007-8-13 11:21:35编辑过]

2007-08-13 11:18
jimewwh
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-8-13
得分:0 
没有人知道吗?????????
2007-08-13 14:14



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-162269-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.051261 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved