UDP Server and Client to have the same port

Mosteck

Active member
Joined
Nov 23, 2008
Messages
31
Location
Toronto
Programming Experience
3-5
I'm writing an application that communicates to a piece of industrial hardware using UDP to collect information from the unit on the process.

The code looks good (analysed my ASCII strings to their custom software ASCII strings using WireShark and they match), but the only difference is that their program matches the server and client ports.

How can I modify my code to send from the same port number (example 8364) as the one I'm sending to?

VB.NET:
Imports System.Net
Imports System.Net.Sockets

Public Class Form2

    Protected Shared Function UDPDataTransmit(ByVal sBuff() As Byte, ByRef rBuff() As Byte, ByVal IP As String, ByVal Port As Integer) As Integer

        Dim UDP As New UdpClient

        Dim retstat As Integer
        Dim Sck As Socket
        Dim Due As DateTime
        Dim Encrp As IPEndPoint

        Try
            Sck = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
            Sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 10000)
            Sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 10000)

            Encrp = New IPEndPoint(IPAddress.Parse(IP), Port)
            retstat = Sck.SendTo(sBuff, 0, sBuff.Length, SocketFlags.None, Encrp)

            If retstat > 0 Then

                '5 second time-out
                Due = Now.AddMilliseconds(10000)

                Do While Sck.Available = 0 AndAlso Now < Due
                Loop

                If Sck.Available = 0 Then
                    'Timed out 
                    retstat = -3
                    Return retstat
                End If

                ReDim rBuff(Sck.Available - 1)
                retstat = Sck.ReceiveFrom(rBuff, 0, Sck.Available, SocketFlags.None, CType(Encrp, EndPoint))

            Else

                'Fail on send
                retstat = -1

            End If

        Catch ex As Exception

            'General Exception received--add code here to analyze the exception. A messagebox would be one idea. 

            MsgBox(ex.Message)

            retstat = -2

        Finally

            'Always close the socket when done. 
            Sck.Close()

        End Try

        Return retstat

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim SendCnt As Integer
        Dim SendString As String
        Dim SendData As Byte() = New Byte() {}
        Dim RecData As Byte() = New Byte() {}
        Dim IPAddress As String
        Dim Port As Integer
        Dim DecodeCnt As Integer
        Dim DecodeLen As Integer
        Dim DecodeAsciiCodeInt As String
        Dim DecodeAsciiCode As String
        Dim DecodedResponse As String

        Dim BursterClearBuffer As String = Chr(4)
        Dim BursterWakeUp As String = "00sr" & Chr(5)
        Dim BursterInfo As String = Chr(2) & "0,1,INFO?" & Chr(3) & Chr(60) & "179" & Chr(62)

        SendString = BursterInfo

        SendData = System.Text.Encoding.ASCII.GetBytes(SendString)
        IPAddress = "172.26.117.154"
        Port = 8364

        UDPDataTransmit(SendData, RecData, IPAddress, Port)

        DecodeLen = RecData.Length

        DecodedResponse = ""

        For DecodeCnt = 0 To RecData.Length - 1

            DecodeAsciiCodeInt = RecData(DecodeCnt)

            Select Case DecodeAsciiCodeInt
                Case "0"
                    DecodeAsciiCode = "<NULL>"
                Case "1"
                    DecodeAsciiCode = "<SOH>"
                Case "2"
                    DecodeAsciiCode = "<STX>"
                Case "3"
                    DecodeAsciiCode = "<ETX>"
                Case "4"
                    DecodeAsciiCode = "<EOT>"
                Case "5"
                    DecodeAsciiCode = "<ENQ>"
                Case "6"
                    DecodeAsciiCode = "<ACK>"
                Case "7"
                    DecodeAsciiCode = "<BEL>"
                Case "8"
                    DecodeAsciiCode = "<BS>"
                Case "9"
                    DecodeAsciiCode = "<TAB>"
                Case "10"
                    DecodeAsciiCode = "<LF>"
                Case Else
                    DecodeAsciiCode = Chr(RecData(DecodeCnt))
            End Select

            DecodedResponse = DecodedResponse & DecodeAsciiCode

        Next

        ReturnTextBox.Text = DecodedResponse
    End Sub
End Class

The code is not complete, just using it for testing purposes..

Thanks!
 
Figured it out... I needed to bind the socket so I added this in

VB.NET:
Encrp = New IPEndPoint((IPAddress.Any), 8364)
Sck.Bind(Encrp)
 
OK, so that didn't exactly solve my problem. Maybe some can help..

The string being received from the pre-made software is:

02 30 2c 31 2c 49 4e 46 4f 3f 03 b3 .0,1,INF O?..

My string in VB.Net is setup as:

Dim BursterInfo As String = Chr(2) & "0,1,INFO?" & Chr(3)

which gives me:

02 30 2c 31 2c 49 4e 46 4f 3f 03 .0,1,INF O?.

Everything looks good, but I can't get the final "b3" into my string. I've tried adding &Hb3, chr(178), asc(178), "178", "b3"....

How do I send something that converts to a Hex b3?

Thanks again...
 
Back
Top