2013-07-22 6 views
5

Per quanto ci sono molte domande su qui che sono molto simili, nessuna delle risposte fornite mi hanno aiutato, che mi rende triste :(UDP SocketException - un solo utilizzo di ogni indirizzo socket è normalmente consentita

I Ho un sistema di gestione molto grande a cui sono stato affidato il compito di scrivere alcuni pacchetti di invio/ricezione UDP. Avevo già scritto un prototipo e tutto andava bene, quindi ho iniziato a unire il mio codice in detto sistema. ve ora ottenuto una (non show-stopping, ma fastidioso) SocketException spuntando:

System.Net.Sockets.SocketException occurred 
    ErrorCode=10048 
    Message=Only one usage of each socket address (protocol/network address/port) is normally permitted 
    NativeErrorCode=10048 
    Source=System 
    StackTrace: 
     at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress) 
     at System.Net.Sockets.Socket.Bind(EndPoint localEP) 
     at System.Net.Sockets.UdpClient..ctor(Int32 port, AddressFamily family) 
     at System.Net.Sockets.UdpClient..ctor(Int32 port) 
     at Goose.Job.DeviceServerUDPReceiver.InitialiseReceiverClient() in C:\WORK\Trunk\GooseOrders\Classes\SheetCounter\DeviceServerUDPReceiver.vb:line 39 

Ecco la classe UDPReceiver - che è responsabile solo per sedersi in un ciclo e in attesa di risposte dai Device Server che abbiamo disseminato sul luogo.

Public Class DeviceServerUDPReceiver : Implements IDisposable 
'/////////////////////////////////////////////////////////////////////////////// 
' CONSTANTS 
'/////////////////////////////////////////////////////////////////////////////// 
Private Const TIBBO_DEVICE_REPLY_CMD_START As Integer = 0 
Private Const TIBBO_DEVICE_REPLY_CMD_END As Integer = 3 
Private Const TIBBO_MESSAGE_REPLY_DIVIDER As String = "_" 
Private Const TIBBO_DEVICE_REPLY_OK As String = "OK" 

'/////////////////////////////////////////////////////////////////////////////// 
' MEMBER VARIABLES 
'/////////////////////////////////////////////////////////////////////////////// 
Public _ReceivingClient As System.Net.Sockets.UdpClient 
Public _iReceivingPort As Integer = 2002 
Public _thReceivingThread As System.Threading.Thread 
Public _bClosing As Boolean 

'/////////////////////////////////////////////////////////////////////////////// 
' EVENTS 
'/////////////////////////////////////////////////////////////////////////////// 
Public Event GotDeviceResponse(ByVal sResponse As String) 
Public Event FoundNewDevice(ByVal TibboObject As TibboDevice) 

'/////////////////////////////////////////////////////////////////////////////// 
' METHODS 
'/////////////////////////////////////////////////////////////////////////////// 
' Initialises the UDP receiver client on the specified port number. Then runs 
' a listening thread constantly waiting to receive udp messages 
Public Sub InitialiseReceiverClient() 
    Try 
     ' TODO - FIX SOCKET EXCEPTION HERE - NOT THREAD ISSUE - THIS IS DUE TO 
     ' THE SOCKET NOT BEING CLOSED. BUT SEEING HOW UDP IS CONNECTIONLESS .... ?! 
     _ReceivingClient = New System.Net.Sockets.UdpClient(_iReceivingPort) 
     Dim thStartThread As Threading.ThreadStart = New Threading.ThreadStart(AddressOf SitAndReceive) 
     _thReceivingThread = New Threading.Thread(thStartThread) 
     _thReceivingThread.IsBackground = True 
     _thReceivingThread.Start() 
    Catch ex As System.Net.Sockets.SocketException 
     Console.WriteLine("Socket Exception: " & ex.Message) 
    Finally 

    End Try 
End Sub 

' The endless loop listener thread. Will sit and wait for udp packets to 
' process 
Private Sub SitAndReceive() 
    Dim epEndPoint As System.Net.IPEndPoint = New System.Net.IPEndPoint(System.Net.IPAddress.Any, _iReceivingPort) 

    ' infinite loop to listen for udp messages 
    While (_bClosing = False) 
     Try 
      Dim sMessage As String = "" 
      Dim byData() As Byte 

      byData = _ReceivingClient.Receive(epEndPoint) 
      sMessage = System.Text.Encoding.ASCII.GetString(byData) 
      Console.WriteLine(sMessage) 

      ProcessIncomingUDPDataMessage(sMessage) 

     Catch ex As System.Net.Sockets.SocketException 
      Console.WriteLine(ex.Message) 
     End Try 
    End While 
End Sub 

' close the connection to the receiving udp socket 
Public Sub Close() 
    _bClosing = True 
End Sub 


' Processes incoming udp packets for answeres from the device servers 
Private Sub ProcessIncomingUDPDataMessage(ByVal sMessage As String) 

    ' UDP Data packet from Tibbo devices is set out as follows 
    ' 
    ' CMD_ANSWER 
    ' Where "CMD" = The command the device is replying too and 
    ' "ANSWER" = It's reply 
    Select Case sMessage.Substring(TIBBO_DEVICE_REPLY_CMD_START, TIBBO_DEVICE_REPLY_CMD_END) 
     Case TibboDevice.DEVICE_COMMAND_ATO 
      '///////////////////////////////////////////////////////////////////////// 
      ' Any Tibbo's out there reply message 
      '///////////////////////////////////////////////////////////////////////// 
      Dim s() As String = sMessage.Split(TIBBO_MESSAGE_REPLY_DIVIDER) 
      Dim sMacAddress As String = s(2) ' the replying devices' mac address 
      Dim sIpAddress As System.Net.IPAddress = System.Net.IPAddress.Parse(s(3)) ' ip 
      Dim sNetBiosName As String = s(1) ' netbios name 
      Dim iTibboStatus As TibboDevice.ETIIBO_DEVICE_STATE = TibboDevice.ETIIBO_DEVICE_STATE.TIBBO_DEVICE_STATE_BAD ' status 

      ' set this device status depending on the reply 
      If s(4) = TIBBO_DEVICE_REPLY_OK Then 
       iTibboStatus = TibboDevice.ETIIBO_DEVICE_STATE.TIBBO_DEVICE_STATE_OK 
      End If 

      ' create a new tibbo device to pass back to the main form 
      Dim Tibbo As TibboDevice = New TibboDevice(sMacAddress, sIpAddress, sNetBiosName, iTibboStatus) 
      ' raise event to add this to our list 
      RaiseEvent FoundNewDevice(Tibbo) 


     Case TibboDevice.DEVICE_COMMAND_STS 
      '////////////////////////////////////////////////////////////////////////// 
      ' Status reply message 
      '////////////////////////////////////////////////////////////////////////// 
      Dim s() As String = sMessage.Split(TIBBO_MESSAGE_REPLY_DIVIDER) 
      Dim sResult As String = "" 

      ' format our string nicely 
      sResult &= "Mac Address: " & vbTab & vbTab & s(1) 
      sResult &= Environment.NewLine & "IP Address: " & vbTab & vbTab & s(2) 
      sResult &= Environment.NewLine & "Device Name: " & vbTab & vbTab & s(3) 
      sResult &= Environment.NewLine & "TiOS FW: " & vbTab & vbTab & s(4) 
      sResult &= Environment.NewLine & "Goose SC FW: " & vbTab & vbTab & s(5) 
      sResult &= Environment.NewLine & "System Uptime: " & vbTab & vbTab & s(6) 
      sResult &= Environment.NewLine & "System Time: " & vbTab & vbTab & s(7) 
      sResult &= Environment.NewLine & "System Status: " & vbTab & vbTab & s(8) 

      RaiseEvent GotDeviceResponse(sResult) 

     Case TibboDevice.DEVICE_COMMAND_ASC 
      '//////////////////////////////////////////////////////////////////////////// 
      ' Average sheet count message 
      '//////////////////////////////////////////////////////////////////////////// 
      Dim s() As String = sMessage.Split(TIBBO_MESSAGE_REPLY_DIVIDER) 
      Dim sResult As String = "" 

      RaiseEvent GotDeviceResponse(sResult) 

     Case TibboDevice.DEVICE_COMMAND_NAM 
      '//////////////////////////////////////////////////////////////////////////// 
      ' Changed device name reply message 
      ' Device will reply NAM_[NEWNAME] - once it's set it's new name 
      '//////////////////////////////////////////////////////////////////////////// 
      Dim s() As String = sMessage.Split(TIBBO_MESSAGE_REPLY_DIVIDER) 
      Dim sResult As String = "" 

      RaiseEvent GotDeviceResponse(sResult) 

     Case TibboDevice.DEVICE_COMMAND_IDX 
      '//////////////////////////////////////////////////////////////////////////// 
      ' Device responds with it's device id 
      '//////////////////////////////////////////////////////////////////////////// 
      Dim s() As String = sMessage.Split(TIBBO_MESSAGE_REPLY_DIVIDER) 
      Dim sResult As String = "" 

      ' TODO - do something with the result 

     Case TibboDevice.DEVICE_COMMAND_RBT 
      '//////////////////////////////////////////////////////////////////////////// 
      ' Device is going down for a reboot - not much to do here, we have to wait 
      '//////////////////////////////////////////////////////////////////////////// 

     Case TibboDevice.DEVICE_COMMAND_BUZ 
      '//////////////////////////////////////////////////////////////////////////// 
      ' Device has played it's buzz sound - ignore 
      '//////////////////////////////////////////////////////////////////////////// 

     Case TibboDevice.DEVICE_COMMAND_FSH 
      '//////////////////////////////////////////////////////////////////////////// 
      ' Device flashed it's LEDs - ignore 
      '//////////////////////////////////////////////////////////////////////////// 

     Case TibboDevice.DEVICE_COMMAND_AIP 
      '//////////////////////////////////////////////////////////////////////////// 
      ' Device replies with it's actual ip address 
      '//////////////////////////////////////////////////////////////////////////// 
      Dim s() As String = sMessage.Split(TIBBO_MESSAGE_REPLY_DIVIDER) 
      Dim sResult As String = "" 

      ' TODO - do something with the result 

     Case TibboDevice.DEVICE_COMMAND_CBC 
      '//////////////////////////////////////////////////////////////////////////// 
      ' Device replies with it's current box count 
      '//////////////////////////////////////////////////////////////////////////// 
      Dim s() As String = sMessage.Split(TIBBO_MESSAGE_REPLY_DIVIDER) 
      Dim sResult As String = "" 

      ' TODO - do something with the result 

     Case TibboDevice.DEVICE_COMMAND_STP 
      '//////////////////////////////////////////////////////////////////////////// 
      ' Device has been stopped - won't reply. Only way to bring it back to life 
      ' is to press the 'reset' button on the actual unit - ignore 
      '//////////////////////////////////////////////////////////////////////////// 
    End Select 

End Sub 

Protected Overridable Overloads Sub Dispose(disposing As Boolean) 
    If (disposing) Then 
     ' free managed objects 
     '_ReceivingClient = Nothing 
     _bClosing = True 
    End If 
End Sub 

Public Overloads Sub Dispose() Implements IDisposable.Dispose 
    Dispose(True) 
    GC.SuppressFinalize(Me) 
End Sub 


End Class 

Ora, tutto quello che sto facendo nella maschera principale, è: quando la mia forma ascoltatore è chiuso - voglio chiudere l'ascoltatore verso il basso (ovviamente) ... Per questo, sto usando Dispose(). Tuttavia, quando qualcuno desidera riavviarlo, si verifica un'eccezione nella riga byData = _ReceivingClient.Receive(epEndPoint) nella procedura SitAndReceive.

Poiché UDP è basato sulle transazioni e i suoi socket (possibilmente teoricamente) non possono essere in uno stato CLOSE_WAIT, cosa mi impedisce di chiuderlo e quindi di riavviare immediatamente l'ascoltatore?

Devo ammettere che sono nuovo per i socket UDP, finora tuttavia, ho trovato lieto di lavorare con e anche se questa eccezione non comprometterebbe il software degli utenti finali (con un semplice try/catch), mi ha incuriosito e mi piacerebbe capire perché sta accadendo.

Qualsiasi aiuto è molto apprezzato.

risposta

14

Gestito per capirlo alla fine. A quanto pare, se si vuole avere più di una connessione ad un socket, è necessario configurare manualmente, in questo modo:

Dim endPoint = New System.Net.IPEndPoint(0, _iReceivingPort) 
_ReceivingClient = New System.Net.Sockets.UdpClient() 
_ReceivingClient.ExclusiveAddressUse = False 
_ReceivingClient.Client.SetSocketOption(Net.Sockets.SocketOptionLevel.Socket, Net.Sockets.SocketOptionName.ReuseAddress, True) 
_ReceivingClient.Client.Bind(endPoint) 

ora funziona, quindi sono felice.

+1

Bello! Stavo avendo lo stesso problema. Mi ci è voluto un po 'per capire perché non funzionava e mi sono reso conto che stavo creando il mio oggetto UdpClient con una porta (come '_receiver = new UdpClient (myPort)'), che deve fare un po' di binding sottostante. Dopo aver usato il costruttore predefinito, ha funzionato bene. – Daniel

+0

Bello :) Sono contento che tu abbia funzionato! – LokiSinclair

+0

La tua soluzione funziona bene! Stavo inviando una richiesta multicast, con più ricevitori sullo stesso host. Stavo ricevendo lo stesso errore quando stavo cercando di eseguire più di un ricevitore che doveva ascoltare la stessa porta. –