Jump to content

Another TCP server (written with my socket_UDF)


Recommended Posts

Hello, I just made another TCP server. I will use the server for data collection and a lot of clients can get the data from TCP. I now use it for UDP broadcast some devices to get their IP's. You can also use it for chat's.

It is very low level, but so you have more options than with the pure AutoIt functions.

Just start the server in SciTE to see the console outputs. Then you can start up to 63 clients to talk to the server.

I hope you like it.

Server script:

#include "socket_UDF.au3"
;funkey 2013.06.21

_WSAStartup()

Global $iSocket
Global $iReturn
Global $iPort = 20500
Global $sIP_Connected
Global $iPort_Connected
Global $tBuffer = DllStructCreate("char buffer[512]")


$iSocket = _socket($AF_INET, $SOCK_STREAM, $IPPROTO_TCP)
ConsoleWrite("Listen Socket: " & $iSocket & @CRLF)

Global $tReUse = DllStructCreate("BOOLEAN reuse")
DllStructSetData($tReUse, "reuse", True) ; enable reusing the same port
$iReturn = _setsockopt($iSocket, $SOL_SOCKET, $SO_REUSEADDR, $tReUse) ; set reusing option for the port
If $iReturn Then
ConsoleWrite("SetSockOpt error setting reusing the same port!. Windows Sockets Error Codes: " & _WSAGetLastError() & @CRLF)
EndIf

$iReturn = _bind($iSocket, @IPAddress1, $iPort) ;local IP-Address and port to use
If $iReturn Then
ConsoleWrite("Bind error: " & $iReturn & @CRLF) ; 0 is OK
EndIf

$iReturn = _listen($iSocket, 1)
If $iReturn Then
ConsoleWrite("Listen error: " & $iReturn & @CRLF) ; 0 is OK
EndIf

Global $iNewSocket
Global $tReadFds = DllStructCreate($tagFd_set)
Global $tReadFds_Copy = DllStructCreate($tagFd_set)
_FD_ZERO($tReadFds)
_FD_SET($iSocket, $tReadFds)
Global $iSocketMax = $iSocket
Global $iSocketNow
Global $sDataRcv

While 1

DllCall('ntdll.dll', 'none', 'RtlMoveMemory', 'struct*', $tReadFds_Copy, 'struct*', $tReadFds, 'ULONG_PTR', DllStructGetSize($tReadFds))

$iReturn = _select($iSocketMax + 1, $tReadFds_Copy, 2000) ;timeout 2 seconds
If _FD_ISSET($iSocket, $tReadFds_Copy) Then
$iNewSocket = _accept($iSocket, $sIP_Connected, $iPort_Connected)
_FD_SET($iNewSocket, $tReadFds)
_FD_SHOW($tReadFds)
If $iNewSocket > $iSocketMax Then $iSocketMax = $iNewSocket
ConsoleWrite("New connected socket: " & $iNewSocket & @TAB & "IP: " & $sIP_Connected & @TAB & "Port: " & $iPort_Connected & @LF)
EndIf

For $i = 2 To DllStructGetData($tReadFds, "fd_count")
$iSocketNow = DllStructGetData($tReadFds, "fd_array", $i)
If _FD_ISSET($iSocketNow, $tReadFds_Copy) Then
DllCall('ntdll.dll', 'none', 'RtlZeroMemory', 'struct*', $tBuffer, 'ULONG_PTR', DllStructGetSize($tBuffer))
If _recv($iSocketNow, $tBuffer) = $SOCKET_ERROR Then
_closesocket($iSocketNow)
_FD_CLR($iSocketNow, $tReadFds)
Else
$sDataRcv = DllStructGetData($tBuffer, 1)
ConsoleWrite("Data received: " & $sDataRcv & @LF)
If $sDataRcv == "CloseServer!" Then
ExitLoop 2
EndIf
EndIf
EndIf
Next

WEnd

For $i = 1 To DllStructGetData($tReadFds, "fd_count")
_closesocket(DllStructGetData($tReadFds, "fd_array", $i))
Next

_WSACleanup()


Func _FD_SHOW(ByRef $tFd_set)
For $i = 1 To DllStructGetData($tFd_set, "fd_count")
ConsoleWrite($i & ":" & @TAB & DllStructGetData($tFd_set, "fd_array", $i) & @LF)
Next
EndFunc ;==>_FD_SHOW

socket_UDF and example.rar

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to post
Share on other sites
  • 4 months later...
  • 2 weeks later...

Hi funkey

nice,

thanks for sharing

....  I now use it for UDP broadcast some devices to get their IP's. ....

 

could you please post an example to achieve this?

thanks

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to post
Share on other sites
  • 3 weeks later...

 

Hi funkey

 

nice,

thanks for sharing

 

 

could you please post an example to achieve this?

 

thanks

This depends on devices you have and is different for every kind.

I have scripts to list all 'Siemens Sentron devices' and another one to list all 'Moxa Serial Device Servers' in our network.

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to post
Share on other sites

For example I send a few characters via UDP broadcast on a specified port (that's why I build that UDF - AutoIt does not support this, or this is buggy) to my network. Only devices that wait for information on that port and that know the characters send the requested information back.

But what information do you want to get from what devices?

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to post
Share on other sites

I do not know if it's possible, but i wonder if an unknown device that responds to a ping, can be asked to respond what type of device is himself, for example if it's a router or a network printer or something else?
or maybe is possible the opposite of this?.So that I can broadcast for specific devices types (network printers for example?) and get response from (all) that type of devices only?

Edited by PincoPanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to post
Share on other sites

If think, that is not possible for all devices, but for some devices you could get the MAC address from IP address and then look what company made the device (for example use http://wintelguy.com/index.pl to find this out). Here is a script to receive the MAC address via ARP request:

#include "socket_UDF.au3"

_WSAStartup()

Global $MAC = _SendARP(@IPAddress1)
ConsoleWrite("Local MAC address: " & $MAC & @LF)
Global $MAC = _SendARP("192.168.3.4")
ConsoleWrite("Remote MAC address: " & $MAC & @LF)

_WSACleanup()

Func _SendARP($DestIP)
Local $DestAddress = _inet_addr($DestIP)
Local $tMacAddr = DllStructCreate("BYTE[8]")
Local $aRet = DllCall("Iphlpapi.dll", "DWORD", "SendARP", "ULONG", $DestAddress, "ULONG", 0, "struct*", $tMacAddr, "ULONG*", 8)
Local $sMAC = ""
Switch $aRet[0]
Case 0
For $i = 1 To $aRet[4] - 1
$sMAC &= Hex(DllStructGetData($tMacAddr, 1, $i), 2) & "-"
Next
$sMAC &= Hex(DllStructGetData($tMacAddr, 1, $i), 2)
Return $sMAC
Case 31
Return "ERROR_GEN_FAILURE"
Case 87
Return "ERROR_INVALID_PARAMETER"
Case 1784
Return "ERROR_INVALID_USER_BUFFER"
Case 67
Return "ERROR_BAD_NET_NAME"
Case 1168
Return "ERROR_NOT_FOUND"
EndSwitch
EndFunc

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to post
Share on other sites
  • 2 years later...
  • 5 years later...

Thanks funkey for good UDF.
Sorry for bad english. I have a problem with socket_UDF. I don't know different of UDF and TCP from autoit but my script return different.

This is console return.

Quote

Socket: 704
Connect error: 0
Bytes sent: 478
Bytes received: -1
Data received: 
Rev:0xBA2508E9071AB425520408061001721F080A121BC490C4836E67206E68E1BAAD70207468C3A06E682063C3B46E6720FA01892508E8361283250AD1030A04082410000A04085410010A04085210000A04085310005208080F10ABE5ADDE03520E08D10F10FFFFFFFFFFFFFFFFFF01520E08E10F10FFFFFFFFFFFFFFFFFF015204082010005204084010005205089E011000520508A7011000520508A9011014520508B0011000520508A8011001520508B1011000520508AB011001520508B7011000520508CB011000520408481000520508CD0110005204080610015204080510005204083F10005A0608071080B5185A0508900110005A09082310E2BE88DBD92F5A04085210005A04085310005A0608AA0110D00F5A0508910110005A0508920110006A0B083911000000000000000072100809120C4E506C61793130353634373172330808122F68747470733A2F2F7374617469632E6E706C61792E6D6F62692F646C632F6176617461722F467265652F312E706E677204080D12007204081012007204080E12007204081912007204081A120072100851120C6E706C617931303536343731720408611200720408601200720508D3011200720508D4011200720508D501120072120815120E636F6D2E686474742E6E706C6179721F080A121BC490C4836E67206E68E1BAAD70207468C3A06E682063C3B46E6720CA010508D6011200CA0104083A12000A595204083D10005204084710005204083E1000520408481000520408201000520408401000520508B10110005204083F10005A080849108090FBD30962070842150000803FCA01120846120E0A0CF403D00FC03EA8C301F093090A5BFA0158086E12540A0C5204086F10005204087010010A0C5204086F10015204087010010A0C5204086F10025204087010010A0C5204086F10035204087010010A0C5204086F10045204087010000A0C5204086F10055204087010010A8705FA018305083C12FE040A660A04084410000A04084B10005204082010005204083D10005204083E10005204083F100052040840100052040841100052040845100A5A0908D701108090FBD3095A0808D8011080B8992962070842150000803F62070843150000803F6207084E15000000000A670A04084410000A04084B10015204082010015205083D10F4035204083E107D5204083F10015204084010005204084110055204084510145A0908D701108090FBD3095A0808D8011080F4EE0662070842150000C03F62070843150000C03F6207084E15000020410A680A04084410000A04084B10015204082010025205083D10D00F5205083E10F4035204083F100252040840100052040841100A5204084510325A0908D7011080D8F8BD0E5A0808D8011080F4EE066207084215000000406207084315000000406207084E15000070410A680A04084410000A04084B10015204082010035205083D10C03E5205083E10D00F5204083F100352040840100152040841100F5204084510645A0908D7011080A0F6A7135A0808D8011080F4EE066207084215000040406207084315000040406207084E150000A0410A6A0A04084410010A04084B10015204082010045206083D10A8C3015205083E10EA305204083F1004520408401002520408411014520508451096015A0908D7011080D0E7A3305A0808D8011080F4EE066207084215000080406207084315000080406207084E150000C8410A6B0A04084410010A04084B10015204082010055206083D10F093095206083E10E0A8015204083F100552040840100352040841101E5205084510F4035A0908D7011080D8C4BD755A0808D8011080F4EE0662070842150000A04062070843150000A0406207084E150000F0410A1F5204082E10005204082F100A520408331014520408201001FA0104082712000AC91A520508D4171000520508F2171002FA01AA


This is my script.

#include "socket_UDF.au3"

_WSAStartup()

Global $iSock = _socket($AF_INET, $SOCK_STREAM, $IPPROTO_TCP)
ConsoleWrite("Socket: " & $iSock & @CRLF)

Global $tTimeVal = DllStructCreate("DWORD timeout")
DllStructSetData($tTimeVal, "timeout", 5000) ; Timeout 200ms

$iSockError = _setsockopt($iSock, $SOL_SOCKET, $SO_RCVTIMEO, $tTimeVal) ; set Timeout for recv
If $iSockError Then
    ConsoleWrite("SetSockOpt error setting recv timeout!. Windows Sockets Error Codes: " & _WSAGetLastError() & @CRLF)
    _closesocket($iSock)
    TCPShutdown() ;WSACleanup
EndIf

;~ Global $iBind = _bind($iSock, @IPAddress1, 20000) ;local IP-Address and port to use
;~ ConsoleWrite("Bind error: " & $iBind & @CRLF) ; 0 is OK

Global $iConnect = _connect($iSock, "3.1.168.177", 7103)
ConsoleWrite("Connect error: " & $iConnect & @CRLF) ; 0 is OK

If $iConnect Then
    ConsoleWrite("Windows Sockets Error Codes: " & _WSAGetLastError() & @CRLF)
    _closesocket($iSock)
    TCPShutdown() ;WSACleanup
EndIf

Global $sToSend = "0xec0108900f1ae6010a0408131001520408031000520408051000720a08021206332e31342e31726c0814126865326130386133616162323432666562343638633862336433376434643432332362636131373139626339613034393032233837343236383937323130353632353233353632233030313031313030393331313433372330303a31453a45343a30303a36443a3637720508a501120072120815120e636f6d2e686474742e6e706c6179720408161200721a081712166656566a75705547536869647249796932746a774133720408181200720e088d0112094d6e63414844593536720508df011200"
Global $tSendBuffer = DllStructCreate("char[" & StringLen($sToSend) & "]")
DllStructSetData($tSendBuffer, 1, $sToSend)

Global $iSend = _send($iSock, $tSendBuffer)
ConsoleWrite("Bytes sent: " & $iSend & @CRLF)

Global $tRecvBuffer = DllStructCreate("char[64]")
Global $iRecv = _recv($iSock, $tRecvBuffer)
ConsoleWrite("Bytes received: " & $iRecv & @CRLF)

ConsoleWrite("Data received: " & DllStructGetData($tRecvBuffer, 1) & @CRLF) ;input1;OFF

_closesocket($iSock)
_WSACleanup()

TCPStartup()
Opt("TCPTimeout",5000)
$SK_2 = TCPConnect("3.1.168.177", 7103)
TCPSend($SK_2,$sToSend)
$Rev = TCPRecv($SK_2,10*1024)
ConsoleWrite("Rev:"&$Rev &@CRLF)
TCPCloseSocket($SK_2)
TCPShutdown()

 

Link to post
Share on other sites

Hi VetlteV,

you prepare only 64 bytes for UDF _recv() and 10*1024 bytes for AutoIt internal TCPRecv().

Maybe this is the problem.

 

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to post
Share on other sites

Thanks for reply @funkey,

I tried again but the result is still the same.

I test again on another server and return the same result.
 

Quote

Socket: 692
Connect error: 0
Bytes sent: 48
Bytes received: 250
Data received: HTTP/1.1 200 OK
Date: Sat, 11 Dec 2021 07:33:00 GMT
Server: Apache
X-Powered-By: PHP/7.3.32
Upgrade: h2,h2c
Connection: Upgrade
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

d
11.28.88.76
0

---------------------Autoit TCP0----------------------------------
Rev:HTTP/1.1 200 OK
Date: Sat, 11 Dec 2021 07:33:00 GMT
Server: Apache
X-Powered-By: PHP/7.3.32
Upgrade: h2,h2c
Connection: Upgrade
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

d
11.28.88.76
0

#include "socket_UDF.au3"

_WSAStartup()

Global $iSock = _socket($AF_INET, $SOCK_STREAM, $IPPROTO_TCP)
ConsoleWrite("Socket: " & $iSock & @CRLF)

Global $tTimeVal = DllStructCreate("DWORD timeout")
DllStructSetData($tTimeVal, "timeout", 5000) ; Timeout 200ms

$iSockError = _setsockopt($iSock, $SOL_SOCKET, $SO_RCVTIMEO, $tTimeVal) ; set Timeout for recv
If $iSockError Then
    ConsoleWrite("SetSockOpt error setting recv timeout!. Windows Sockets Error Codes: " & _WSAGetLastError() & @CRLF)
    _closesocket($iSock)
    TCPShutdown() ;WSACleanup
EndIf

;~ Global $iBind = _bind($iSock, @IPAddress1, 20000) ;local IP-Address and port to use
;~ ConsoleWrite("Bind error: " & $iBind & @CRLF) ; 0 is OK

Global $iConnect = _connect($iSock, "184.168.101.36", 80)
ConsoleWrite("Connect error: " & $iConnect & @CRLF) ; 0 is OK

If $iConnect Then
    ConsoleWrite("Windows Sockets Error Codes: " & _WSAGetLastError() & @CRLF)
    _closesocket($iSock)
    TCPShutdown() ;WSACleanup
EndIf

Global $sToSend = "GET /getip.php HTTP/1.1" & @CRLF & "Host: updatevng.com" & @CRLF & @CRLF
Global $tSendBuffer = DllStructCreate("char[" & StringLen($sToSend) & "]")
DllStructSetData($tSendBuffer, 1, $sToSend)

Global $iSend = _send($iSock, $tSendBuffer)
ConsoleWrite("Bytes sent: " & $iSend & @CRLF)

Global $tRecvBuffer = DllStructCreate("char["&10*1024&"]")
Global $iRecv = _recv($iSock, $tRecvBuffer)
ConsoleWrite("Bytes received: " & $iRecv & @CRLF)

ConsoleWrite("Data received: " & DllStructGetData($tRecvBuffer, 1) & @CRLF) ;input1;OFF

_closesocket($iSock)
_WSACleanup()

TCPStartup()
Opt("TCPTimeout",5000)
$SK_2 = TCPConnect("184.168.101.36", 80)
TCPSend($SK_2,$sToSend)
$Rev = TCPRecv($SK_2,10*1024)
ConsoleWrite("Rev:"&$Rev &@CRLF)
TCPCloseSocket($SK_2)
TCPShutdown()

 

Link to post
Share on other sites

For me it works, maybe I have another version of the socket_UDF.

See attached.

socket_UDF.au3

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to post
Share on other sites

I just took your code....

 

 

Console output:

Socket: 632
Connect error: 0
Bytes sent: 48
Bytes received: 227
Data received: HTTP/1.1 200 OK
Date: Sat, 11 Dec 2021 11:55:43 GMT
Server: Apache
X-Powered-By: PHP/7.3.32
Upgrade: h2,h2c
Connection: Upgrade
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8


Rev:HTTP/1.1 200 OK
Date: Sat, 11 Dec 2021 11:55:43 GMT
Server: Apache
X-Powered-By: PHP/7.3.32
Upgrade: h2,h2c
Connection: Upgrade
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

 

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to post
Share on other sites

Oh no it's not like that.

Host need test.

Global $iConnect = _connect($iSock, "3.1.168.177", 7103)

Data to send

Global $sToSend = "0xec0108900f1ae6010a0408131001520408031000520408051000720a08021206332e31342e31726c0814126865326130386133616162323432666562343638633862336433376434643432332362636131373139626339613034393032233837343236383937323130353632353233353632233030313031313030393331313433372330303a31453a45343a30303a36443a3637720508a501120072120815120e636f6d2e686474742e6e706c6179720408161200721a081712166656566a75705547536869647249796932746a774133720408181200720e088d0112094d6e63414844593536720508df011200"

And result need or like that.

0xBA2508E9071AB425520408061001721F080A121BC490C4836E67206E68E1BAAD70207468C3A06E682063C3B46E6720FA01892508E8361283250AD1030A04082410000A04085410010A04085210000A04085310005208080F10ABE5ADDE03520E08D10F10FFFFFFFFFFFFFFFFFF01520E08E10F10FFFFFFFFFFFFFFFFFF015204082010005204084010005205089E011000520508A7011000520508A9011014520508B0011000520508A8011001520508B1011000520508AB011001520508B7011000520508CB011000520408481000520508CD0110005204080610015204080510005204083F10005A0608071080B5185A0508900110005A09082310E2BE88DBD92F5A04085210005A04085310005A0608AA0110D00F5A0508910110005A0508920110006A0B083911000000000000000072100809120C4E506C61793130353634373172330808122F68747470733A2F2F7374617469632E6E706C61792E6D6F62692F646C632F6176617461722F467265652F312E706E677204080D12007204081012007204080E12007204081912007204081A120072100851120C6E706C617931303536343731720408611200720408601200720508D3011200720508D4011200720508D501120072120815120E636F6D2E686474742E6E706C6179721F080A121BC490C4836E67206E68E1BAAD70207468C3A06E682063C3B46E6720CA010508D6011200CA0104083A12000A595204083D10005204084710005204083E1000520408481000520408201000520408401000520508B10110005204083F10005A080849108090FBD30962070842150000803FCA01120846120E0A0CF403D00FC03EA8C301F093090A5BFA0158086E12540A0C5204086F10005204087010010A0C5204086F10015204087010010A0C5204086F10025204087010010A0C5204086F10035204087010010A0C5204086F10045204087010000A0C5204086F10055204087010010A8705FA018305083C12FE040A660A04084410000A04084B10005204082010005204083D10005204083E10005204083F100052040840100052040841100052040845100A5A0908D701108090FBD3095A0808D8011080B8992962070842150000803F62070843150000803F6207084E15000000000A670A04084410000A04084B10015204082010015205083D10F4035204083E107D5204083F10015204084010005204084110055204084510145A0908D701108090FBD3095A0808D8011080F4EE0662070842150000C03F62070843150000C03F6207084E15000020410A680A04084410000A04084B10015204082010025205083D10D00F5205083E10F4035204083F100252040840100052040841100A5204084510325A0908D7011080D8F8BD0E5A0808D8011080F4EE066207084215000000406207084315000000406207084E15000070410A680A04084410000A04084B10015204082010035205083D10C03E5205083E10D00F5204083F100352040840100152040841100F5204084510645A0908D7011080A0F6A7135A0808D8011080F4EE066207084215000040406207084315000040406207084E150000A0410A6A0A04084410010A04084B10015204082010045206083D10A8C3015205083E10EA305204083F1004520408401002520408411014520508451096015A0908D7011080D0E7A3305A0808D8011080F4EE066207084215000080406207084315000080406207084E150000C8410A6B0A04084410010A04084B10015204082010055206083D10F093095206083E10E0A8015204083F100552040840100352040841101E5205084510F4035A0908D7011080D8C4BD755A0808D8011080F4EE0662070842150000A04062070843150000A0406207084E150000F0410A1F5204082E10005204082F100A520408331014520408201001FA0104082712000AC91A520508D4171000520508F2171002FA01AA

 

Link to post
Share on other sites

So you need to declare the data as binary. The inbuild function automatically detects it.

 

#include "socket_UDF.au3"

_WSAStartup()

Global $iSock = _socket($AF_INET, $SOCK_STREAM, $IPPROTO_TCP)
ConsoleWrite("Socket: " & $iSock & @CRLF)

Global $tTimeVal = DllStructCreate("DWORD timeout")
DllStructSetData($tTimeVal, "timeout", 5000) ; Timeout 200ms

$iSockError = _setsockopt($iSock, $SOL_SOCKET, $SO_RCVTIMEO, $tTimeVal) ; set Timeout for recv
If $iSockError Then
    ConsoleWrite("SetSockOpt error setting recv timeout!. Windows Sockets Error Codes: " & _WSAGetLastError() & @CRLF)
    _closesocket($iSock)
    TCPShutdown() ;WSACleanup
EndIf

Global $iConnect = _connect($iSock, "184.168.101.36", 80)
ConsoleWrite("Connect error: " & $iConnect & @CRLF) ; 0 is OK

If $iConnect Then
    ConsoleWrite("Windows Sockets Error Codes: " & _WSAGetLastError() & @CRLF)
    _closesocket($iSock)
    TCPShutdown() ;WSACleanup
EndIf

;~ Global $sToSend = "GET /getip.php HTTP/1.1" & @CRLF & "Host: updatevng.com" & @CRLF & @CRLF
;~ Global $bToSend = Binary($sToSend)
;~ ConsoleWrite($bToSend & @CRLF)

Global $bToSend = Binary("0xec0108900f1ae6010a0408131001520408031000520408051000720a08021206332e31342e31726c0814126865326130386133616162323432666562343638633862336433376434643432332362636131373139626339613034393032233837343236383937323130353632353233353632233030313031313030393331313433372330303a31453a45343a30303a36443a3637720508a501120072120815120e636f6d2e686474742e6e706c6179720408161200721a081712166656566a75705547536869647249796932746a774133720408181200720e088d0112094d6e63414844593536720508df011200")


;~ Global $tSendBuffer = DllStructCreate("char[" & StringLen($sToSend) & "]")
Global $tSendBuffer = DllStructCreate("byte[" & BinaryLen($bToSend) & "]")
;~ DllStructSetData($tSendBuffer, 1, $sToSend)
DllStructSetData($tSendBuffer, 1, $bToSend)

Global $iSend = _send($iSock, $tSendBuffer)
ConsoleWrite("Bytes sent: " & $iSend & @CRLF)

Global $tRecvBuffer = DllStructCreate("char["&10*1024&"]")
Global $iRecv = _recv($iSock, $tRecvBuffer)
ConsoleWrite("Bytes received: " & $iRecv & @CRLF)

ConsoleWrite("Data received: " & DllStructGetData($tRecvBuffer, 1) & @CRLF)

_closesocket($iSock)
_WSACleanup()

 

 

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to post
Share on other sites

You are welcome!

Can you tell me why you want to use this UDF instead of the standard functions?

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By noellarkin
      Readability is a Python Library that emulates the "Reading Mode" used by Browsers, ie it takes an input URL, and returns the simplified HTML. It removes headers, footers and scripts.
      I made a simple server out of it, which takes CLI arguments for server IP and server Port to start the server. Default IP and port are 127.0.0.1:8900
      Example requests that can be made:
      http://127.0.0.1:8900?url=https://google.com&output_type=TITLE
      http://127.0.0.1:8900?url=https://google.com&output_type=SHORT_TITLE
      http://127.0.0.1:8900?url=https://google.com&output_type=CONTENT
      http://127.0.0.1:8900?url=https://google.com&output_type=SUMMARY
      http://127.0.0.1:8900/health (to check if the server is running)
      import http.server import requests import re import logging import sys from readability import Document # Set up logging logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") class RequestHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): # Log the request logging.info(f"Received request: {self.path}") # Regular expression to match URLs URL_REGEX = re.compile(r"^https?://.+$") # Allowed output types ALLOWED_OUTPUT_TYPES = ["TITLE", "SHORT_TITLE", "CONTENT", "SUMMARY"] if self.path == "/health": # This is a health check request, return a 200 status code self.send_response(200) self.send_header("Content-type", "text/plain") self.send_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0") self.end_headers() self.wfile.write(b"OK") else: # Parse the query string to get the URL and output type query_string = self.path[2:] query_params = query_string.split("&") url = query_params[0].split("=")[1] output_type = query_params[1].split("=")[1] # Validate the input if not URL_REGEX.match(url): # URL is invalid self.send_response(400) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write(b"Invalid URL") elif output_type not in ALLOWED_OUTPUT_TYPES: # Output type is invalid self.send_response(400) self.send_header("Content-type", "text/plain") self.send_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0") self.end_headers() self.wfile.write(b"Invalid output type") else: # Input is valid, proceed with processing the request try: doc = Document(requests.get(url).content) output = { "TITLE": doc.title(), "SHORT_TITLE": doc.short_title(), "CONTENT": doc.content(), "SUMMARY": doc.summary() }[output_type] # Send the response self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write(output.encode()) except Exception as e: # Log the error logging.error(f"Error: {e}") # Return an error message to the client self.send_response(500) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write(b"An error occurred while processing the request") # Get the server IP and port from the command line arguments server_ip = sys.argv[1] if len(sys.argv) > 1 else "127.0.0.1" server_port = int(sys.argv[2]) if len(sys.argv) > 2 else 8900 # Create the server and run it indefinitely server_address = (server_ip, server_port) httpd = http.server.HTTPServer(server_address, RequestHandler) # Log an info message when the server starts logging.info("Server started") httpd.serve_forever() Note: make sure you have the readability library https://github.com/buriy/python-readability before using this
      pip install readability-lxml  
    • By mLipok
      Usually when I collect data from DataBase I need to give EndUser a possibility to select rows which should be taken in the processing loop.
      I was searching on the forum and I'm not able to find any UDF or even example of how to select data from array.
      I have my own solutions but I think they are not worth posting on the forum as it is very old code and I am looking for a better solution.

      Could anybody point me to some examples/solutions ?

      Thank you in advance.
      @mLipok
    • By tarretarretarre
      Version 2.x.x and 3.x.x has been moved to branch 3.x
      About Autoit-Socket-IO
      Autoit-Socket-IO is a event driven TCP/IP wrapper heavily inspired from Socket.IO with focus on user friendliness and long term sustainability.
      I constantly want to make this UDF faster and better, so if you have any suggestions or questions (beginner and advanced) Do not hesitate to ask them, I will gladly help!
      Key features
      Simple API 99% data-type serialization thanks to Autoit-Serialize Can easily be extended with your own functionality thanks to Autoit-Events "Educational" examples Data encryption thanks to _<Crypt.au3> Limitations
      Speed. This UDF will sacrifice some speed for convenience Getting started
      Download the script from AutoIt or pull it from the official github repo git@github.com:tarreislam/Autoit-Socket-IO.git and checkout the tag 4.0.0-beta Check out the documentaion Take a look in the examples/ folder Changelog
      To see changes from 3.x.x and 2.x.x please checkout the 3.x branch
      Version 4.0.0-beta (This update break scripts.)
      Code base fully rewritten with Autoit-Events and decoupled to improve code quality and reduce bloat. The new UDF is very different from 3.x.x so please checkout the UPGRADE guide to fully understand all changes Added new documentation documentaion Success stories
      Since December 2017-now I have used version 1.5.0 in an production environment for 150+ clients with great success, the only downtime is planned windows updates and power outages.
       
      Newest version (2020-09-15!)

       
      Older versions (Not supported anymore)
      Autoit-Socket-IO-1.0.0.zip Autoit-Socket-IO-1.1.0.zip Autoit-Socket-IO-1.3.0.zip Autoit-Socket-IO-1.4.0.zip Autoit-Socket-IO-1.5.0.zip
      Autoit-Socket-IO-2.0.0.zip
    • By tarretarretarre
      Version 2.x.x and 3.x.x has been moved to branch 3.x
      About Autoit-Socket-IO
      Autoit-Socket-IO is a event driven TCP/IP wrapper heavily inspired from Socket.IO with focus on user friendliness and long term sustainability.
      I constantly want to make this UDF faster and better, so if you have any suggestions or questions (beginner and advanced) Do not hesitate to ask them, I will gladly help!
      Key features
      Simple API 99% data-type serialization thanks to Autoit-Serialize Can easily be extended with your own functionality thanks to Autoit-Events "Educational" examples Data encryption thanks to _<Crypt.au3> Limitations
      Speed. This UDF will sacrifice some speed for convenience Read more in the official thread
    • By TheXman
      Purpose (from Microsoft's website)
      The HTTP Server API enables applications to communicate over HTTP without using Microsoft Internet Information Server (IIS). Applications can register to receive HTTP requests for particular URLs, receive HTTP requests, and send HTTP responses. The HTTP Server API includes SSL support so that applications can exchange data over secure HTTP connections without IIS.
      Description
      There have been several times in the past that I wanted to either retrieve information from one of my PCs or execute commands on one of my PCs, whether it was from across the world or sitting on my couch.  Since AutoIt is one of my favorite tools for automating just about anything on my PC, I looked for ways to make to make it happen.  Setting up a full blown IIS server seemed like overkill so I looked for lighter weight solutions.  I though about creating my own AutoIt UDP or TCP server but that just wasn't robust enough,  Then I found Microsoft's HTTP Server API and it looked very promising.  After doing a little research into the APIs, I found that it was flexible & robust enough to handle just about any of the tasks that I required now and in the future.  So a while back I decided to wrap the API functionality that I needed into an AutoIt UDF file to allow me to easily create the functionality I needed at the time.  It has come in very handy over the years.  Of course it wasn't all wrapped up with a nice little bow like it is now.  That only happened when I decided to share it with anyone else who could use it or learn from it.
      The example file that I included is a very granular example of the steps required to get a lightweight HTTP Server up and listening for GET requests.  The UDF is a wrapper for the Microsoft APIs.  That means to do anything over and above what I show in the example, one would probably have to have at least a general knowledge of APIs or the ability to figure out which APIs/functions to use, what structures and data is needed to be passed to them, and in what order.  However, the UDF gives a very solid foundation on which to build upon.  Of course, if anyone has questions about the UDF or how to implement any particular functionality, I would probably help to the extent that I could or point you in the right direction so that you can figure out how to implement your own solution.
      The APIs included in the UDF are the ones that I needed in the past to do what I needed to do.  If any additional APIs need to be added to the UDF file, please make those suggestions in the related forum topic.
      Being that this is basically an AutoIt wrapper for the Microsoft API functions, there's no need to create AutoIt-specific documentation.  All of the UDF functions, structures, constants, and enumerations are named after their Microsoft API counterparts.  Therefore, you can refer to Microsoft's extensive documentation of their HTTP Server API.  As stated earlier, if there is one or more APIs that you find yourself needing for your particular solution, please suggest it in the related Example Scripts forum topic.
      Related Links
      Microsoft HTTP Server API - Start Page
      Microsoft HTTP Server API - API v2 Reference
      Microsoft HTTP Server API - Programming Model
×
×
  • Create New...