40 posts in this topic
Packet-based TCP comms Helper
If you are talking using text based protocols, Kip's TCP.au3 event driven UDF is great. But what happens if you want to talk to a 3rd party providers device/software that talks using binary format data in packet form? What if the packet size is variable? PTCP is a wrapper around Kip's TCP.au3 that lets you focus on dealing with the packets, rather than figuring out how to determine if there is a complete packet available etc.
For sending, you just send a binary string (the actual packet data)
For receiving, you just get a complete packet - even if the packet size is variable!
How? When you connect, you pass an AutoIt expression that tells PTCP how to tell if a packet is complete. For fixed sized packets, that's easy - just pass the number of bytes. For variable length packets, the expression can contain references to the packet data itself, as well as the number of bytes currently in the Rx buffer. This means you can specify the packet size as some combination of bytes in the packet.
An example might be: "($iAvail>7)?((BitAND(BinaryMid($aPacket,7,1), '0xff'))+(BitShift(BitAND(BinaryMid($aPacket,8,1),'0xff'),-8))+8):(0)"
This says: we need at least 8 bytes of the packet to know the packet size; once we have these 8 bytes, the packet size is stored in bytes 7 & 8 as little-endian (LSB first)
Other than the packetizing details, it's pretty much just Kip's event driven TCP code.
Hope you find it useful.
TCP, UDP problem with receiving data
I've a problem with receiving data on my PC by TCP and UDP. I can send data to another computer, but can't receive it.
I'm not shure where is the problem (on PC or on "another comupers")
This is client code:
;TCP client TCPStartup() $socket = TCPConnect("192.168.0.18", 7777) ;try to connect to server and save number of socket If $socket = -1 Then ;if $socket = -1 then error MsgBox(16, "Error:", "Can't connect to server") EndIf $sendedBytes = TCPSend($socket, "nothing here :)") ;send message to connected socket If $sendedBytes = 0 Then ;if receiving data TCPSend(...) = 0 then error MsgBox(16, "Error", "Can't send message") EndIf TCPCloseSocket($socket) TCPShutdown() and server code:
;TCP server TCPStartup() $mainsocket = TCPListen("192.168.0.18", 7777) ;making main receiving socket While 1 ;receiving loop $acceptedSocket = TCPAccept($mainsocket) ;possible connection to accept If $acceptedSocket <> -1 Then $receivedData = TCPRecv($acceptedSocket, 1024) ;if main socket is connected then receive message MsgBox(64, "Received message!", "Message: " & $receivedData) TCPCloseSocket($acceptedSocket) ;close open connecion EndIf WEnd TCPShutdown()
When server is on PC I can't receive any messages from any other computers.
When client is on PC I can receive messages on any another computer.
I tried to turn off antiviruses and windows firewall but it did't change anything
Error code from client from TCPConnect is 10060 (connection time out) and from TCPSend: 10038
I've found something about 10060 error code, Microsoft says "Connection timed out. A connection attempt failed because the connected party did not properly respond after a period of time, or the established connection failed because the connected host has failed to respond."
So it means that the problem is with time of response but it makes no sense couse "another computer" is Virtual Machine with bridge internet connection from PC (pings beetwen PC and VM are lower than 1ms.
About 10038 error code microsoft says that this is problem with socket and it actually makes sense.
Hi, when a client sends /logout to the server i want that the server find the id of the client and set his online status to 0
Using 'default' I have no problems, but when I do this with 'blank517' gives me id 10 and then for the database remains online
Database while Blank517 logout:
id | username | password | permissions | online |
0 | default | pass1 | 0 | 0 |
1 | Blank517 | pass | 0 | 1 |
Func _Recv_From_Sockets_() For $0 = 1 To $max_connections $Recv = TCPRecv ($Socket_Data[$0],1024) If StringLeft($Recv, 1) = "/" Then If StringInStr($Recv, "logout") Then _SQLite_Query(-1, "SELECT id FROM Users WHERE username = '" & $Socket_Data[$0] & "' AND online = '1';", $hQuery) While _SQLite_FetchData($hQuery, $aRow) = $SQLITE_OK $sMsg &= $aRow; <-- $sMsg = 10 after FetchData WEnd _SQLite_Exec(-1, "UPDATE Users SET online = '0' WHERE id = '" & $sMsg & "';") For $000 = 1 To $max_connections TCPSend($Socket_Data[$000], $Socket_Data[$0] & " ha effettuato il logout") Next TCPCloseSocket($Socket_Data[$0]) $sMsg = Null EndIf Else _Broadcast_To_Sockets_ ($Recv) EndIf Next EndFunc *excuse me for my bad english *
Send message using TCP in autoIT is not work
Hello every body! I am trying to dev an simple chat using TCP. but it don't work for me. Please help me.
I want to in 1 second, Client will send to server a message. And then, Server recv this message and write to Console. But when i run server and client. In server, i was recv only one message. Please tell me why?
In server, i code:
#include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Global $r = @CRLF; Global $ip = "127.0.0.1" Global $port = 1337 Global $client, $iSocket TCPStartup() $iSocket = TCPListen($ip, $port) While 1 $client = TCPAccept($iSocket) If $client <> -1 Then TCPSend($client, "Client is Connected"); EndIf $msg = TCPRecv($client, 1024) if StringLen($msg) <>0 Then ConsoleWrite("Server recv: " &$msg) EndIf TCPSend($client, "Send this msg to client"); Sleep(1000) WEnd In client, i code:
#include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Global $ip = "127.0.0.1" Global $port = 1337 Global $r = @CRLF; TCPStartup() $server = TCPConnect($ip, $port) While 1 TCPSend($server, "Send msg to server") Sleep(1000) WEnd
Autoit real time server with INet
please help me,
my script not working, i want gui run like local real time
#NoTrayIcon #include <INet.au3> #include <String.au3> #include <array.au3> Local $Refresh = 0 ; Set refresh OFF Local $Timer = TimerInit() Local $RefreshEvery = 1 ; seconds ;Global $nCheck Global $link = BinaryToString (InetRead ("http://test.no-ip.info/time.asp")) Global $timeserver = _StringBetween($link, 'id="timeserver">' , '</span>') Global $realtime = $timeserver GuiCreate("Time My Server",200,50) $htime = GuiCtrlCreateLabel("", 10, 10, 180, 20) $btnStart = GUICtrlCreateButton("Start", 40, 30, 57, 17) $btnStop = GUICtrlCreateButton("Stop", 110, 30, 57, 17) GuiSetState() While 1 $nMsg = GUIGetMsg() Select Case $nMsg = $btnStart $Refresh = 1 ; Set refresh ON Case $nMsg = $btnStop $Refresh = 0 ; Set refresh OFF EndSelect If $Refresh And (TimerDiff($Timer) > $RefreshEvery*1000) Then time() $Timer = TimerInit() EndIf WEnd Func time() ;If @SEC <> $nCheck Then GUICtrlSetData($htime, "The current time is " & $realtime) ;$nCheck = @SEC ;EndIf EndFunc