Bonjour,

Je developpe une petite application qui doit, par l'intermediaire d'une
liaison ethernet, échanger des fichiers.
J'utilise ainsi des sockets avec une lecture du fichier juste avant,
voici mon code source :


Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
94
95
96
97
98
99
100
Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" 
(ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal 
dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal 
dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, 
ByVal hTemplateFile As Long) As Long 
 
 
Public Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, 
lpbuffer As Any, ByVal nNumberOfBytesToRead As Long, 
lpNumberOfBytesRead As Long, lpOverlapped As Long) As Long 
 
 
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As 
Long) As Long 
 
 
Public Const FILE_SHARE_READ = &H1 
Public Const GENERIC_READ = &H80000000 
Public Const FILE_ATTRIBUTE_NORMAL = &H80 
 
 
Public Const OPEN_EXISTING = 3 
Public Const OPEN_ALWAYS = 4 
 
 
Public Sub MyReadFile(NewFile As String, Taille As Long) 
    Dim sec As SECURITY_ATTRIBUTES 
    Dim h1 As Long 
    Dim ReadBuffer As String 
    Dim BytesRead As Long 
    Dim rc As Long 
    Dim paquet As Long 
    paquet = 1024 
    Dim Nbr_lu As Long 
    Dim CR As Long 
    Dim wsadata As WSA_DATA 
    Dim sock As Long 
    Dim CR1 As Long 
    Dim Result As String 
    Dim Nbr_total As Long 
    Nbr_total = 0 
 
 
    sec.bInheritHandle = True 
    sec.lpSecurityDescriptor = 0 
    sec.nLength = Len(sec) 
 
 
    h1 = CreateFile(NewFile, GENERIC_READ, 0, sec, OPEN_ALWAYS, 
FILE_ATTRIBUTE_NORMAL, 0) 
 
 
CR = WSAStartup(&H101, wsadata) 
If CR <> 0 Then 
    MsgBox "Mauvaise version de Winsock.dll" 
    Exit Sub 
Else: MsgBox ("WSA ok") 
End If 
 
 
sock = Socket(AF_INET, SOCK_STREAM, 0) 
If sock < 0 Then 
   MsgBox "Erreur sur la creation du socket = " & WSAGetLastError() 
   FermerSocket sock 
   Exit Sub 
End If 
 
 
rmtserver.sin_family = AF_INET 
rmtserver.sin_port = htons(8686) 
rmtserver.sin_addr.S_addr = inet_addr("127.0.0.1") 
rmtserver.sin_zero(0) = 0 
 
 
CR = connect(sock, rmtserver, Len(rmtserver)) 
If CR < 0 Then 
    MsgBox "Erreur sur Connect : " & WSAGetLastError() 
    FermerSocket sock 
    Exit Sub 
End If 
 
 
If CR = 0 Then MsgBox ("Connexion ok") 
 
 
While (Nbr_total < Taille) 
rc = ReadFile(h1, ReadBuffer, paquet, Nbr_lu, 0) 
CR1 = send(sock, ReadBuffer, paquet, 0) 
Nbr_total = Nbr_total + Nbr_lu 
Wend 
 
 
CloseHandle (h1) 
 
 
Result = closesocket(sock) 
If Result = SOCKET_ERROR Then 
   MsgBox "Erreur sur la fermeture du socket" 
End If 
End Sub

Voyez vous une/des erreur(s) dans mon programme ?
Est t-il possible de mélanger deux langage de programmation c'est a
dire de faire un client en VB et un serveur en C++ ?


Merci de votre aide
Johnson