Challenges with Ms Access VBA with socket programming (1 Viewer)

nectorch

Member
Local time
Today, 15:08
Joined
Aug 4, 2021
Messages
41
I'm not having my code below initializing the VBA socket code as a result when I start the code nothing happen , see the code below where I need help and suggestion for the required fixing.

Calling codes in sub procedures

Please strdata has the data in hex format


Code:
Dim lngStatus as integer
lngStatus = OpenSocket("192.168.1.197", 8888)


lngStatus = send(8888, strData, 12280, 0)


lngStatus = recvB(8888, chands, 12280, 0)


Individual codes that are being called above see below

Code:
Option Compare Database
Option Explicit




Public Const COMMAND_ERROR = -1
Public Const RECV_ERROR = -1
Public Const NO_ERROR = 0
Public socketId As Long
Public Const ScpiPort = 8888


'Global Variables for WINSOCK
Global State As Integer


Sub CloseConnection()
Dim x As Long
' we close our connection here
    x = CloseSocket(socketId)
    If x = SOCKET_ERROR Then
        MsgBox ("ERROR: closesocket = " + Str$(x))
        Exit Sub
    End If
End Sub


Sub EndIt()
Dim x As Long
    'Shutdown Winsock DLL
    x = WSACleanup()
End Sub




Function OpenSocket(ByVal Hostname As String, ByVal PortNumber As Integer) As Integer
 
    Dim I_SocketAddress As SOCKADDR_IN
    Dim ipAddress As Long
    Dim x As Long
   
    ipAddress = inet_addr(Hostname)


    'Create a new socket
    socketId = socket(AF_INET, SOCK_STREAM, 0)
    If socketId = SOCKET_ERROR Then
        MsgBox ("ERROR: socket = " + Str$(socketId))
        OpenSocket = COMMAND_ERROR
        Exit Function
    End If


    'Open a connection to a server
    I_SocketAddress.sin_family = AF_INET
    I_SocketAddress.sin_port = htons(PortNumber)
    I_SocketAddress.sin_addr = ipAddress
    I_SocketAddress.sin_zero = String$(8, 0)


    x = connect(socketId, I_SocketAddress, Len(I_SocketAddress))
    If socketId = SOCKET_ERROR Then
        MsgBox ("ERROR: connect = " + Str$(x))
        OpenSocket = COMMAND_ERROR
        Exit Function
    End If
    OpenSocket = socketId
End Function


Function SendCommand(ByVal command As String) As Integer
' our communication command...


    Dim strSend As String
    Dim count As Long
   
    strSend = command + vbCrLf
   
    count = send(socketId, ByVal strSend, Len(strSend), 0)
   
    If count = SOCKET_ERROR Then
        MsgBox ("ERROR: send = " + Str$(count))
        SendCommand = COMMAND_ERROR
        Exit Function
    End If
   
    SendCommand = NO_ERROR


End Function


Function RecvAscii(dataBuf As String, ByVal maxLength As Integer) As Integer


    Dim c As String * 1
    Dim length As Integer
    Dim count As Long
   
    dataBuf = ""
    While length < maxLength
        DoEvents
        count = recv(socketId, c, 1, 0)
        If count < 1 Then
            RecvAscii = RECV_ERROR
            dataBuf = Chr$(0)
            Exit Function
        End If
       
        If c = Chr$(10) Then
           dataBuf = dataBuf + Chr$(0)
           RecvAscii = NO_ERROR
           Exit Function
        End If
       
        length = length + count
        dataBuf = dataBuf + c
    Wend
   
    RecvAscii = RECV_ERROR
   
End Function
Function initWinsock() As Boolean ' {


    Dim wsaVersion As Long
        wsaVersion = 512




    Dim rc  As Long
    Dim wsa As WSADATA


    rc = WSAStartup(wsaVersion, wsa)


    If rc <> 0 Then
       initWinsock = False
       Exit Function
    End If


    initWinsock = True


End Function ' }


Main Bas module

Code:
Option Compare Database
Option Explicit
'This is the Winsock API definition file for Visual Basic


'Setup the variable type 'hostent' for the WSAStartup command
Type Hostent
  h_name As Long
  h_aliases As Long
  h_addrtype As String * 2
  h_length As String * 2
  h_addr_list As Long
End Type
Public Const SZHOSTENT = 16




'Set the Internet address type to a long integer (32-bit)
Type in_addr
   s_addr As Long
End Type




'A note to those familiar with the C header file for Winsock
'Visual Basic does not permit a user-defined variable type
'to be used as a return structure.  In the case of the
'variable definition below, sin_addr must
'be declared as a long integer rather than the user-defined
'variable type of in_addr.
Type SOCKADDR_IN
   sin_family As Integer
   sin_port As Integer
   sin_addr As Long
   sin_zero As String * 8
End Type


Public Const WSADESCRIPTION_LEN = 256
Public Const WSASYS_STATUS_LEN = 128
Public Const WSA_DescriptionSize = WSADESCRIPTION_LEN + 1
Public Const WSA_SysStatusSize = WSASYS_STATUS_LEN + 1


'Setup the structure for the information returned from
'the WSAStartup() function.
Type WSADATA
   wVersion As Integer
   wHighVersion As Integer
   szDescription As String * WSA_DescriptionSize
   szSystemStatus As String * WSA_SysStatusSize
   iMaxSockets As Integer
   iMaxUdpDg As Integer
   lpVendorInfo As String * 200
End Type


'Define socket return codes
Public Const INVALID_SOCKET = &HFFFF
Public Const SOCKET_ERROR = -1


'Define socket types
Public Const SOCK_STREAM = 1           'Stream socket
Public Const SOCK_DGRAM = 2            'Datagram socket
Public Const SOCK_RAW = 3              'Raw data socket
Public Const SOCK_RDM = 4              'Reliable Delivery socket
Public Const SOCK_SEQPACKET = 5        'Sequenced Packet socket




'Define address families
Public Const AF_UNSPEC = 0             'unspecified
Public Const AF_UNIX = 1               'local to host (pipes, portals)
Public Const AF_INET = 2               'internetwork: UDP, TCP, etc.
Public Const AF_IMPLINK = 3            'arpanet imp addresses
Public Const AF_PUP = 4                'pup protocols: e.g. BSP
Public Const AF_CHAOS = 5              'mit CHAOS protocols
Public Const AF_NS = 6                 'XEROX NS protocols
Public Const AF_ISO = 7                'ISO protocols
Public Const AF_OSI = AF_ISO           'OSI is ISO
Public Const AF_ECMA = 8               'european computer manufacturers
Public Const AF_DATAKIT = 9            'datakit protocols
Public Const AF_CCITT = 10             'CCITT protocols, X.25 etc
Public Const AF_SNA = 11               'IBM SNA
Public Const AF_DECnet = 12            'DECnet
Public Const AF_DLI = 13               'Direct data link interface
Public Const AF_LAT = 14               'LAT
Public Const AF_HYLINK = 15            'NSC Hyperchannel
Public Const AF_APPLETALK = 16         'AppleTalk
Public Const AF_NETBIOS = 17           'NetBios-style addresses
Public Const AF_MAX = 18               'Maximum # of address families




'Setup sockaddr data type to store Internet addresses
Type sockaddr
  sa_family As Integer
  sa_data As String * 14
End Type
Public Const SADDRLEN = 16




'Declare Socket functions


Public Declare PtrSafe Function CloseSocket Lib "wsock32.dll" Alias "closesocket" (ByVal s As Long) As Long


Public Declare PtrSafe Function connect Lib "wsock32.dll" (ByVal s As Long, addr As SOCKADDR_IN, ByVal namelen As Long) As Long


Public Declare PtrSafe Function htons Lib "wsock32.dll" (ByVal hostshort As Long) As Integer


Public Declare PtrSafe Function inet_addr Lib "wsock32.dll" (ByVal cp As String) As Long


Public Declare PtrSafe Function recv Lib "wsock32.dll" (ByVal s As Long, buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long


Public Declare PtrSafe Function recvB Lib "wsock32.dll" Alias "recv" (ByVal s As Long, buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long


Public Declare PtrSafe Function send Lib "wsock32.dll" (ByVal s As Long, buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long


Public Declare PtrSafe Function socket Lib "wsock32.dll" (ByVal af As Long, ByVal socktype As Long, ByVal protocol As Long) As Long


Public Declare PtrSafe Function WSAStartup Lib "wsock32.dll" (ByVal wVersionRequired As Long, lpWSAData As WSADATA) As Long


Public Declare PtrSafe Function WSACleanup Lib "wsock32.dll" () As Long


Public Declare PtrSafe Function WSAUnhookBlockingHook Lib "wsock32.dll" () As Long


Public Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Public Declare PtrSafe Function setsockopt Lib "wsock32.dll" (ByVal s As Long, ByVal level As Long, ByVal optname As Long, optval As Long, ByVal optlen As Long) As Long
Global Const SO_RCVTIMEO = &H1006
Global Const SOL_SOCKET = &HFFFF&


Just in cases you may want further details on this I have attached the pdf details which shows how it works, now since I'm not using excel I need help so that I use the same code in Microsoft Access VBA and be able to do the following:

(1) Initialize winsock
(2) Send data to a remote gadget plugged separate on its own power source but on the same network
(3) Receive processed data from a remote gadget plugged separate on its own power source but on the same network
(4) Clean the connection after processing
(5) Close the connection

Please note that the same gadget works very well when I use the VBA Bas (open, send receive and close/flash) COM port system , but the challenge is that under this method, the gadget has to be connected to the remote computer and only one person can use it. Now we want multiple users to use the gadget that is the reason why we need the socket method.

Regards

Chris
 

Attachments

  • VBA Winsock.pdf
    221.6 KB · Views: 755
Last edited:

Jon

Access World Site Owner
Staff member
Local time
Today, 13:08
Joined
Sep 28, 1999
Messages
7,303
Welcome to the forums! We are the most active Microsoft Access community on the internet by far, with posts going back over 20 years!

To get started, I highly recommend you read the post below. It contains important information for all new users to this forum.

https://www.access-programmers.co.uk/forums/threads/new-member-read-me-first.223250/

We look forward to having you around here, learning stuff and having fun!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:08
Joined
Feb 19, 2002
Messages
42,970
I won't go so far as to say that you can't do this with Access but that is my first inclination. I coded a few socket programs 30 years ago in COBOL. They sat in memory on the mainframe and whenever a message was placed in a queue by a remote server, my program started up and did something with the message and then went back to sleep. Socket programs are like TSR's (Terminate and stay resident) for Windows which leads me to believe that you are going to need to use a compiled language. Doesn't mean that program can't automat Access if that is what you want but keep in mind that the Socket program code either has to be reentrant or serially reusable, i.e. variables are always cleared when the code starts so old values don't linger.

Yours is an extremely unusual request for Access but we had a similar one quite recently. Are you the same person with a new account?
 

sonic8

AWF VIP
Local time
Today, 14:08
Joined
Oct 27, 2015
Messages
998
Socket client programming, as you intend to do, works in Access.
Socket server programming would also be possible in theory, but is different kind of beast, as Pat mentioned and I would not recommend to try it.

To troubleshoot your code:
1. Try to connect to your gadget using a different client, like Telnet, just to verify the gadget is reachable and listens to requests at all.
2. Run your code step by step to get a clearer idea of the problem than "nothing happens".
 

nectorch

Member
Local time
Today, 15:08
Joined
Aug 4, 2021
Messages
41
Run your code step by step to get a clearer idea of the problem than "nothing happens".

I have done that it now giving an error on sending as below:

send = -1
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:08
Joined
Feb 28, 2001
Messages
26,996
If you look in that PDF attached to your post #1, somewhere in the declarations on pg 5, you would see that you have a SOCKET error because that is what -1 as a return code seems to indicate. Unfortunately, the documentation does not go into depth regarding troubleshooting. For instance, it does not show the definitions of some of the other errors that the code would possibly return, such as COMMAND_ERROR. There might be others, I didn't examine everything in depth.

I believe WinSock is telling you that you have tried to use a socket that isn't open or that you have requested something of the socket that it cannot do. If I read the code correctly, you are attempting to send something that is 12280 bytes long in a single SEND. If you have a protocol manual for the OTHER partner in this transaction, see if they place a limit on transmission size. That looks like an AWFULLY big load to send all at once if that is a decimal number, and worse if it is a hex number. Not saying it can't be done - but it would be good to verify that the partner device will allow it. I say that because USUALLY when sending stuff over sockets, you have to break it up into discrete buffers on your end and allow the other end to reassemble what you send, one buffer at a time. Not always... but it CAN be a protocol requirement. And the thing about this kind of linkage is that you HAVE to remember to play by your partner's rules if at all possible.

As to Pat's discussion regarding client/server transactions...

At least in theory you could tell Windows that you need to wait for a "recv" operation to be satisified, probably with a flag setting. However, I didn't see the discussion of flag definitions in that PDF either.

The main difficulty with this style of operation is that since Access is single-threaded, you will do nothing else with the program until it gets a valid reception. Or if you can't set up a "receive and wait for it" then you have to use a looping method where you send your message and attempt to receive. But if you get back nothing, trigger a form timer or a sleep timer inside your code and wait some amount of time, then receive again.

The logic of this style of "loop and wait" programming can get quite tedious. The only difference between Pat's TSR comment and this description is that with Windows and Access, you have to NOT terminate, so the only way to stay resident is to enter into a voluntary WAIT state for the Windows Process Manager. Access will NOT like that, but even worse, the display will be tied up while waiting so will appear to have gone "dead" on you. There are other pitfalls here.
 

nectorch

Member
Local time
Today, 15:08
Joined
Aug 4, 2021
Messages
41
The Doc Man

Thank you so much, you have some points here , after implementing the sleep function , the code is now generating again an error below, but I'm able to ping the gadget from the remote computer see the ping details

Code:
lngStatus = OpenSocket("192.168.1.197", 8888)


lngStatus = send(8888, strData, 12280, 0)
Dim ival As Long, sv As Long


ival = 1000    ' Time in milliseconds
sv = LenB(ival)
x = setsockopt(socketId, SOL_SOCKET, SO_RCVTIMEO, ival, LenB(sv))


' Read maximum of bytes from TCP/IP Port.Please note while 20 = Length


lngStatus = recvB(8888, chanda, 12280, 0)


Just in case you want to look at the data that I'm sending as well in cases I'm making a mistake:

- Header 1 = The first byte of package header 0x1A
- Header 2 = The second byte of package header Ox5D
- Command Key = 0x02
- Length = The length of the content, big-endian
- Content = The Json based business data
-CRC = Two-Byte verification (CRC), it will be generated by bytes start from
Header 1 up to content

Actual Final data extracted before sending using the function to help audit the data see below:

Code:
strData = BuildData(JsonConverter.ConvertToJson(transaction, Whitespace:=3))


n = FreeFile()
Open "C:\Users\chris.hankwembo\Desktop\WinRequest\Test.txt" For Output As #n
Print #n, ShowHex(strData)
Close #n
MsgBox "strData:" & vbCrLf & ShowHex(strData)

Data finally prepared to be sent via the send function


Code:
0000 =  1A 5D 02 00 00 03 D5 7B 0D 0A 20 20 20 22 50 6F | ..]..........{....   "Po
0010 =  73 56 65 6E 64 6F 72 22 3A 20 22 4E 65 63 74 6F | sVendor": "Necto
0020 =  72 20 50 72 69 6D 65 20 41 63 63 6F 75 6E 74 69 | r Prime Accounti
0030 =  6E 67 20 53 6F 6C 75 74 69 6F 6E 73 22 2C 0D 0A | ng Solutions",....
0040 =  20 20 20 22 50 6F 73 53 6F 66 74 56 65 72 73 69 |    "PosSoftVersi
0050 =  6F 6E 22 3A 20 22 32 2E 30 2E 30 2E 31 22 2C 0D | on": "2.0.0.1",..
0060 =  0A 20 20 20 22 50 6F 73 4D 6F 64 65 6C 22 3A 20 | ..   "PosModel":
0070 =  22 43 61 70 2D 32 30 31 37 22 2C 0D 0A 20 20 20 | "Cap-2017",....  
0080 =  22 50 6F 73 53 65 72 69 61 6C 4E 75 6D 62 65 72 | "PosSerialNumber
0090 =  22 3A 20 22 31 30 30 31 30 30 30 30 31 38 32 39 | ": "100100001829
00A0 =  22 2C 0D 0A 20 20 20 22 49 73 73 75 65 54 69 6D | ",....   "IssueTim
00B0 =  65 22 3A 20 22 32 30 32 31 30 38 30 36 31 34 32 | e": "20210806142
00C0 =  35 30 34 22 2C 0D 0A 20 20 20 22 54 72 61 6E 73 | 504",....   "Trans
00D0 =  61 63 74 69 6F 6E 54 79 70 65 22 3A 20 30 2C 0D | actionType": 0,..
00E0 =  0A 20 20 20 22 50 61 79 6D 65 6E 74 4D 6F 64 65 | ..   "PaymentMode
00F0 =  22 3A 20 30 2C 0D 0A 20 20 20 22 53 61 6C 65 54 | ": 0,....   "SaleT
0100 =  79 70 65 22 3A 20 30 2C 0D 0A 20 20 20 22 4C 6F | ype": 0,....   "Lo
0110 =  63 61 6C 50 75 72 63 68 61 73 65 4F 72 64 65 72 | calPurchaseOrder
0120 =  22 3A 20 6E 75 6C 6C 2C 0D 0A 20 20 20 22 43 61 | ": null,....   "Ca
0130 =  73 68 69 65 72 22 3A 20 22 41 64 6D 69 6E 20 4D | shier": "Admin M
0140 =  61 6E 61 67 65 72 22 2C 0D 0A 20 20 20 22 42 75 | anager",....   "Bu
0150 =  79 65 72 54 50 49 4E 22 3A 20 22 31 30 32 32 35 | yerTPIN": "10225
0160 =  33 30 30 33 54 22 2C 0D 0A 20 20 20 22 42 75 79 | 3003T",....   "Buy
0170 =  65 72 4E 61 6D 65 22 3A 20 22 4E 64 6F 6C 61 20 | erName": "Ndola
0180 =  50 6C 61 6E 6E 69 6E 67 20 47 72 6F 75 70 22 2C | Planning Group",
0190 =  0D 0A 20 20 20 22 42 75 79 65 72 54 61 78 41 63 | ....   "BuyerTaxAc
01A0 =  63 6F 75 6E 74 4E 61 6D 65 22 3A 20 22 4E 64 6F | countName": "Ndo
01B0 =  6C 61 20 50 6C 61 6E 6E 69 6E 67 20 47 72 6F 75 | la Planning Grou
01C0 =  70 22 2C 0D 0A 20 20 20 22 42 75 79 65 72 41 64 | p",....   "BuyerAd
01D0 =  64 72 65 73 73 22 3A 20 22 42 6F 78 22 2C 0D 0A | dress": "Box",....
01E0 =  20 20 20 22 42 75 79 65 72 54 65 6C 22 3A 20 6E |    "BuyerTel": n
01F0 =  75 6C 6C 2C 0D 0A 20 20 20 22 4F 72 69 67 69 6E | ull,....   "Origin
0200 =  61 6C 49 6E 76 6F 69 63 65 43 6F 64 65 22 3A 20 | alInvoiceCode":
0210 =  6E 75 6C 6C 2C 0D 0A 20 20 20 22 4F 72 69 67 69 | null,....   "Origi
0220 =  6E 61 6C 49 6E 76 6F 69 63 65 4E 75 6D 62 65 72 | nalInvoiceNumber
0230 =  22 3A 20 6E 75 6C 6C 2C 0D 0A 20 20 20 22 4D 65 | ": null,....   "Me
0240 =  6D 6F 22 3A 20 6E 75 6C 6C 2C 0D 0A 20 20 20 22 | mo": null,....   "
0250 =  43 75 72 72 65 6E 63 79 2D 54 79 70 65 22 3A 20 | Currency-Type":
0260 =  22 5A 4D 57 22 2C 0D 0A 20 20 20 22 43 6F 6E 76 | "ZMW",....   "Conv
0270 =  65 72 73 69 6F 6E 2D 52 61 74 65 22 3A 20 31 2C | ersion-Rate": 1,
0280 =  0D 0A 20 20 20 22 49 74 65 6D 73 22 3A 20 5B 0D | ....   "Items": [..
0290 =  0A 20 20 20 20 20 20 7B 0D 0A 20 20 20 20 20 20 | ..      {....    
02A0 =  20 20 20 22 49 74 65 6D 49 64 22 3A 20 31 2C 0D |    "ItemId": 1,..
02B0 =  0A 20 20 20 20 20 20 20 20 20 22 44 65 73 63 72 | ..         "Descr
02C0 =  69 70 74 69 6F 6E 22 3A 20 22 46 72 75 69 74 20 | iption": "Fruit
02D0 =  4A 75 69 63 65 22 2C 0D 0A 20 20 20 20 20 20 20 | Juice",....      
02E0 =  20 20 22 42 61 72 43 6F 64 65 22 3A 20 22 32 30 |   "BarCode": "20
02F0 =  30 30 22 2C 0D 0A 20 20 20 20 20 20 20 20 20 22 | 00",....         "
0300 =  51 75 61 6E 74 69 74 79 22 3A 20 31 2C 0D 0A 20 | Quantity": 1,....
0310 =  20 20 20 20 20 20 20 20 22 55 6E 69 74 50 72 69 |         "UnitPri
0320 =  63 65 22 3A 20 38 36 2C 0D 0A 20 20 20 20 20 20 | ce": 86,....    
0330 =  20 20 20 22 44 69 73 63 6F 75 6E 74 22 3A 20 30 |    "Discount": 0
0340 =  2C 0D 0A 20 20 20 20 20 20 20 20 20 22 54 61 78 | ,....         "Tax
0350 =  4C 61 62 65 6C 73 22 3A 20 5B 0D 0A 20 20 20 20 | Labels": [....  
0360 =  20 20 20 20 20 20 20 20 22 41 22 0D 0A 20 20 20 |         "A"....  
0370 =  20 20 20 20 20 20 5D 2C 0D 0A 20 20 20 20 20 20 |       ],....    
0380 =  20 20 20 22 54 6F 74 61 6C 41 6D 6F 75 6E 74 22 |    "TotalAmount"
0390 =  3A 20 38 36 2C 0D 0A 20 20 20 20 20 20 20 20 20 | : 86,....        
03A0 =  22 49 73 54 61 78 49 6E 63 6C 75 73 69 76 65 22 | "IsTaxInclusive"
03B0 =  3A 20 74 72 75 65 2C 0D 0A 20 20 20 20 20 20 20 | : true,....      
03C0 =  20 20 22 52 52 50 22 3A 20 30 0D 0A 20 20 20 20 |   "RRP": 0....  
03D0 =  20 20 7D 0D 0A 20 20 20 5D 0D 0A 7D 00 13       |   }....   ]....}....


The error mentioned above keep on coming see the screen shoot:

ErrorSending.png


Ping details from the remote computer on the same router/network

bytes writes.png
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:08
Joined
Feb 19, 2002
Messages
42,970
Since you know how to write in C, why not just use that language for the socket and write the message to a table and start Access to process it.
 

nectorch

Member
Local time
Today, 15:08
Joined
Aug 4, 2021
Messages
41
The process will to hard to bring it into the accounting software which was written in Ms Access VBA, however, there is also plan B. If I use the utility software from the third party then all the work stations will be able to use that gadget since the shared port will be visible to all computers but its too expensive to have it distributed with my software I will need $3000.00.

I have the COM port system which I did in VBA and its working ok, but only the person physically connected to the gadget can use not all on the same network
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:08
Joined
Feb 28, 2001
Messages
26,996
The fact that you can ping it eliminates general network connectivity problems. However, PING is a protocol from the User Datagram Protocol set, which is generally "connectionless." You, on the other hand, are attempting to set up a socket to a specific port on your "gadget" - which implies you are trying to do something in the TCP family - Transmission Control Protocol. Therefore, the ability to PING eliminates having issues with layer 1 (physical), layer 2 (point to point), and layer 3 (end to end) protocols. However, the higher level protocols related to session and presentation layers are still up for grabs.

Your data dump reveals several cases of the hex sequence 0D 0A which is a newline, or the vbCRLF sequence. So that means your returned values are in the ASCII text group (unless it is UTF-n, but I can't tell that from the dump).

I'm going to ask why you told SEND that you wanted to send 12280 bytes, 'cause that's a BIG number. From where did you get it? Do you actually have 12280 bytes of data to send? I ask that because the stuff you showed us was more like 951 bytes of data returned from your gadget, which is about right for a typical TCP sequence that breaks up longer transmissions into 1 KB (approximately) chunks. If that is part of a VPN, the overhead of the VPN will be quite a bit vs. the "standard" 1500 byte limit for most networking devices. I've seen VPNs with less than 1 KB of usable space. If that 12280 is somehow an encoded value, that would explain a lot. Otherwise, that might be enough to instantly make the gadget object, and if so, that is where you get the SEND error.
 

nectorch

Member
Local time
Today, 15:08
Joined
Aug 4, 2021
Messages
41

The_Doc_Man


I do agree with you 100% in all your points:

(1) The data I'm sending is in hex format as per requirement of the gadget , True
(2) The data I'm expecting to receive is suppose to be in the same format , the way I receive it in COM PORT system
(3) After changing the data length to 1 that is when I'm receiving those errors on the socket, I did this to see whether my VBA code is working or not.

Code:
Call initWinsock
lngStatus = OpenSocket("192.168.1.197", 8888)


lngStatus = send(8888, strData, 1, 0)
Dim ival As Long, sv As Long


ival = 1000    ' Time in milliseconds
sv = LenB(ival)
x = setsockopt(socketId, SOL_SOCKET, SO_RCVTIMEO, ival, LenB(sv))


' Read maximum of bytes from TCP/IP Port.Please note while 20 = Length


lngStatus = recvB(8888, chanda, 1, 0)


The reasons why I'm using 12280 length is because I measure the data that is received via the COM port system which we are currently using at the moment it looks like the below attachment, I'm dong so to avoid leaving some garbage inside the port :

Actual receipt from the same gadget when using the COM PORT SYSTEM

See the text attachment
 

Attachments

  • test.txt
    34.2 KB · Views: 390

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:08
Joined
Feb 28, 2001
Messages
26,996
You might wish to read through this thread: https://groups.google.com/g/comp.os.ms-windows.programmer.win32/c/IGr0VxjvPOo?pli=1

Here is a discussion of some potential value: https://docs.microsoft.com/en-us/troubleshoot/windows/win32/data-segment-tcp-winsock

And here is a critical link for error processing: https://docs.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2
In particular, this reference points out that your returned error is GENERIC and needs to be analyzed by a SECOND call to WSAGetLastError to get more information on the ACTUAL cause of the error.

Looks like some binary stuff is coming back in the latter part of that dump. There will be no possible way for me to decipher that information without a detailed set of manuals for the gadget, and even then, I'm not sure I would understand it because it is not my topic of expertise. I'm originally a chemist and became a system analyst.

To be honest, we are reaching the end of my ability to do much more than advise on general concepts. You are now getting a lot deeper into the WinSock stuff than I ever did. My ability to analyze this far comes from my U.S. Navy security certifications, but since I was mostly a system admin, I never had to professionally delve into the byte-twiddling of network stuff. I'm not saying you're on your own here, because (a) I can still offer suggestions and (b) there are others here who might have insights. But I'm just saying I'm getting out of my depth. I sense some experimentation in your future.
 

Users who are viewing this thread

Top Bottom