UDPClient weird problem.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gangreen
    New Member
    • Feb 2008
    • 98

    UDPClient weird problem.

    I'm trying to send bytes to a server, but I always get an exception at the send command saying: the socket should not be connected (line 26 in the code below).

    This is weird right? cause I don't see how I can send without a connection...

    I work in visual basic.net

    Code:
    Function sendCommand(ByVal command As String)
            Dim dpr2() As Byte
            Dim sender() As Byte = (prepCommand(command))
    
    
            '//find a useable port
            While (True)
                Try
                    datagramSocket = New UdpClient(port)
                    Console.WriteLine("success, using port " + datagramSocket.Client.LocalEndPoint.ToString)
                    Exit While
                Catch ex As Exception
                    Console.WriteLine("port " + port + " unusable, trying next port")
                    port = port + 1
                End Try
            End While
    
    
            Try
                '//Create new UDP connection and connect to specified ip address + port
                datagramSocket.Connect(socketAddress.Address, socketAddress.Port)
                datagramSocket.ExclusiveAddressUse = False
    
                '//Initialize and send command as UDP packet to specified ip address and port
                Dim dps(sender.Length) As Byte
                datagramSocket.Send(sender, sender.Length, socketAddress)
                Console.WriteLine("Sending command to " & socketAddress.Address.ToString & ":" & socketAddress.Port)
            Catch Exc As Exception
                '//Catch failed command send
                Console.WriteLine("error: could not send UDP packet to server")
                Console.WriteLine("details: " + Exc.Message)
                datagramSocket.Close()
                Return Nothing
            End Try
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    UDP is a connectionless protocol, you do not establish a connection for it.

    I really suggest you do some reading up on the subject matter.
    It would be much easier for you to translate that java project to vb.net, if you actually understood the concepts behind the code.

    Comment

    • Gangreen
      New Member
      • Feb 2008
      • 98

      #3
      Originally posted by Plater
      UDP is a connectionless protocol, you do not establish a connection for it.

      I really suggest you do some reading up on the subject matter.
      It would be much easier for you to translate that java project to vb.net, if you actually understood the concepts behind the code.
      You're probably right...my UDP - knowledge ain't that great

      Comment

      • Gangreen
        New Member
        • Feb 2008
        • 98

        #4
        I've been reading, and (correct me if I'm wrong) it appears you can use UDP for communication with a remote server..

        Read at 'remarks'

        But I tried what was stated there, and tried several other things I have read elsewhere, but I'm still getting the exception...

        I tried using the default constructor: new(), and then connect and send with the IPEndPoint, without the IPEndPoint in the send instruction, I tried using the constructor with the IPendPoint as argument....ple ase give me a hint

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Hmm, very strange that the examples would say to connect. Would not be the first time the msdn has had copy/paste errors.

          I think you would want something more like this:
          [code=vbnet]
          ' Sends message to a different host using optional hostname and port parameters.
          'sendBytes is your byte array
          Dim udpClientB As New UdpClient()
          udpClientB.Send (sendBytes, sendBytes.Lengt h, "AlternateHostM achineName", 11000)
          [/code]

          Comment

          • Gangreen
            New Member
            • Feb 2008
            • 98

            #6
            Yes, that did the trick, thank you so much.

            Wow, I must say I'm learning quite a lot with this translating job .

            Comment

            Working...