Search the Community
Showing results for tags 'two way communication'.
This topic will contain simple and working client server communication example along with some basic info for understanding. Examples are based on multi client communication that is happening on one computer (localhost). Changing (if it's not already set correctly) the IP address parameter on server script to reflect your network device IP address will allow you to communicate with other clients on network or even to communicate on internet if your network settings (forwarded router port, ip address on that port is seen from others on internet, you're not trying to do client server from identical computer on identical internet connection) are configured correctly. Every client connecting will need to have server IP address for network (or server public IP address for internet) connection in his client script. So edit client script to correspond to server address TCPConnect('type server ip address in client', 1018). Sometimes firewall can be your problem, if something isn't working turn off firewall and see if it helps. Server will always listen on his Network Adapter IP Address and not on his public address. In some rare occasions server can be set to listen on 0.0.0.0, with that he will listen from any device that he have physically connected on his MB USB or something third (with localhost included for listening). But i would not know the end result of what will happen if some other program that you have installed need that port (or you started 2 servers that interpret one with another). First part will hold just plain code.Second part will hold identical code with comments below commands for more info and understanding hopefully to help new autoit users to understand what and why that something is there.Other parts will hold additional info.First part: Working Code (working in two way communication, with multiple and-or identical client)Server #include <Array.au3> TCPStartup() Dim $Socket_Data $Socket_Data = 0 $Listen = TCPListen(@IPAddress1, 1018, 500) If @error Then ConsoleWrite('!--> TCPListen error number ( ' & @error & ' ), look in the help file (on MSDN link) on func TCPListen to get more info about this error.' & @CRLF) Exit EndIf While 1 For $x = $Socket_Data To 1 Step -1 $Recv = TCPRecv($Socket_Data[$x], 1000000) If $Recv Then MsgBox(0, 'Client Number ' & $x & ' with connected socket identifier ' & $Socket_Data[$x], 'Recived msg from Client: ' & @CRLF & $Recv, 5) TCPSend($Socket_Data[$x], 'bye') TCPCloseSocket($Socket_Data[$x]) _ArrayDelete($Socket_Data, $x) $Socket_Data -= 1 EndIf Next _Accept() WEnd Func _Accept() Local $Accept = TCPAccept($Listen) If $Accept <> -1 Then _ArrayAdd($Socket_Data, $Accept) $Socket_Data += 1 EndIf EndFunc ;==>_AcceptClient TCPStartup() $Socket = TCPConnect(@IPAddress1, 1018) If @error Then ConsoleWrite('!--> TCPConnect error number ( ' & @error & ' ), look in the help file (on MSDN link) on func TCPConnect to get more info about this error.' & @CRLF) Exit EndIf TCPSend($Socket, 'Hi there. My computer name is' & ", " & @ComputerName & ", and its drive serial is " & DriveGetSerial(@HomeDrive)) Do $Recv = TCPRecv($Socket, 1000000) Until $Recv MsgBox(0, 'Recived from server:', $Recv)Second part: Explained Working Code (for upper communication) Third Part: Understanding for how is msg received with TCPRecv on server (or on client) Forth Part: Packets Fifth Part: How to get or check IP address of your device? Sixth Part: How to forward ports? (2 nice youtube tutorials) Seventh Part: Internet communication? If i wrote something incorrectly or wrong please tell me so that i can correct it. Edited: Local and Global, added aditional info about reciving msgs for maxlen buffer.