GodlessSinner Posted August 24, 2010 Share Posted August 24, 2010 (edited) Server:expandcollapse popup#include <INet.au3> #Include <File.au3> #Include <Array.au3> #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> Global $edit, $recv, $test ;Global $currentlocation = "start", $str, $Folders, $Files, $ConnectedSocket, $list, $drivelist, $var, $action = "browsing" ;Opt('MustDeclareVars', 1) Global $tmp, $szIPADDRESS = @IPAddress1, $nPORT = 8090, $MainSocket, $GOOEY, $szIP_Accepted, $send, $Button1, $msg ;============================================== ;============================================== ;SERVER!! Start Me First !!!!!!!!!!!!!!! ;============================================== ;============================================== $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 400) $edit = GUICtrlCreateEdit("", 10, 10, 280, 180) $send = GUICtrlCreateInput("Send",10, 200, 280, 20) $Button1 = GUICtrlCreateButton("Send", 10, 230, 280, 50) GUISetState() IP() Example() Func IP() _INetGetSource('doesnt matter what written here!!!') ;_INetGetSource('http://google.com/index.html') ;If @error Then ;MsgBox(0, "", "ip") ;EndIf EndFunc Func Example() TCPStartup() ; Create a Listening "SOCKET". ; Using your IP Address and Port 33891. ;============================================== $MainSocket = TCPListen($szIPADDRESS, $nPORT) ; If the Socket creation fails, exit. If $MainSocket = -1 Then MsgBox(0, "", "Exit here(") Exit EndIf ; Create a GUI for messages ;============================================== ; Initialize a variable to represent a connection ;============================================== $ConnectedSocket = -1 ;Wait for and Accept a connection ;============================================== Do $ConnectedSocket = TCPAccept($MainSocket) Until $ConnectedSocket <> -1 ; GUI Message Loop ;============================================== While 1 $msg = GUIGetMsg() ;MsgBox(0, "", $msg) Switch $msg Case $Button1 If(GUICtrlRead($send) <> "") Then TCPSend($ConnectedSocket, GUICtrlRead($send)) EndIf EndSwitch ; GUI Closed ;-------------------- If $msg = $GUI_EVENT_CLOSE Then ExitLoop ; Try to receive (up to) 2048 bytes ;---------------------------------------------------------------- $recv = TCPRecv($ConnectedSocket, 20048) ; If the receive failed with @error then the socket has disconnected ;---------------------------------------------------------------- If @error Then ExitLoop ; Update the edit control with what we have received ;---------------------------------------------------------------- If $recv <> "" Then GUICtrlSetData($edit, $recv) EndIf WEnd If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket) TCPShutdown() ;TCPStartup() IP() Example() ;============================================== ;$MainSocket = TCPListen($szIPADDRESS, $nPORT) ; $ConnectedSocket = -1 ;Wait for and Accept a connection ;============================================== ;Do ; $ConnectedSocket = TCPAccept($MainSocket) ; Until $ConnectedSocket <> -1 EndIf EndFunc ;==>Example ; Function to return IP Address from a connected socket. ;----------------------------------------------------------------------Client:expandcollapse popupOpt('MustDeclareVars', 1) ;============================================== ;============================================== ;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!! ;============================================== ;============================================== Example() Func Example() ; Set Some reusable info ;-------------------------- Local $ConnectedSocket, $szData ; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address ; Local $szServerPC = @ComputerName ; Local $szIPADDRESS = TCPNameToIP($szServerPC) Local $szIPADDRESS = @IPAddress1 Local $nPORT = 8090 ; Start The TCP Services ;============================================== TCPStartup() ; Initialize a variable to represent a connection ;============================================== $ConnectedSocket = -1 ;Attempt to connect to SERVER at its IP and PORT 33891 ;======================================================= $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT) ; If there is an error... show it If @error Then MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error) ; If there is no error loop an inputbox for data ; to send to the SERVER. Else ;Loop forever asking for data to send to the SERVER While 1 ; InputBox for data to transmit $szData = InputBox("Data for Server", @LF & @LF & "Enter data to transmit to the SERVER:") ; If they cancel the InputBox or leave it blank we exit our forever loop If @error Or $szData = "" Then ExitLoop ; We should have data in $szData... lets attempt to send it through our connected socket. TCPSend($ConnectedSocket, $szData) ; If the send failed with @error then the socket has disconnected ;---------------------------------------------------------------- If @error Then ExitLoop WEnd EndIf EndFunc ;==>ExampleIf server call _InetGetSource, then when I disconnects the client - server is shutting down(Can some1 explain, please, whats wrong?(URL typed in _INEtGets... doesn't matter existence or not, if you comment this line - server continues working after client disconnected) Edited August 24, 2010 by GodlessSinner _____________________________________________________________________________ Link to comment Share on other sites More sharing options...
Tvern Posted August 24, 2010 Share Posted August 24, 2010 (edited) There is a commented line saying: ; If the receive failed with @error then the socket has disconnected. That will shut down the server when the client disconnects. Edit: The line under the commented line I mean. #79. Edited August 24, 2010 by Tvern Link to comment Share on other sites More sharing options...
GodlessSinner Posted August 25, 2010 Author Share Posted August 25, 2010 Yes, if received @error socket disconnects... and listening again for incoming connection, BUT if this line: 28 _INetGetSource('doesnt matter what written here!!!') is uncommented then server exits in this line: 42 If $MainSocket = -1 Then 43 MsgBox(0, "", "Exit here(") 44 Exit 45 EndIf I don't understand why and how _InetGetSource sets $MainSocket to -1. _____________________________________________________________________________ Link to comment Share on other sites More sharing options...
Tvern Posted August 25, 2010 Share Posted August 25, 2010 (edited) Right. I misunderstood. I'm not very familiar with the TCP functions, but there are a few things I noticed: 1. You're calling the example function from itsself without giving it a chance to finish, I don't think you'll get a stack overflow with this script anytime soon, but it's never a good idea to do so. Instead, you could place your initial function calls in a loop. 2. I don't think the example script is supposed to be repeated like this as it seems a waste to TcpOpen() and TcpClose() every time you run the function. (as opposed to once at the start of the script and once on exit) 3. I don't think you're supposed to recreate the main socket every time you run the function, or atleast not without closing it when the function ends. I'm still confused that _INetGetSource, (or more specifically InetRead) makes any difference at all, but it might have something to do with the above 3 issues. I'll have a look if I change to code to work, but don't hold your breath. Edit: I think it works how you want it to now. It InetReads, then waits for the client, connects and repeats if the connection is broken. I still find it odd that the original script ran differently with, or without the InetRead, but I guess it has something to do with the inner workings of AutoIt. p.s: I think that if you want to connect to multiple clients you have to put TCPAccept in the While loop and store the Socket Id's in an array, then TCPRecv, for each ID in the array. expandcollapse popup#include <INet.au3> #Include <File.au3> #Include <Array.au3> #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> Global $edit, $recv, $test ;Global $currentlocation = "start", $str, $Folders, $Files, $ConnectedSocket, $list, $drivelist, $var, $action = "browsing" ;Opt('MustDeclareVars', 1) Global $tmp, $szIPADDRESS = @IPAddress1, $nPORT = 8090, $MainSocket, $GOOEY, $szIP_Accepted, $send, $Button1, $msg ;============================================== ;============================================== ;SERVER!! Start Me First !!!!!!!!!!!!!!! ;============================================== ;============================================== $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 400) $edit = GUICtrlCreateEdit("", 10, 10, 280, 180) $send = GUICtrlCreateInput("Send",10, 200, 280, 20) $Button1 = GUICtrlCreateButton("Send", 10, 230, 280, 50) GUISetState() If Not TCPStartup() Then _Exit("TCPStartup() has failed") $MainSocket = TCPListen($szIPADDRESS, $nPORT) If $MainSocket = -1 Then _Exit("$MainSocket = -1") While 1 _IP() Example() WEnd Func _Exit($msg = "") If $msg Then ConsoleWrite("ExitMessage : " & $msg & @CRLF) TCPShutdown() Exit EndFunc Func _IP() ConsoleWrite("Status: InetReading..." & @CRLF) ;InetReads, Uncomment one ;~ Local $source = InetRead("") ;failing InetRead Local $source = InetRead("http://www.google.com/") ;working InetRead If $source Then ConsoleWrite("Status: InetRead complete" & @CRLF) Else ConsoleWrite("Status: InetRead failed" & @CRLF) EndIf EndFunc Func Example() ; Initialize a variable to represent a connection ;============================================== $ConnectedSocket = -1 ;Wait for and Accept a connection ;============================================== ConsoleWrite("Status: Waiting for connection...." & @CRLF) Do $ConnectedSocket = TCPAccept($MainSocket) Until $ConnectedSocket <> -1 ConsoleWrite("Status: Connected to client" & @CRLF) ; GUI Message Loop ;============================================== While 1 $msg = GUIGetMsg() Switch $msg Case $Button1 If(GUICtrlRead($send) <> "") Then TCPSend($ConnectedSocket, GUICtrlRead($send)) EndIf EndSwitch ; GUI Closed ;-------------------- If $msg = $GUI_EVENT_CLOSE Then ExitLoop ; Try to receive (up to) 2048 bytes ;---------------------------------------------------------------- $recv = TCPRecv($ConnectedSocket, 20048) ; If the receive failed with @error then the socket has disconnected ;---------------------------------------------------------------- If @error Then ConsoleWrite("Status: Connection with client was broken." & @CRLF) ExitLoop EndIf ; Update the edit control with what we have received ;---------------------------------------------------------------- If $recv <> "" Then GUICtrlSetData($edit, $recv) EndIf WEnd If $ConnectedSocket <> -1 Then ConsoleWrite("Status: Closing socket." & @CRLF) TCPCloseSocket($ConnectedSocket) EndIf EndFunc ;==>Example Edited August 25, 2010 by Tvern Link to comment Share on other sites More sharing options...
GodlessSinner Posted August 25, 2010 Author Share Posted August 25, 2010 Tvern, thank you very much! _____________________________________________________________________________ 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