Salut à tous,
je suis en recherche d'un exemple complet d'un client/Serveur de pipe nomme, je rencontre des problémes coté client à ouvrir la pipe, je recoit toujours le handle 0. En gros c'est surtout la partie cliente qui m'interresse.
merci à tous de votre aide,
Voici mon code actuel:
#######################################
Cote Client
######################################
Imports LibrairieBasique
Imports LibrairieBasique.LibrairiePipe
Imports System.Threading
Public Class EmissionEvenement_PIPE
Dim log As Log
Private hPipe As Integer
Dim res As Integer
Dim Message() As Byte
Dim TailleMessage() As Byte
Dim cbnCount As Integer
Dim errorcode As Integer
Dim TheadConnectionServeur As Thread
Dim Connecte As Boolean
Sub New(ByVal log As Log)
Me.log = log
Connecte = False
TheadConnectionServeur = New Thread(AddressOf GestionconnectionServeur)
DemmarerAnalyse()
End Sub
Private Sub GestionconnectionServeur()
While ConnectionServeur() = False
TheadConnectionServeur.Sleep(100)
End While
Connecte = True
End Sub
Public Sub DemmarerAnalyse()
TheadConnectionServeur.Start()
End Sub
Public Sub ArreterAnalyse()
res = CloseHandle(pipeName)
End Sub
Private Function ConnectionServeur()
Try
Dim result As Long
log.debug("CreateFile")
result = CreateFile(pipeName, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
If result <> -1 Then
hPipe = res
log.debug("pipe numero : " & hPipe.ToString)
'log.debug("ConnectNamedPipe")
'result = ConnectNamedPipe(hPipe, 0)
'If result <> -1 Then
'log.erreur("ConnectionServeur : ConnectNamedPipe")
'Return False
'End If
Connecte = True
Return True
Else
If GetLastError() = ERROR_PIPE_BUSY Then
log.erreur("ConnectionServeur : CreateFile : PIPE OCCUPEE")
System.Threading.Thread.Sleep(1000)
Else
log.erreur("ConnectionServeur : CreateFile")
End If
Return False
End If
Catch e As Exception
log.erreur("ConnectionServeur impossible : Exception -> Source : " & _
e.Source & ", Message : " & e.Message & ",e.StackTrace : " & e.StackTrace)
Return False
End Try
End Function
Public Sub EnvoyerEvenement(ByVal Evenement As Constantes.EVENT_LOG)
If Connecte = True Then
Message = Constantes.SET_EVENT_LOG_TO_UDP_PACKET(Evenement)
'TailleMessage = System.Text.Encoding.ASCII.GetBytes(Message.Length)
Try
log.debug("WriteFile")
res = WriteFile(hPipe, Message, Message.Length, cbnCount, 0)
log.debug("res : " & res.ToString)
If res = 0 Then
log.erreur("EnvoyerEvenement -> Envoie Message")
Else
log.debug("EnvoyerEvenement -> Message envoye")
End If
Catch ex As Exception
log.erreur("EnvoyerEvenement impossible")
End Try
End If
End Sub
End Class
######################################
Cote serveur
#####################################
Imports LibrairieBasique
Imports LibrairieBasique.LibrairiePipe
Imports System.Text
Public Class ReceptionEvenement_PIPE
Private hPipe As Integer
Private openMode, pipeMode As Integer
Dim ETAT As Boolean
Dim log As Log
Sub New(ByVal log As log)
Me.log = log
If Creation_Pipe() = True Then
Demmarer_Ecoute_Pipe()
Else
log.erreur("serveur non demarre car probleme creation du pipe")
End If
End Sub
'Creation du pipe nomme
Function Creation_Pipe()
Try
openMode = PIPE_ACCESS_DUPLEX Or FILE_FLAG_WRITE_THROUGH
pipeMode = PIPE_WAIT Or PIPE_TYPE_MESSAGE Or PIPE_READMODE_MESSAGE
hPipe = CreateNamedPipe(pipeName, openMode, pipeMode, 10, 10000, 2000, 10000, IntPtr.Zero)
Catch e As Exception
log.erreur("Analyse -> Creation_Pipe -> Source : " + e.Source + ", Message : " + e.Message)
Return False
End Try
log.debug("pipe no : " & hPipe.ToString)
Return True
End Function
Public Sub Demmarer_Ecoute_Pipe()
Dim cbnCount, res As Integer
Dim BufferMessage(PIPE_BUFFSIZE) As Byte
Dim Retour() As Byte = Encoding.ASCII.GetBytes("ACK")
ETAT = True
While ETAT = True
Try
res = ConnectNamedPipe(hPipe, 0)
res = ReadFile(hPipe, BufferMessage, PIPE_BUFFSIZE, cbnCount, 0)
'If res <> 0 Then
If cbnCount > 0 Then
log.debug(Encoding.ASCII.GetString(BufferMessage))
' res = WriteFile(hPipe, Retour, Len(Retour), cbnCount, 0)
' res = FlushFileBuffers(hPipe)
End If
'Else
'log.debug("Analyse -> Serveur_Pipe -> erreur lecture message, erreur no : " & GetLastError().ToString)
'End If
res = DisconnectNamedPipe(hPipe)
Catch e As Exception
log.erreur("Analyse -> Serveur_Pipe -> Source : " + e.Source + ", Message : " + e.Message)
End Try
End While
End Sub
Public Sub Arreter_Ecoute_Pipe()
ETAT = False
End Sub
End Class
###########################################
librairie
###########################################
Public Class LibrairiePipe
Public Shared Function getInteger(ByVal tab As Byte())
Return ((tab(3) << 24) + (tab(2) << 16) + (tab(1) << 8) + tab(0))
End Function
Public Const pipeName As String = "\\.\pipe\lognt"
Public Const PIPE_BUFFSIZE As Short = 1536
Public Const FILE_ATTRIBUTE_NORMAL As Short = &H80S
Public Const FILE_FLAG_NO_BUFFERING As Integer = &H20000000
Public Const FILE_FLAG_WRITE_THROUGH As Integer = &H80000000
Public Const GENERIC_READ As Integer = &H80000000
Public Const GENERIC_WRITE As Integer = &H40000000
Public Const FILE_SHARE_READ As Short = &H1
Public Const FILE_SHARE_WRITE As Short = &H2
Public Const CREATE_ALWAYS As Short = 2
Public Const CREATE_NEW As Short = 1
Public Const OPEN_ALWAYS As Short = 4
Public Const OPEN_EXISTING As Short = 3
Public Const TRUNCATE_EXISTING As Short = 5
Public Const FILE_ATTRIBUTE_ARCHIVE As Short = &H20
Public Const FILE_ATTRIBUTE_HIDDEN As Short = &H2
Public Const FILE_ATTRIBUTE_READONLY As Short = &H1
Public Const FILE_ATTRIBUTE_SYSTEM As Short = &H4
Public Const FILE_FLAG_DELETE_ON_CLOSE As Integer = &H4000000
Public Const FILE_FLAG_OVERLAPPED As Integer = &H40000000
Public Const FILE_FLAG_POSIX_SEMANTICS As Integer = &H1000000
Public Const FILE_FLAG_RANDOM_ACCESS As Integer = &H10000000
Public Const FILE_FLAG_SEQUENTIAL_SCAN As Integer = &H8000000
Public Const PIPE_ACCESS_DUPLEX As Short = &H3S
Public Const PIPE_READMODE_MESSAGE As Short = &H2S
Public Const PIPE_TYPE_MESSAGE As Short = &H4S
Public Const PIPE_WAIT As Short = &H0S
Public Const INVALID_HANDLE_VALUE As Short = -1
Public Const ERROR_PIPE_BUSY = 231
Declare Function GetLastError Lib "kernel32.dll" () As Long
Declare Function CreateNamedPipe Lib "kernel32" Alias "CreateNamedPipeA" _
(ByVal lpName As String, ByVal dwOpenMode As Integer, _
ByVal dwPipeMode As Integer, ByVal nMaxInstances As Integer, _
ByVal nOutBufferSize As Integer, ByVal nInBufferSize As Integer, _
ByVal nDefaultTimeOut As Integer, ByVal lpSecurityAttributes As IntPtr _
) As Integer
Declare Function ConnectNamedPipe Lib "kernel32" _
(ByVal hNamedPipe As Integer, ByVal lpOverlapped As Integer) As Integer
Public Declare Function CallNamedPipe Lib "kernel32" Alias "CallNamedPipeA" _
(ByVal lpNamedPipeName As String, _
ByRef lpInBuffer As Integer, _
ByVal nInBufferSize As Integer, _
ByRef lpOutBuffer As Byte, _
ByVal nOutBufferSize As Integer, _
ByRef lpBytesRead As Integer, ByVal nTimeOut As Integer) As Integer
Declare Function DisconnectNamedPipe Lib "kernel32" _
(ByVal hNamedPipe As Integer) As Integer
Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
Declare Function WriteFile Lib "kernel32" _
(ByVal hFile As Integer, ByRef lpBuffer() As Byte, _
ByVal nNumberOfBytesToWrite As Integer, ByRef lpNumberOfBytesWritten As Integer, _
ByVal lpOverlapped As Integer _
) As Integer
Declare Function ReadFile Lib "kernel32" _
(ByVal hFile As Integer, ByRef lpBuffer() As Byte, _
ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, _
ByVal lpOverlapped As Integer _
) As Integer
Declare Function FlushFileBuffers Lib "kernel32" _
(ByVal hFile As Integer) As Integer
Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Integer) As Integer
End Class