Données d'un socket tronquées [VB2008]
Bonjour,
Voici le petit soucis que je rencontre.
J'ai un boitier mobile qui m'envoie des données via tcp et via GPRS.
Les 8 premiers bytes doivent lui être renvoyés à l'identique pour confirmer la connection GPRS. Cette procédure fonctionne.
Une fois cela fait, le boitier crache ses données chaque 10 secondes.
Je reçois les premières données mais celles-ci sont tronquées.
Ensuite je ne capte plus les données.
Je pense que je me suis trompé au niveau des boucles. Je n'arrive pas à trouver si je dois boucler sur la connection du client socket ou sur le contenu du netsworkstream.
Ce code fonctionne uniquement dans la console output pour l'instant.
Merci d'avance pour votre aide.
Tems2008
Voici le code :
Code:
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ipAddress As IPAddress = Dns.GetHostEntry("192.168.2.101").AddressList(0)
Dim serverSocket As New TcpListener(ipAddress, 7080)
Dim clientSocket As TcpClient
Dim counter As Integer
serverSocket.Start()
msg("Serveur démarré port 7080" + " - " + ipAddress.ToString + " - " + "Date : " + Date.Now)
counter = 0
'Boucle infinie
While (True)
counter += 1
clientSocket = serverSocket.AcceptTcpClient()
msg("Client No: " + Convert.ToString(counter) + " started!")
msg("Date : " + Date.Now)
'msg("ipAddress serveur : " + serverSocket.LocalEndpoint.ToString)
msg("ipaddress client : " + clientSocket.Client.RemoteEndPoint.ToString)
Dim client As New handleClinet
client.startClient(clientSocket, Convert.ToString(counter))
End While
clientSocket.Close()
serverSocket.Stop()
msg("exit")
'Console.ReadLine()
End Sub
Public Sub msg(ByVal mesg As String)
mesg.Trim()
Console.WriteLine(" >> " + mesg)
End Sub
Public Class handleClinet
Dim clientSocket As TcpClient
Dim clNo As String
Public Sub startClient(ByVal inClientSocket As TcpClient, ByVal clineNo As String)
Me.clientSocket = inClientSocket
Me.clNo = clineNo
Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf prog)
ctThread.Start()
End Sub
Private Sub prog()
Dim i As Integer = 0
Dim longueur_chaine As Integer = 0
Dim header_lu As Boolean = False
Dim resultat_networkstream As Integer = 0
Try
If header_lu = False Then
Dim networkStream As NetworkStream = clientSocket.GetStream()
Dim bytes(clientSocket.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(clientSocket.ReceiveBufferSize))
Dim sendbytes As [Byte]() = bytes
networkStream.Write(sendbytes, 0, 8)
header_lu = True
networkStream.Flush()
Form1.msg("")
Form1.msg("HEADER ENVOYE - ACK RECU")
'Header envoyé
End If
Catch ex As Exception
Form1.msg(ex.ToString)
End Try
While (clientSocket.Connected)
Form1.msg("")
Form1.msg("DEBUT DE RECEPTION DES DONNEES")
Form1.msg("")
Dim networkStream2 As NetworkStream = clientSocket.GetStream()
Dim bytes2(clientSocket.ReceiveBufferSize) As Byte
networkStream2.Read(bytes2, 0, CInt(clientSocket.ReceiveBufferSize))
Dim data_ns_100 As String = ""
data_ns_100 = Encoding.ASCII.GetString(bytes2)
'
' code de test
'
Dim testarray() As String = Split(data_ns_100, ",")
Dim LastNonEmpty As Integer = -1
For ii As Integer = 0 To testarray.Length - 1
If testarray(ii) <> "" Then
LastNonEmpty += 1
testarray(LastNonEmpty) = testarray(ii)
End If
Next
For aa As Integer = 0 To LastNonEmpty
Form1.msg(testarray(aa))
Next
'
Form1.msg(" SUITE ")
Form1.msg("")
networkStream2.Dispose()
networkStream2.Flush()
End While
Form1.msg("Header renvoyé")
Form1.msg("Fin")
End Sub
End Class |
Et l'affichage des données :
Citation:
>> Server Started 7080 - 192.168.2.101 - Date : 26/08/2008 22:51:46
>> Client No: 1 started!
>> Date : 26/08/2008 22:52:49
>> ipaddress client : 202.87.141.80:2020
>>
>> HEADER ENVOYE - ACK RECU
>>
>> DEBUT DE RECEPTION DES DONNEES
>>
>> 1010000019
>> 20080826111530
>> 5
>> 2
1010000019
>> 20080826111540
>> 6
>> 2
1010000019
>> 20080826111550
>> 5
>> 2
1010000019
>> 20080826111600
>> 5
>> 2
1010000019
>> 20080826111610
>> 5
>> 2
1010000019
>> 20080826111620
>> 5
>> 2
1010000019
>> 20080826111630
>> 5
>> 2
1010000019
>> 20080826111640
>> 5
>> 2
ici, il devrait y avoir le mot SUITE et il n'y a rien.