themax90 Posted December 5, 2005 Share Posted December 5, 2005 (edited) I don't know if this matters, or if it changes anything about TCP commands, but I made a "Newbie Friendly" version of TCP. TCP should already be "Newbie Friendly" but perhaps since I broke this down they can understand a bit more about the TCP Native Commands. The only USEFUL functions are TCPStartServer and TCPStopServer, but I made the other functions just for fun.I ONLY TRULY RECOMMEND TCPStartServer!!!These functions were made to explain TCP as a scripter would understand because sometimes Helpfiles can be misleading to patrons of different languages.TCP Funcs.au3expandcollapse popup#include-once ; ---------------------------------------------------------------------------- ; AutoIt Version: 3.1.1.92 ; Author: Max Gardner <AutoIt Smith;king.of.all@comcast.net> ; ---------------------------------------------------------------------------- ; ---------------------------------------------------------------------------- ; Function: TCPStartServer($Port[, $MaxConnection]) ; $Port = Port To Start Server On ; $MaxConnection = (Optional) Maximum Connections Allowed; Default 1 ; ; Returns: ; Success : Returns Socket and @Error = 0 ; Failure : Returns 0 or 1 and sets @Error to Windows API WSAGetLasterror ; 0 = TCP Services Not Avialible On That Port ; -1 = TCP Services Would Not StartUp ; ---------------------------------------------------------------------------- Func TCPStartServer($Port, $MaxConnect = 1) Local $Socket $Socket = TCPStartup() Select Case $Socket = 0 SetError(@Error) Return -1 EndSelect $Socket = TCPListen(@IpAddress1, $Port, $MaxConnect) Select Case $Socket = -1 SetError(@Error) Return 0 EndSelect SetError(0) Return $Socket EndFunc ; ---------------------------------------------------------------------------- ; Function: TCPAllowConnection($Socket) ; $Socket = The Main Socket As Returned By TCPStartServer ; ; Returns: ; Success : Returns ConnectedSocket and @Error = 0 ; Failure : Returns -1 and sets @Error to Windows API WSAGetLasterror ; ---------------------------------------------------------------------------- Func TCPAllowConnection($Socket) Local $ConnectedSocket $ConnectedSocket = TCPAccept($Socket) Select Case $ConnectedSocket >= 0 SetError(0) Return $ConnectedSocket Case Else SetError(@Error) Return -1 EndSelect EndFunc ; ---------------------------------------------------------------------------- ; Function: TCPReceive($ConnectedSocket, $MaxChar) ; $ConnectedSocket = The Connected Socket As Returned By TCPAllowConnection ; ; Returns: ; Success : Returns Recieved String and @Error = 0 ; Failure : Returns -1 and sets @Error to Windows API WSAGetLasterror ; ---------------------------------------------------------------------------- Func TCPReceive($ConnectedSocket, $MaxChar = 512) Local $Data $Data = TCPRecv($ConnectedSocket, $MaxChar) Select Case $Data = "" SetError(@Error) Return -1 Case Else SetError(0) Return $Data EndSelect EndFunc ; ---------------------------------------------------------------------------- ; Function: TCPSendMessage($ConnectedSocket, $String) ; $ConnectedSocket = The Connected Socket As Returned By TCPAllowConnection ; $String = Data to be sent ; ; Returns: ; Success : Returns 1 and @Error = 0 ; Failure : Returns -1 or 0 and sets @Error to Windows API WSAGetLasterror ; ; ---------------------------------------------------------------------------- Func TCPSendMessage($ConnectedSocket, $String) Local $Sent $Sent = TCPSend($ConnectedSocket, $String) Select Case $Sent = 0 SetError(@Error) Return -1 Case Else SetError(0) Return 1 EndSelect EndFunc ; ---------------------------------------------------------------------------- ; Function: TCPStopServer($MainSocket, $ConnectedSocket) ; $MainSocket = The Socket As Returned By TCPStartServer ; $ConnectedSocket = The Connected Socket As Returned By TCPAllowConnection ; ; Returns: ; Success : Returns 1 and @Error = 0 ; Failure : Returns -1 and sets @Error to Windows API WSAGetLasterror ; 0 = Unable To Shutdown TCP Services ; -1 = Unable To Close Socket/s ; ---------------------------------------------------------------------------- Func TCPStopServer($MainSocket, $ConnectedSocket) Local $Shutdown Select Case $ConnectedSocket <> -1 $ConnectedSocket = TCPCloseSocket($ConnectedSocket) Select Case $ConnectedSocket = 0 SetError(@Error) Return -1 EndSelect EndSelect Select Case $MainSocket <> -1 $MainSocket = TCPCloseSocket($MainSocket) Select Case $MainSocket = 0 SetError(@Error) Return -1 EndSelect EndSelect $Shutdown = TCPShutdown() Select Case $Shutdown = 0 SetError(@Error) Return 0 EndSelect SetError(0) Return 1 EndFuncHere is an example of how it would work server side.#include <Misc.au3> #include 'TCP Funcs.au3' Local $ConnectedSocket = -1 $MainSocket = TCPStartServer(7000) While _IsPressed('1B') = 0 If $ConnectedSocket = -1 Then $ConnectedSocket = TCPAllowConnection($MainSocket) $Data = TCPReceive($ConnectedSocket) If @Error = 0 Then MsgBox(0, "Message Received", $Data) Sleep(100) WEnd TCPStopServer($MainSocket, $ConnectedSocket)As you see it reduces the entire amount of code you need for TCP services from about 70 to around 10ish . Here is a sample sender that you can use to test the example#include 'TCP Funcs.au3' TCPStartup() $Socket = TCPConnect("71.56.132.184", 7000) TCPSendMessage($Socket, "This is a test!") TCPCloseSocket($Socket) TCPShutdown()I hope you like it!AutoIt Smith Edited August 18, 2006 by AutoIt Smith Link to comment Share on other sites More sharing options...
ShaneHale Posted May 16, 2007 Share Posted May 16, 2007 Only recieves one message. Thought because it is looping I should beable to send message 1 then message 2 and so on. Has to be restarted every time. How can I make it recieve multiple messages without restart ove reciever/server ? Link to comment Share on other sites More sharing options...
McGod Posted May 16, 2007 Share Posted May 16, 2007 Way to bump a 2 year old post. [indent][center][u]Formerly Chip[/u][/center]~UDFs~[/indent][u]IRC.au3 - Allows you to connect to IRC ServersINetCon.au3 - Connects/Disconnects/Check Status of InternetHardware Key - Creates a unique hardware hashScriptComm - Allows you to communicate between scripts using WM_COPYDATA[/u][indent]~Programs~[/indent][indent]SimonAu3ForumsIRC Bot~Web Site~Web Autoit Example[/indent][indent][b][/b][/indent][u][/u] Link to comment Share on other sites More sharing options...
themax90 Posted May 16, 2007 Author Share Posted May 16, 2007 You can receive multiple messages. I have no idea what you are talking about. This has been the base of two projects and never once has this happened. Link to comment Share on other sites More sharing options...
andyvl Posted August 26, 2007 Share Posted August 26, 2007 I tried this. But it is not working tcplisten is success (retrieves a socket) tcpaccept results in -1 so rest also (allowconnection -1, tcpreceive also. When running, My msgbox is flashing -1 Even my tcpsendmessage results in a return 0 (failure) Why is this? I've been trying to get something like this to work for a whole day now. But keep getting thos damn -1 returns. Link to comment Share on other sites More sharing options...
testingtest Posted August 27, 2007 Share Posted August 27, 2007 (edited) Way to bump a 2 year old post.It is not really consider bumping if you got a question or there is updated code. This is consider more of a bump A bump isn't always a bad thing, I never seen this post before Edited August 27, 2007 by testingtest Link to comment Share on other sites More sharing options...
Mast3rpyr0 Posted September 21, 2007 Share Posted September 21, 2007 Sorry to bring back an old post but i was wonder on how i could use this same server/client code but beable to have more connections. All data being sent would be the same. It would help alot in managing my network. My UDF's : _INetUpdateCheck() My Programs : GameLauncher vAlpha, InfoCrypt, WindowDesigner, ScreenCap, DailyRemindersPick3GeneratorBackupUtility! Other : Bored? Click Here! Link to comment Share on other sites More sharing options...
jvanegmond Posted September 21, 2007 Share Posted September 21, 2007 Sorry to bring back an old post but i was wonder on how i could use this same server/client code but beable to have more connections. All data being sent would be the same. It would help alot in managing my network.Check out other topics by AutoItSmith. The concept is quite simple, you have an array in which all socket information is stored. When someone new connects, you place the socket number into that array, and when he disconnects you remove him from the array. github.com/jvanegmond Link to comment Share on other sites More sharing options...
themax90 Posted September 21, 2007 Author Share Posted September 21, 2007 Yes, the connection limit is only that of autoits array parameter. It does slow down drastically with very high volumes, but that issue has been addressed in ITS 1.0(TBA) - but thats simply because autoit doesn't have the ability to do more then one thing at a single time. Wow, this is a hella old post, I havn't even programmed in like a year! Link to comment Share on other sites More sharing options...
jvanegmond Posted September 22, 2007 Share Posted September 22, 2007 Wow, this is a hella old post, I havn't even programmed in like a year!I know.. Too bad.. I loved programming with you, you always have great idea's. github.com/jvanegmond Link to comment Share on other sites More sharing options...
jaenster Posted September 22, 2007 Share Posted September 22, 2007 Just dont be a noob.. tcp is already easy in autoit -jaenster Link to comment Share on other sites More sharing options...
Sven Posted April 5, 2008 Share Posted April 5, 2008 (edited) Server has this: #include 'TCP Funcs.au3' Local $ConnectedSocket = -1 $MainSocket = TCPStartServer(7000) While 1 If $ConnectedSocket = -1 Then $ConnectedSocket = TCPAllowConnection($MainSocket) $Data = TCPReceive($ConnectedSocket) If @Error = 0 Then MsgBox(0, "Message Received", $Data) Sleep(100) WEnd TCPStopServer($MainSocket, $ConnectedSocket)oÝ÷ Ø)bz{a²Øb±«¢+Ø¥¹±Õ±Ðí5¥Í¹ÔÌÐì(¥¹±ÕÌäíQ @չ̹ÔÌÌäì)¡½ÑåÍÐ ÌäíÌäì°Ìäí}ÑÍÐÌäì¤()Ý¡¥±Ä(ͱÀ ÄÀÀ¤)]¹()Õ¹}ÑÍÐ ¤(Q AMÑÉÑÕÀ ¤(ÀÌØíM½ÐôQ A ½¹¹Ð¡%AÉÍÌÄ°ÜÀÀÀ¤(Q AM¹5ÍÍ ÀÌØíM½Ð°ÅÕ½ÐíQ¡¥Ì¥ÌÑÍÐÌÌìÅÕ½Ðì¤(Q A ±½ÍM½Ð ÀÌØíM½Ð¤(Q AM¡Õѽݸ ¤)¹Õ¹ How comes only one message is received by the server? I mean, the first time I press 'a', the message (= This is a test!) is sent to the server, which shows it nicely inside a messagebox, however when I press 'a' again, nothing happens. Edited April 5, 2008 by Sven Link to comment Share on other sites More sharing options...
jvanegmond Posted April 5, 2008 Share Posted April 5, 2008 #include 'TCP Funcs.au3' Local $ConnectedSocket = -1 $MainSocket = TCPStartServer(7000) While 1 If $ConnectedSocket = -1 Then $ConnectedSocket = TCPAllowConnection($MainSocket) $Data = TCPReceive($ConnectedSocket) If @Error = 0 Then MsgBox(0, "Message Received", $Data) Else $ConnectedSocket = -1 EndIf Sleep(100) WEnd TCPStopServer($MainSocket, $ConnectedSocket) github.com/jvanegmond Link to comment Share on other sites More sharing options...
Sven Posted April 5, 2008 Share Posted April 5, 2008 (edited) Thank you for your input, greatly appreciated. However, when using your suggestions for both server and client, whenever the client connects to the server, the server keeps spawning '-1' messageboxes. When the hotkey is pressed, server shows the correct message, but then reverts back to looping '-1' messages. Unfortunately I'm unable to figure out why or how to get rid of the -1s. Trying to intercept the -1 messages results in no messageboxes shown at all, even when the right one should appear. Oh, and thank you for the fix for the client. TCPConnect takes quite alot of time, so anything that reduces its use is very welcome. Edited April 5, 2008 by Sven Link to comment Share on other sites More sharing options...
jvanegmond Posted April 6, 2008 Share Posted April 6, 2008 I hope that this helps. It doesn't require any includes. Server: Dim $iMainSocket = 0 Dim $iConnectedSocket = -1 Dim $sData = "" TCPStartup() ; Initialize the TCP functions/library $iMainSocket = TCPListen(@IPAddress1,7000) ; Create a listening socket While 1 If $iConnectedSocket >= 0 Then ; We have a connection $sData = TCPRecv($iConnectedSocket,1024) ; Receive data up If @error Then ; The client has closed the connection. Reset $iConnectedSocket so that we may listen for new connections. $iConnectedSocket = -1 ConsoleWrite("Client has disconnected" & @CRLF) ElseIf $sData Then ; There has been some data sent. MsgBox(0, "Message Received", $sData) EndIf Else ; We do not have a connection, try to accept an incoming connection $iConnectedSocket = TCPAccept($iMainSocket) ; The connection is made, in the next loop that follows it will try to receive data, ; If the connection is not made, the next loop will try to accept an incoming connection. If $iConnectedSocket >= 0 Then ConsoleWrite("Connection established" & @CRLF) EndIf EndIf Wend github.com/jvanegmond Link to comment Share on other sites More sharing options...
McGod Posted April 6, 2008 Share Posted April 6, 2008 An idea for you guys to consider is to add a Endmessage character or something. If you have: TCPSend($Sock, "Message1") TCPSend($Sock, "Message2") and the server lags a little bit or is parsing out messages you can get: Message1Message2 [indent][center][u]Formerly Chip[/u][/center]~UDFs~[/indent][u]IRC.au3 - Allows you to connect to IRC ServersINetCon.au3 - Connects/Disconnects/Check Status of InternetHardware Key - Creates a unique hardware hashScriptComm - Allows you to communicate between scripts using WM_COPYDATA[/u][indent]~Programs~[/indent][indent]SimonAu3ForumsIRC Bot~Web Site~Web Autoit Example[/indent][indent][b][/b][/indent][u][/u] Link to comment Share on other sites More sharing options...
Sven Posted April 6, 2008 Share Posted April 6, 2008 Manadar, thanks to you it finally works. Thank you. Link to comment Share on other sites More sharing options...
okdewit Posted April 11, 2008 Share Posted April 11, 2008 Thanks a lot! Finally a way for my scripts to communicate Just having one small problem: Sending data back from the server to the client... Anyone having an idea how to run a server script on SERVER, which is always listening for incoming connections, and a client script on PC (behind NAT), which connects outwards through a NAT to SERVER. Once the connection is established they both have to set some "connection established" token, and enable other scripts to send data in two directions over this connection... Is that possible? Thanks! Link to comment Share on other sites More sharing options...
jvanegmond Posted April 11, 2008 Share Posted April 11, 2008 Thanks a lot!Finally a way for my scripts to communicate Just having one small problem:Sending data back from the server to the client...Anyone having an idea how to run a server script on SERVER, which is always listening for incoming connections,and a client script on PC (behind NAT), which connects outwards through a NAT to SERVER.Once the connection is established they both have to set some "connection established" token, and enable other scripts to send data in two directions over this connection...Is that possible?Thanks!Ok, I just want to say before we start off. This is not "Finally a way for your scripts to community". The functions to do it have been around for almost 2 whole years. xD But I don't blame you for not knowing.It doesn't matter if the client is behind a NAT, because NAT only involves incoming connections and not outgoing.The connection established token you are talking about is automatically handled by AutoIt and TCP.Other then that, just try any of the scripts above. github.com/jvanegmond Link to comment Share on other sites More sharing options...
Skrip Posted April 11, 2008 Share Posted April 11, 2008 I still have no clue how to send messages to the client from the server. I always seem to get it wrong. Even after looking at other people's scripts... [left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left] Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now