Unable to receive data in VBA over UDP socket

Viewed 135

I am writing VBA code for receiving data over UDP socket where my sender code is in java. I am able to send data from VBA to java but am unable to receive data from java to VBA. Data that has been sent is also reflect in wireshark but is not able to get received data. Please help me with the same. Below is my code.

Const INVALID_SOCKET = -1
Const WSADESCRIPTION_LEN = 256

Enum AF
  AF_UNSPEC = 0
  AF_INET = 2
  AF_IPX = 6
  AF_APPLETALK = 16
  AF_NETBIOS = 17
  AF_INET6 = 23
  AF_IRDA = 26
  AF_BTH = 32

End Enum

Enum sock_type
   SOCK_STREAM = 1
   SOCK_DGRAM = 2
   SOCK_RAW = 3
   SOCK_RDM = 4
   SOCK_SEQPACKET = 5
End Enum

Enum Protocol
   IPPROTO_ICMP = 1
   IPPROTO_IGMP = 2
   BTHPROTO_RFCOMM = 3
   IPPROTO_TCP = 6
   IPPROTO_UDP = 17
   IPPROTO_ICMPV6 = 58
   IPPROTO_RM = 113


End Enum

Type sockaddr
   sa_family As Integer
   sa_data(0 To 13) As Byte
End Type
 
Type sockaddr_in
  sin_family As Integer
   sin_port(0 To 1) As Byte
  sin_addr(0 To 3) As Byte
  sin_zero(0 To 7) As Byte
End Type



'typedef UINT_PTR        SOCKET;
Type socket
   pointer As Long
End Type


Type LPWSADATA_Type
   wVersion As Integer
   wHighVersion As Integer
   szDescription(0 To WSADESCRIPTION_LEN) As Byte
   szSystemStatus(0 To WSADESCRIPTION_LEN) As Byte
   iMaxSockets As Integer
   iMaxUdpDg As Integer
   lpVendorInfo As Long
End Type

'int errorno = WSAGetLastError()
Public Declare Function WSAGetLastError Lib "Ws2_32.dll" _
   () As Integer
   
Public Declare Function WSAStartup Lib "Ws2_32.dll" _
    (ByVal wVersionRequested As Integer, ByRef lpWSAData As LPWSADATA_Type) As Long


Public Declare Function sendto Lib "Ws2_32.dll" _
    (ByVal socket As Long, ByRef Buf() As Byte, _
     ByVal length As Long, ByVal flags As Long, _
     ByRef toaddr As sockaddr_in, tolen As Long) As Long

Public Declare Function recvfrom Lib "Ws2_32.dll" _
    (ByVal socket As Long, ByRef Buf As Byte, _
     ByVal length As Long, ByVal flags As Long, _
     ByRef toaddr As sockaddr_in, tolen As Long) As Long


Public Declare Function f_socket Lib "Ws2_32.dll" Alias "socket" _
    (ByVal AF As Long, ByVal stype As Long, ByVal Protocol As Long) As Long

Public Declare Function closesocket Lib "Ws2_32.dll" (ByVal socket As Long) As Long
    
Private Declare Function recv Lib "Ws2_32.dll" (ByVal socket As Long, ByRef Buf As Byte, ByVal length As Long, ByVal flags As Long) As Long
Public Declare Sub WSACleanup Lib "Ws2_32.dll" ()
Sub main()
Dim ConnectSocket As socket

Dim wsaData As LPWSADATA_Type
      
Dim iResult As Integer
   iResult = 0

Dim send_sock As sock_type
  send_sock = INVALID_SOCKET
    
Dim iFamily As AF
   iFamily = AF_INET
    
Dim iType As Integer
   iType = SOCK_DGRAM
   
    
Dim iProtocol As Integer
   iProtocol = IPPROTO_UDP
'this is the ip addr and port I want to send to. 
Dim RecvAddr As sockaddr_in
RecvAddr.sin_family = AF_INET

RecvAddr.sin_port(1) = 128
RecvAddr.sin_port(0) = 0

RecvAddr.sin_addr(0) = 224
RecvAddr.sin_addr(1) = 0
RecvAddr.sin_addr(2) = 0
RecvAddr.sin_addr(3) = 3

Dim SendBuf(0 To 3) As Byte
Dim BufLen As Integer
Dim rLen2 As Long
Dim rLen As Long
Dim lengthData As String
Dim receivedcount As Integer
Dim bytesReceived(0 To 3) As Byte
Dim stringData As String

BufLen = 8
SendBuf(0) = 65
SendBuf(1) = 66
SendBuf(2) = 67
SendBuf(3) = 0
'Dim teststring As String
'teststring = "ABC"
'Dim SendBuf() As Byte
'SendBuf = StrConv(teststring, vbUpperCase)

'BufLen = Len(teststring)
'BufLen = UBound(SendBuf) - LBound(SendBuf)
'teststring needs to contain the data you want to send.

iResult = WSAStartup(&H202, wsaData)
If iResult <> 0 Then
   MsgBox ("WSAStartup failed: " & iResult)
   Exit Sub
End If


send_sock = f_socket(iFamily, iType, iProtocol)
    
If send_sock = INVALID_SOCKET Then
   Errno = WSAGetLastError()
   MsgBox ("INVALID SOCKET")
   Exit Sub
End If

'iResult = sendto(send_sock, SendBuf, BufLen, 0, RecvAddr, Len(RecvAddr))
'If iResult = -1 Then
 'MsgBox ("sendto failed with error: " & WSAGetLastError())
'Else
 'MsgBox ("Sending successfull " & BufLen)
'End If

MsgBox ("1.receiving.. " & Len(RecvAddr))
'MsgBox ("2.receiving.. " & bytesReceived)
'rLen = recv(send_sock, bytesReceived(0), 3, 0)
rLen = recvfrom(send_sock, bytesReceived(0), 3, 0, RecvAddr, Len(RecvAddr))
'MsgBox "Hi.."
DoEvents
MsgBox "rLen value = " & rLen
        
   receivedcount = receivedcount + 1
    If RetVal <> 0 Then
        'closesocket (send_sock)
        Call WSACleanup
        Exit Sub
        Else
        Debug.Print "closed socket"
         MsgBox "closed socket " & receivedcount
        receivedcount = 0
    End If
   
   stringData = ""
   For i = 0 To rLen - 1
        stringData = stringData & Chr(bytesReceived(i))
    Next i
        MsgBox "stringData value = " & stringData
        
                    
iResult = closesocket(send_sock)
If iResult <> 0 Then
   MsgBox ("closesocket failed with error : " & WSAGetLastError())
   Call WSACleanup
End If
End Sub

0 Answers
Related