TCPclient fonctionnement instable
Salut à tous
Je monte un projet visant à relier un terminal win ce 6.0 vers un excel bureau.
J'utiliste VSTO pour créer un add-in excel qui gère l'aspect communication
J'utilise TcpClient en mode synchrone
Au global ca marche et les applis échangent bien. Mais pour une raison que je n'arrive pas à m'expliquer, au bout d'un moment, la connexion du client au serveur echoue avec l'erreur 10060 ( timeout de connexion serveur ).
Et alors quand ca bug une fois, en fermant/redemarrant des 2 cotés, parfois ça remarche, parfois pas
Je suis pas expert en sockets, mais j'imagine qu'il y a quelquechose qui doit pas se fermer proprement, vous avez une idée ?
Coté wince (client)
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
| Private Sub EnvoiVersExcel()
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Label2.Text = "tentative connexion serveur"
Label2.Refresh()
client = New TcpClient(IpServeur, PortServeur)
Label2.Text = "connexion reussie"
Dim message As String = MessagePourExcel
Label2.Refresh()
' Translate the passed message into ASCII and store it as a Byte array.
Dim data As [Byte]() = System.Text.Encoding.UTF8.GetBytes(message)
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
stream = client.GetStream()
' Send the message to the connected TcpServer.
Label2.Text = "envoi du code"
Label2.Refresh()
stream.Write(data, 0, data.Length)
Label2.Text = "code envoyé"
Label2.Refresh()
' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
Label2.Text = "reponse recue"
Label2.Refresh()
responseData = System.Text.Encoding.UTF8.GetString(data, 0, bytes)
TraitementReponse(responseData)
' Close everything.
stream.Close()
client.Close()
Label2.Text = "connexion fermée"
Label2.Refresh()
Catch mess As ArgumentNullException
MsgBox(mess.ToString)
Catch mess As SocketException
MsgBox(mess.ToString)
Finally
stream.Close()
client.Close()
End Try
End Sub |
Coté Addin excel (serveur)
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
| Private Sub TreadEcoute()
serveuron = True
server = Nothing
Try
' Set the TcpListener on port 13000.
'Dim port As Int32 = 8484
Dim localAddr As IPAddress = IPAddress.Parse(IpServeur)
server = New TcpListener(localAddr, Port)
' Start listening for client requests.
server.Start()
' Buffer for reading data
Dim bytes(1024) As Byte
Dim data As String = Nothing
' Enter the listening loop.
While serveuron
' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
client = server.AcceptTcpClient()
Label2.Label = "en cours..."
data = Nothing
' Get a stream object for reading and writing
stream = client.GetStream()
Dim i As Int32
' Loop to receive all the data sent by the client.
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
Label2.Label = "réception"
' Translate data bytes to a ASCII string.
data = System.Text.Encoding.UTF8.GetString(bytes, 0, i)
EcrireMessageDansCelulle(data)
' Process the data sent by the client.
'data = data.ToUpper()
data = Reponse(data)
Dim msg As Byte() = System.Text.Encoding.UTF8.GetBytes(data)
' Send back a response.
stream.Write(msg, 0, msg.Length)
i = stream.Read(bytes, 0, bytes.Length)
Label2.Label = "réponse"
End While
' Shutdown and end connection
stream.Close()
client.Close()
Label2.Label = "fermée"
End While
Catch mess As SocketException
MsgBox(mess.ToString)
Finally
stream.Close()
client.Close()
server.Stop()
serveuron = False
End Try
End Sub |
Merci d'avance