重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
你可以根据服务器收到的数据的长度来判断,如果服务器收到的数据长度是0,那么意味着你的客户端程序已经断开了连接。从TCP/IP协议栈的角度来说,就是客户端程序关闭了自己写的这一半连接,向服务器发出了一个FIN。这涉及到TCP的状态迁移,关于这方面的知识,建议你看一下Richard Stevens先生的《TCP/IP 详解》卷一和《Unix网络编程》卷一,上面有详细的解释。
成都创新互联公司自2013年创立以来,是专业互联网技术服务公司,拥有项目成都网站设计、成都做网站、外贸网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元府谷做网站,已为上家服务,为府谷各地企业和个人服务,联系电话:18980820575
关于你的第二个问题,建议你仔细看一下自己的服务器程序代码。服务器程序首先要建立一个监听socket,当有客户端连接上来时,服务器会在一个新socket上接受客户端连接。所以并不存在“乱”的问题。关于这个问题同样推荐你看上面的两本关于网络编程的经典著作。
既然从网上找的实例没问题,也许就是调用的时候没出来或者在检测网卡当中出现了问题。
Dim p As Process = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
p = Process.Start("notepad")
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If p IsNot Nothing AndAlso p.HasExited Then
p = Nothing
MsgBox("程序已退出!")
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Interval = 100
Timer1.Enabled = True
End Sub
Microsoft.VisualBasic.Devices.Network.IsAvailable As Boolean
生成实例后可以通过此属性来获取是否联网的值,True代表已联网,False代表没有联网
Microsoft.VisualBasic.Devices.Network.NetworkAvailabilityChanged(ByVal Object, ByVal Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs)
该事件是在电脑断开/连上互联网是引发的
其中:
e.IsNetworkAvailable
的功能同Microsoft.VisualBasic.Devices.Network.IsAvailable
实例:
Dim netListener As New Microsoft.VisualBasic.Devices.Network '生成实例
If netListener.IsAvailable = True Then
MsgBox("已联网")
Else
MsgBox("未联网")
End If
NetworkAvailabilityChanged实例:
WithEvents netListener As New Microsoft.VisualBasic.Devices.Network '生成实例
Private Sub OnNetworkChanged(ByVal Object, ByVal Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs)Handles netListener.NetworkAvailabilityChanged
If e.IsNetworkAvailable = True Then
MsgBox("已联网")
Else
MsgBox("未联网")
End If
End Sub
‘客户端向这样写:在窗体说放两个文本框、两个标签、两个按钮、一个列表框
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading
Public Class Form1
'网络基础数据流
Private ns As NetworkStream
Private swriter As StreamWriter
Private nsread As StreamReader
Private tcpclient As TcpClient
Private tcpconnected As Boolean = False
Private clisocket As Socket
Private mythread As Thread
'发送
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Me.TextBox2.Text "" Then
swriter.WriteLine(Me.TextBox2.Text)
swriter.Flush()
Me.TextBox2.Text = ""
Else
MessageBox.Show("发送信息不能为空!", "错误提示")
End If
End Sub
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
'建立连连接
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ipremote As IPAddress
Dim tcpclient As TcpClient
Try
ipremote = IPAddress.Parse(Me.TextBox1.Text)
Catch ex As Exception
MessageBox.Show("IP地址不合法!", "错误提示")
End Try
Try
tcpclient = New TcpClient(Me.TextBox1.Text, 8000)
ns = tcpclient.GetStream
swriter = New StreamWriter(ns)
Me.StatusBar1.Text = "已经连接上"
Me.Button2.Enabled = False
Me.Button1.Enabled = True
tcpconnected = True
Catch ex As Exception
MessageBox.Show("无法与远程8000端口建立连接!", "错误提示")
End Try
End Sub
Private Sub listen()
Try
Dim tcplistener As New TcpListener(IPAddress.Parse("127.0.0.1"), 8000)
tcplistener.Start()
tcpclient = tcplistener.AcceptTcpClient
ns = tcpclient.GetStream()
nsread = New StreamReader(ns)
While True
Dim msg As String = nsread.ReadLine
If msg = "stop" Then
tcplistener.Stop()
ns.Close()
nsread.Close()
mythread.Abort()
Else
Dim mytime As String = DateTime.Now.ToShortTimeString
Me.ListBox1.Items.Add(mytime + " " + msg)
End If
End While
Catch ex As System.Security.SecurityException
MessageBox.Show("侦听失败!", "错误")
End Try
End Sub
End Class
是的,是socket套接字编程。
不知道你用什么协议,矩形PLC是用Modbus-tcp协议,附件是矩形PLC基于Modbus-TCP的编程参考文档,可以看看。