1

I'm currently working on a UDP communication PC <-> ARM LM3S6965 (Luminary) through the Ethernet. On the PC there is a VB.net application that simulates a UDP server/client.

the message is broadcasted from Vb.net application to ARM LM3S6965 based device, and in return response is sent from ARM Device to vb.net application.

but sometimes receive UdpClient throws socket exception i.e. Only one usage of each socket address (protocol/network address/port) is normally permitted.

the line which throws exception is

udpReceivingClient = New UdpClient(mnPort)

Complete VB.net Code is as follows

Module mod_Search_UDP
Public mnPort As Int32 = 3040                                   'Port number to send/recieve data on
'Public Const msBroadcastAddress As String = "255.255.255.255"   'Sends data to all LOCAL listening clients, to send data over WAN you'll need to enter a public (external) IP address of the other client
'Dim endPoint As IPEndPoint = New IPEndPoint(msBroadcastAddress, mnPort)
Public udpReceivingClient As UdpClient                          'Client for handling incoming data
Public udpSendingClient As UdpClient                            'Client for sending data
Public receivingThread As Thread                                'Create a separate thread to listen for incoming data, helps to prevent the form from freezing up
Public mbiClosing As Boolean = False                            'Used to close clients if form is closing
Public mbiCloseRxClient As Boolean = False

Public Sub InitializeSender()
    Dim soc As Socket
    Dim lsPort As String
    Dim lnPort As Int32 = 3040
    Const lsBroadcastAdd As String = "255.255.255.255"
    'Dim endPoint As IPEndPoint = New IPEndPoint(msBroadcastAddress, mnPort)
    'udpSendingClient = New UdpClient(endPoint)
    udpSendingClient = New UdpClient(lsBroadcastAdd, lnPort)

    udpSendingClient.EnableBroadcast = True

    soc = udpSendingClient.Client
    lsPort = (CType(soc.LocalEndPoint, IPEndPoint).Port.ToString())
    mnPort = Convert.ToInt32(lsPort)
End Sub

Public Sub InitializeReceiver()
    'Create UdpClient class and bind it to the local port number provided
    'Try
    udpReceivingClient = New UdpClient(mnPort)
    'udpReceivingClient.EnableBroadcast = True
    mbiCloseRxClient = True
    'Catch ex As Exception
    '    MsgBox(ex.ToString)
    'End Try


    Dim start As ThreadStart = New ThreadStart(AddressOf MT_Receiver)
    receivingThread = New Thread(start)
    receivingThread.IsBackground = True
    receivingThread.Start()
End Sub

Public Sub MT_Receiver()

    Dim endPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, mnPort)    'Listen for incoming data from any IP address on the specified port 
    Dim lbData() As Byte                                                'Buffer for storing incoming bytes
    Dim llRet As UInt16
    'udpListen.Poll(1000, Net.Sockets.SelectMode.SelectRead)
    While (True)                                                        'Setup an infinite loop
        If mbiClosing = True Then                                       'Exit sub if form is closing
            Exit Sub
        End If
        llRet = udpReceivingClient.Available
        If llRet > 0 Then
            lbData = udpReceivingClient.Receive(endPoint)                   'Receive incoming bytes
            'If udpListen.Available Then
            '    udpListen.Receive(lbData, 256, Net.Sockets.SocketFlags.None)
            'End If
            'If lbData Is Nothing Then
            'Else
            frmSearchUDP.MT_Validate_Msg(lbData)
        End If
    End While
End Sub
Public Sub MT_Send_UDP(ByVal lbTxBuffer() As Byte)
    InitializeSender()
    If mbiCloseRxClient = True Then
        receivingThread.Abort()
        udpReceivingClient.Client.Dispose()
        udpReceivingClient.Close()

    End If
    Try
        udpSendingClient.Send(lbTxBuffer, lbTxBuffer.Length)
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
    udpSendingClient.Client.Dispose()
    udpSendingClient.Close()
    InitializeReceiver()
    'Try
    '    udpReceivingClient.BeginReceive(AddressOf MT_RX_Callback, Nothing)
    'Catch ex As Exception
    '    MsgBox(ex.ToString)
    'End Try
End Sub
End Module

Whats mistake is there in code ? how can i use same port for receive and transmit ?

Sushant
  • 53
  • 7
  • Those who are giving down vote pls comment why. – Sushant Nov 05 '14 at 08:54
  • As soon as you're connected with the server, you have a Stream object to receive and send data. I don't see why you would need 2 UdpClients for this. Maybe you want to explain the special use case I can't think of? – Maximilian Riegler Nov 05 '14 at 08:56
  • i am new to vb.net, i refered this code from [This Link](http://stackoverflow.com/questions/16160550/visual-basic-udpclient-server-client-model). are you suggesting i can broadcast as well as receive from 1 client it self ?? – Sushant Nov 05 '14 at 09:07
  • I didn't fully understand what you were trying to accomplish since you didn't state anything regarding that matter in your post. – Maximilian Riegler Nov 05 '14 at 09:12
  • I'm currently working on a UDP communication PC <-> ARM LM3S6965 (Luminary) through the Ethernet. On the PC there is a VB.net application that simulates a UDP server/client. The message will be broadcasted from VB.Net application, and a response will be sent from n number of ARM Device connected. – Sushant Nov 05 '14 at 09:14
  • @rinukkusu Turns out that if you use a UDPClient that broadcasts a message out, it WILL NOT be able to receive messages back [Mentioned here in answer](http://stackoverflow.com/questions/16800226/udpclient-receive-doesnt-receive-any-data/16841026#16841026) – Sushant Nov 10 '14 at 07:31

0 Answers0