读书人

要个VB.NET的Socket施用例子

发布时间: 2011-12-23 23:32:01 作者: rapoo

要个VB.NET的Socket使用例子
要个VB.NET的Socket使用例子,最简单的就可以了
能在两台能互通信!
如果能用的网页也可以,分不够再加!!


[解决办法]
以前写的,不知道对你有用没?
TextBox1 为要连接的ip
TextBox2 为呢称
RichTextBox1 为聊天内容
RichTextBox2 为要发送的内容
Button1 发送

Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Public Class Form1
Dim MyTcpListener As TcpListener
Dim MyListenThread As Thread

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'关闭SOCKET
Try
If Me.MyTcpListener IsNot Nothing Then
'关闭监听器
Me.MyTcpListener.Stop()
End If
If Me.MyListenThread IsNot Nothing Then
'如果线程还处于运行状态就关闭它
If Me.MyListenThread.ThreadState <> ThreadState.Running Then
Me.MyListenThread.Abort()
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "信息提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
e.Cancel = False
End Sub


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' 开始监听()
'创建监听线程
Me.MyListenThread = New Thread(AddressOf StartListen)
'启动线程
Me.MyListenThread.Start()
End Sub
Private Sub StartListen()
MyTcpListener = New TcpListener(888)
'开始监听
MyTcpListener.Start()
'While (True)
'获取TcpClient
Dim MyTcpClient As TcpClient = MyTcpListener.AcceptTcpClient()
Dim MyStream As NetworkStream = MyTcpClient.GetStream()
Dim MyBytes(1024) As Byte
Dim MyBytesRead As Integer = MyStream.Read(MyBytes, 0, MyBytes.Length)
Dim MyMessage As String = System.Text.UnicodeEncoding.GetEncoding( "gb2312 ").GetString(MyBytes, 0, MyBytesRead)
Me.RichTextBox2.AppendText(MyMessage)
'End While

End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If (Me.TextBox1.Text.Length < 1 Or Me.TextBox2.Text.Length < 1 Or Me.RichTextBox1.Text.Length < 1) Then
Exit Sub
End If
Try
Dim MyMessage As String = "消息来自( " + Me.TextBox2.Text + "): " + Me.RichTextBox1.Text + Chr(10) + Chr(13)
'根据目标计算机地址建立连接
Dim MyTcpClient As TcpClient = New TcpClient(Me.TextBox1.Text, 888)
'获得用于网络访问的数据流
Dim MyTcpStream As Net.Sockets.NetworkStream = MyTcpClient.GetStream()
Dim MyStream As IO.StreamWriter = New IO.StreamWriter(MyTcpStream, System.Text.UnicodeEncoding.GetEncoding( "gb2312 "))
'将字符串写入流
MyStream.Write(MyMessage)
'将缓冲数据写入基础流
MyStream.Flush()
'关闭网络流
MyStream.Close()
'MyTcpClient.Close()
Me.RichTextBox2.AppendText( "发送: " & Me.RichTextBox1.Text)
Me.RichTextBox1.Clear()
Catch ex As Exception
MessageBox.Show(ex.Message, "信息提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
End Class

------解决方案--------------------


给个监视封包的

Private tmpLocalIP As String
Public Function MyReceive(ByVal LocalIP As String, ByVal LocalPort As Integer) As Boolean
tmpLocalIP = LocalIP
tmpSourceSocket = New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Raw, Net.Sockets.ProtocolType.IP)
tmpSourceSocket.Bind(New Net.IPEndPoint(Net.IPAddress.Parse(LocalIP), LocalPort))
Dim tmpIN() As Byte = {1, 0, 0, 0}
Dim tmpOUT(3) As Byte
tmpSourceSocket.IOControl(&H98000001, tmpIN, tmpOUT)
Dim tmpReceiveBuffer(4095) As Byte
ReceiveMutex = New System.Threading.Mutex(False, LocalIP & LocalPort)
Dim tmpReceiveThread As New Threading.Thread(AddressOf HideRecieve)
tmpReceiveThread.Start()
End Function

Private tmpLast20DestinAddCounter As Integer = 0
Private Sub HideRecieve()
If Not ReceiveMutex Is Nothing Then
ReceiveMutex.WaitOne()
Try
While (StopReceive = False)
Try
ReDim tmpReceiveBuffer(4095)
tmpSourceSocket.Receive(tmpReceiveBuffer)
Dim tmpReceiveLength As Int16 = CInt(tmpReceiveBuffer(2)) < < 8 Or tmpReceiveBuffer(3)
ReDim Preserve tmpReceiveBuffer(tmpReceiveLength - 1)
If tmpReceiveBuffer(16) & ". " & tmpReceiveBuffer(17) & ". " & tmpReceiveBuffer(18) & ". " & tmpReceiveBuffer(19) = tmpLocalIP Then
Dim tmpSourcePort As Integer = CInt(tmpReceiveBuffer((tmpReceiveBuffer(0) And &HF) * 4)) < < 8 Or tmpReceiveBuffer((tmpReceiveBuffer(0) And &HF) * 4 + 1)
Dim tmpTargetPort As Integer = CInt(tmpReceiveBuffer((tmpReceiveBuffer(0) And &HF) * 4 + 2)) < < 8 Or tmpReceiveBuffer((tmpReceiveBuffer(0) And &HF) * 4 + 3)
tmpLast20DestinAdd(tmpLast20DestinAddCounter, 0) = tmpReceiveBuffer(12) & ". " & tmpReceiveBuffer(13) & ". " & tmpReceiveBuffer(14) & ". " & tmpReceiveBuffer(15) & ": " & tmpSourcePort & ": " & tmpTargetPort
tmpLast20DestinAdd(tmpLast20DestinAddCounter, 1) = CInt(tmpReceiveBuffer(2)) < < 8 Or tmpReceiveBuffer(3)
tmpLast20DestinAddCounter += 1
If tmpLast20DestinAddCounter > 19 Then
tmpLast20DestinAddCounter = 0
End If
End If
Catch ex As Exception

End Try
End While

Catch ex As Exception

Finally
ReceiveMutex.ReleaseMutex()
End Try
End If
End Sub

读书人网 >VB Dotnet

热点推荐