1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
private _TCP As TcpClient
private _socket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
private stream As NetworkStream
private Dt As [Byte]()
'********************************************************************************************
'*************************** Connexion ***************************
'********************************************************************************************
Function connection(ByVal LaCom() As String, ByVal Datt As [Byte]()) As Boolean
Dim Bool As Boolean = False
Dim LaLigne As String
'
Try
System.Threading.Thread.Sleep(200)
While Not Bool
Dt = Datt
_TCP = New TcpClient
_TCP.Connect(LaCom(0), CInt(LaCom(1)))
stream = _TCP.GetStream()
stream.ReadTimeout = 1000
stream.WriteTimeout = 1000
_TCP.ReceiveTimeout = 10000
_TCP.SendTimeout = 10000
If _TCP.Connected Then
stream.Write(Dt, 0, Dt.Length)
LaLigne = Reception_Eth()
Dim L As String = Mid(LaLigne, 1, 1)
If Not LaLigne.Contains("ERROR") Then
Bool = True
ElseIf LaLigne.Contains("ERROR") Then
System.Threading.Thread.Sleep(500)
_TCP.Close()
Return False
Else
System.Threading.Thread.Sleep(500)
_TCP.Close()
End If
'
End If
End While
'
Catch ex As Exception
_TCP.Close()
Return False
'
End Try
Return True
End Function
Public Function Reception_Eth() As String
Dim Ligne As String = ""
Dim Code As String = ""
Dim NbByte As Int32 = 0
Dim Code2 As String = ""
' Dim CarArr As String = Val(6)
Dim bytes As Int32 = 2
Dim MaLigne As String = "0"
Dim FinBytes As Byte = 0
Dim Dt2 As [Byte]() = New [Byte](256) {}
Dim j As Int32
'
Try
System.Threading.Thread.Sleep(200)
'
While Dt2(bytes - 1) <> 6
j = 0
Dt = New [Byte](256) {}
bytes = stream.Read(Dt, 0, Dt.Length)
For i As Integer = NbByte To bytes
Dt2(i) = Dt(j)
j = j + 1
Next i
'
System.Threading.Thread.Sleep(300)
End While
'
Code = DecoupReceptionTCP(Dt, Ligne)
Code2 = Checksum(Ligne)
If Code <> Code2 Then
Return "ERROR"
End If
Return Ligne
Catch ex As Exception
MsgBox(ex.Message)
Return "ERROR"
End Try
End Function |
Partager