[VB.NET] Comment configurer le LocalPort pour le socket
Bonjour,
Voici mon problème, lorsque j'envoie ma trame à mon appareil, celui ci la recoit et me répond, mais la propriété "available" reste toujours à "0". J'ai remarqué que le port local n'est pas égal à 9600, comment faire pour l'attribuer à 9600. Le port change de facon aléatoire.
Adresse IP de mon ordinateur : 192.168.0.90
Adresse IP de mon appareil : 192.168.0.200
Port utilisé : 9600
Mode UDP
J'ai vérifié avec un analyseur de packet pour savoir si l'envoie et la réception fonctionne, et ca fonctionne. Voici les images.
http://i93.photobucket.com/albums/l4...okPacket-1.jpg
Envoie de l'information
http://i93.photobucket.com/albums/l4...tus/Send-1.jpg
Réception de l'information
http://i93.photobucket.com/albums/l4.../Receive-1.jpg
Voici mon 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
| Imports System.Net
Imports System.Net.Sockets
Imports System.text
Public Sub ReadDM(ByVal Word As Integer, ByVal NbAcq As Integer)
Dim MySocket As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Dim AdresseIP As IPAddress = IPAddress.Parse("192.168.0.200")
Dim RemoteIpEndPoint As New IPEndPoint(AdresseIP, 9600)
Dim x As Integer = 0
Dim available As Integer
Dim ErrCmpt As Integer
'Connexion à l'automate
MySocket.Connect(RemoteIpEndPoint)
Dim msg As Byte() = Encoding.UTF8.GetBytes("@00RD0000000157*" & vbCr)
Reprise:
MySocket.Send(msg, msg.Length, SocketFlags.None)
Try
MySocket.Poll(1000000, SelectMode.SelectRead)
Try
available = MySocket.Available()
Catch ex As Exception
MsgBox("Problème lors de la réception des données")
Exit Sub
End Try
If available = 0 Then
ErrCmpt += 1
If ErrCmpt = 5 Then GoTo CommunicationError
GoTo Reprise
End If
Dim NewBuffer() As Byte = New Byte(available) {}
Dim tempRemoteEP As EndPoint = RemoteIpEndPoint
MySocket.ReceiveFrom(NewBuffer, available, 0, tempRemoteEP)
MsgBox(NewBuffer)
Catch ex As Exception
ErrCmpt += 1
If ErrCmpt = 5 Then GoTo CommunicationError
GoTo Reprise
End Try
MySocket.Shutdown(SocketShutdown.Both)
MySocket.Close()
Exit Sub
CommunicationError:
MySocket.Shutdown(SocketShutdown.Both)
MySocket.Close()
End Sub |
Merci pour votre aide.