网站建设资讯

NEWS

网站建设资讯

vb.net中网络通讯 vb网络通信

用vb.net实现网络通信

用 vb.net socket通信

吉林网站建设公司创新互联建站,吉林网站设计制作,有大型网站制作公司丰富经验。已为吉林成百上千提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的吉林做网站的公司定做!

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

VB.NET与PHP通讯问题

与语言没有关系, 你需要考虑的是两个系统直接如何通讯. 选一种通讯协议(一般用http协议), 设计好数据结构, 可以是xml或json. 假设选用http协议传输json数据. 你需要做以下工作:

使用php开发一个http接口, 根据参数查询数据库, 把结果转换为json格式后返回. 这是目前最流行的接口格式, 基本上所有语言都可以开发和请求这样的接口, 很多开放平台都是这样设计接口的, 例如微信的这个接口,你可以参考一下 网页链接

使用VB.NET请求这个接口, 获取返回的数据(json格式, 一个特殊的字符串), 使用VB.NET对json数据进行解码

所以,要完成这个工作,你需要知道http接口开发和请求, 以及json编码和解码的相关知识和技能, 祝你好运

VB与VB.NET串口及以太网通迅

关于串口通讯的问题:

很明显,你还不知道(不会、不习惯)使用事件驱动的方式接收数据。

建议你仔细看看串口组件(无论VB6还是VB.net)的OnComm事件,你的问题很容易解决。

关于以太网通讯:

在TCP通讯中端口确实可以重用,你百度一下“TCP端口重用”能查到很多示例。

但绝大多数情况下不推荐端口重用,而应该采取服务器端建立连接池的方法。

或者,干脆不用TCP,用UDP解决也可以。

VB.NET 2003中 想做一个基于TCP/IP的网络通信程序

就可以了,我有一段不太成熟的代码:

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 = "启动服务端 "

TextBox2.AppendText( "服务器已停止! " 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( "服务器已启动! " vbCrLf)

TCPListenerRun = True

Button1.Text = "停止服务端 "

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 = "启动客户端 "

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 = "启动客户端 "

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


文章名称:vb.net中网络通讯 vb网络通信
网站URL:http://njwzjz.com/article/dddsjid.html