vb.net服务器通信,vbnet网络通讯
用vb.net实现网络通信
用 vb.net socket通信
创新互联专注于企业网络营销推广、网站重做改版、伊春网站定制设计、自适应品牌网站建设、H5高端网站建设、商城网站开发、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为伊春等各大城市提供网站开发制作服务。
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与VB.NET串口及以太网通迅
关于串口通讯的问题:
很明显,你还不知道(不会、不习惯)使用事件驱动的方式接收数据。
建议你仔细看看串口组件(无论VB6还是VB.net)的OnComm事件,你的问题很容易解决。
关于以太网通讯:
在TCP通讯中端口确实可以重用,你百度一下“TCP端口重用”能查到很多示例。
但绝大多数情况下不推荐端口重用,而应该采取服务器端建立连接池的方法。
或者,干脆不用TCP,用UDP解决也可以。
有关VB.NET socket通信断开后重连问题
这个很正常,因为服务器(对方)还没对客户端(本地)做Close动作。还有一个说法是本地端控件的缓存还存在着与服务器对应Handle,只是State属性状态不是sckConnected而已。继续进行测试,你也会发现只要超过60秒通信闲置,也会自动Not sckConnected。
TCP连线,可以使用Load 控件数组进行尝试连线,当控件Close后就Unload,这可以取代必须关闭窗体、重新打开连接的问题。
vb.net 做的通信程序,设备掉电后出错:无法从传输连接中读取数据: 远程主机强迫关闭了一个现有的连接
看下这个异常的类型 在捕捉异常的时候处理为关闭并重新打开端口监听(这个看你自己需求) 然后在socketException下面再catch其它exception 进行弹出错误提示之类的操作 就可以了
VB.NET 服务器与客户端数据交互问题?
客户端简单代码:
Imports System.Net.Sockets
Imports System.IO
Private Output As NetworkStream
Private Writer As BinaryWriter
Private Reader As BinaryReader
Dim StrSend As String
Dim client As New TcpClient '
client.Connect(servIP, 2006)
Output = client.GetStream
Writer = New BinaryWriter(Output)
Reader = New BinaryReader(Output)
StrSend = "字符串"
System.Threading.Thread.Sleep(100)
Writer.Write(StrSend)
Try
Message = Reader.ReadString
If Message = "OK" Then
LBinfo.Text = "成功!" '测试
Else
LBinfo.Text = "失败!"
End If
System.Threading.Thread.Sleep(100)
Catch ex As Exception
MessageBox.Show("Client Application Closing!")
Finally
Writer.Close()
Reader.Close()
Output.Close()
client.Close()
End Try
服务器端
用线程
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading
Imports System.Windows.Forms
Private Connection As Socket
Private readThread As Thread
Private SocketStream As NetworkStream
Private Writer As BinaryWriter
Private Reader As BinaryReader
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
readThread = New Thread(AddressOf RunServer)
readThread.Start()
End Sub
Public Sub RunServer()
Dim Listener As TcpListener
Try
Listener = New TcpListener(Net.IPAddress.Parse("192.168.1.16"), 2006)
Listener.Start()
While True
Connection = Listener.AcceptSocket 'accept an incoming connection
SocketStream = New NetworkStream(Connection) 'create networkstream object associated with socket
Writer = New BinaryWriter(SocketStream) 'create object for transferring data across stream
Reader = New BinaryReader(SocketStream) 'read string data sent from client
Try
Do
strIncept = Reader.ReadString
strIncept = DataDisp(strIncept) '此处插入数据处理程序()
SendInfoToClient(strIncept) 'send to client
Loop While Connection.Connected
Catch ex As Exception
MsgBox(ex.ToString)
Finally
Writer.Close()
Reader.Close()
SocketStream.Close()
End Try
End While
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub SendInfoToClient(ByVal Info As String)
'Send to client
Try
If Connection.Connected Then
Writer.Write(Info)
Connection.Close()
End If
Catch ex As SocketException
MessageBox.Show(ex.ToString)
End Try
End Sub
整个过程都有了
用VB.NET编程,实现一个客户端和一个服务器能连接并通讯,急啊
自学还是必要的
你去看看TcpServerChannel TcpClient类
msdn链接地址
网站题目:vb.net服务器通信,vbnet网络通讯
分享路径:http://scjbc.cn/article/hogdeo.html