用 vb.net socket通信
專注于為中小企業(yè)提供網(wǎng)站制作、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)龍海免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
Dim th As Threading.Thread
2 Dim tcpl As System.Net.Sockets.TcpListener
3
4 Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
5 th = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf MyListen))
6 th.Start()
7 End Sub
8
9 Public Sub SendMessage()Sub SendMessage(ByVal IP As String, ByVal SendMsg As String)
10 Try
11 If IP "" Then
12 Dim tcpc As New System.Net.Sockets.TcpClient(IP, 5656)
13 Dim tcpStream As Net.Sockets.NetworkStream = tcpc.GetStream
14 Dim reqStream As New IO.StreamWriter(tcpStream)
15 reqStream.Write(SendMsg)
16 reqStream.Flush()
17 tcpStream.Close()
18 tcpc.Close()
19 End If
20 Catch ex As Exception
21 MsgBox(ex.Message.ToString)
22 End Try
23 End Sub
24 Private Sub MyListen()Sub MyListen()
25 Try
26 Dim ipAddress As System.Net.IPAddress = System.Net.Dns.Resolve(System.Net.Dns.GetHostName).AddressList(0)
27 tcpl = New System.Net.Sockets.TcpListener(ipAddress, 5656)
28 tcpl.Start()
29 While True
30 Dim s As System.Net.Sockets.Socket = tcpl.AcceptSocket()
31 Dim MyBuffer(1024) As Byte
32 Dim i As Integer
33 i = s.Receive(MyBuffer)
34 If i 0 Then
35 Dim lstrRec As String
36 Dim j As Integer
37 For j = 0 To i - 1
38 TextBox1.Text += Chr(MyBuffer(j)) ","
39 Next
40 End If
41 End While
42 Catch ex As Exception
43 MsgBox(ex.Message.ToString)
44 End Try
45 End Sub
46
47 Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
48 SendMessage("192.168.0.61", TextBox2.Text)
49 End Sub
用VB5 Winsock控件創(chuàng)建TCP/IP通訊程序 隨著Windows 95中文版和Windows NT Server 4.0中文版的流行, Microsoft公司推出了相應(yīng)平臺(tái)上的開發(fā)軟件: Visual Basic 5.0 中文企業(yè) 版。它為Windows環(huán)境下的網(wǎng)絡(luò)開發(fā)提供了強(qiáng)大的工具,Winsock控件就是其中之一。 Winsock控件建立在TCP、UDP協(xié)議的基礎(chǔ)上,完成與遠(yuǎn)程計(jì)算機(jī)的通信。即使對TCP/IP不太熟悉的用戶,使用該控件也可以在十幾分鐘內(nèi)創(chuàng)建一個(gè)簡單的客戶機(jī)/服務(wù)器程序。下面我們對Winsock控件的事件、方法、屬性按其在程序中出現(xiàn)的順序分別作詳細(xì)的介紹,以便更好地理解程序源代碼。
就可以了,我有一段不太成熟的代碼:
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.IO
Public Class Form1
Dim TCPListenerRun, TCPClientRun As Boolean
Dim ControlTCPClient, ServerClient As TcpClient
Dim ControlTCPListener As TcpListener
Dim TCPClientT, TCPListenerT As Thread
Private ControlStream, SControlStream As NetworkStream
Private ControlReadStream, SControlReadStream As StreamReader
Private ControlWriteStream, SControlWriteStream As StreamWriter
'異步委托
Private Delegate Sub InvokeDelegate(ByVal iMessage As String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If TCPListenerRun Then
TCPListenerT.Abort()
ControlTCPListener.Stop()
TCPListenerRun = False
Button1.Text = "啟動(dòng)服務(wù)端 "
TextBox2.AppendText( "服務(wù)器已停止! " vbCrLf)
Else
ControlTCPListener = New TcpListener(IPAddress.Parse( "127.0.0.1 "), 13000)
ControlTCPListener.Start()
TCPListenerT = New Thread(AddressOf Listen)
TCPListenerT.Start()
TextBox2.AppendText( "等待連接... " vbCrLf)
TextBox2.AppendText( "服務(wù)器已啟動(dòng)! " vbCrLf)
TCPListenerRun = True
Button1.Text = "停止服務(wù)端 "
End If
Catch ex As Exception
TextBox2.AppendText(ex.Message vbCrLf)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
If TCPClientRun Then
TCPClientT.Abort()
ControlTCPClient.Close()
TCPClientRun = False
Button2.Text = "啟動(dòng)客戶端 "
Else
ControlTCPClient = New TcpClient(New IPEndPoint(IPAddress.Parse( "127.0.0.1 "), 13001))
ControlTCPClient.Connect(New IPEndPoint(IPAddress.Parse( "127.0.0.1 "), 13000))
TCPClientRun = True
Button2.Text = "停止客戶端 "
TCPClientT = New Thread(AddressOf Receive)
TCPClientT.Start()
ControlStream = ControlTCPClient.GetStream()
ControlReadStream = New StreamReader(ControlStream)
ControlWriteStream = New StreamWriter(ControlStream)
End If
Catch ex As SocketException
'If ex.ErrorCode = 10048 Then
ControlTCPClient.Close()
'End If
TCPClientRun = False
Button2.Text = "啟動(dòng)客戶端 "
TextBox2.AppendText(ex.Message vbCrLf)
End Try
End Sub
Private Sub Listen()
ServerClient = ControlTCPListener.AcceptTcpClient
TextBox2.BeginInvoke(New InvokeDelegate(AddressOf InvokeMethod_Receive), "成功連接... ")
SControlStream = ServerClient.GetStream()
SControlReadStream = New StreamReader(SControlStream)
SControlWriteStream = New StreamWriter(SControlStream)
While True
TextBox2.BeginInvoke(New InvokeDelegate(AddressOf InvokeMethod_Receive), SControlReadStream.ReadLine())
End While
End Sub
Private Sub Receive()
While True
TextBox2.BeginInvoke(New InvokeDelegate(AddressOf InvokeMethod_Receive), ControlReadStream.ReadLine())
'Console.WriteLine(ControlTCPClient.Available)
End While
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TCPListenerRun = False
TCPClientRun = False
End Sub
Private Sub InvokeMethod_Receive(ByVal iMessage As String)
TextBox2.AppendText(iMessage vbCrLf)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
ControlWriteStream.WriteLine(TextBox1.Text)
ControlWriteStream.Flush()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
SControlWriteStream.WriteLine(TextBox3.Text)
SControlWriteStream.Flush()
End Sub
End Class