Re: Mondo confused on the TCPxxxxx commands...

"valery_vi" <[email protected]>
Newsgroups gmane.comp.windows.autoit.user
Message-ID <[email protected]>
I have two scripts (not tested)
Server.au3:

opt("TrayIconDebug", 1)
#Include<String.au3>
$IP = @IPAddress1
TrayTip("SERVER IP:", "" & $IP, 1)
$PORT = 58789
TcpStartUp ()
$SOCKET = TcpListen ($IP, $PORT, 128)
If $SOCKET = -1 Then Message("Server Initialization failed!")
Do
	$CONNECT = TcpAccept ($SOCKET)
	Sleep(1)
Until $CONNECT >= 0
Do
	Sleep(1)
	$NAME = TcpRecv ($CONNECT, 512)
Until $NAME <> ""
TcpSend ($CONNECT, "Name_Ok!")
$EXT = StringSplit($NAME, ".")
$EXT = $EXT[$EXT[0]]
$FILE = FileSaveDialog("Save File", "::{20D04FE0-3AEA-1069-A2D8-
08002B30309D}", "FileTransfer(*." & $EXT & ")", 18, $NAME)
$TEMP = StringSplit($FILE, ".")
If $TEMP[$TEMP[0]] <> $EXT Then $FILE = $FILE & "." & $EXT
Do
	Sleep(1)
	$NR_PARTI = TcpRecv ($CONNECT, 512)
Until $NR_PARTI <> ""
TcpSend ($CONNECT, "Nr_Parti_Ok!")
$NR = StringSplit($NR_PARTI, "~")
$FILECREATE = FileOpen($FILE, 2)
For $IDX = 1 To $NR[1]
	Do
		Sleep(1)
		$RECV = TCPRecv ($CONNECT, 4096)
	Until $RECV <> "" And @error = 0
	FileWrite($FILECREATE, _HexToString ($RECV))
	TrayTip("", Round(($IDX * 99.9) / $NR[1], 1) & "% done!", 1)
	Do
		Sleep(1)
		$SEND = TCPSend ($CONNECT, $IDX)
	Until $SEND > 0 And @error = 0
Next
If $NR[2] > 0 Then
	Do
		Sleep(1)
		$RECV = TCPRecv ($CONNECT, $NR[2] * 2)
	Until $RECV <> "" And @error = 0
	FileWrite($FILECREATE, _HexToString ($RECV))
	Do
		Sleep(1)
		$SEND = TCPSend ($CONNECT, $NR[2])
	Until $SEND > 0 And @error = 0
EndIf
FileClose($FILECREATE)
Exit 0
Func Message($MESSAGE)
	MsgBox(64 + 262144, "", $MESSAGE, 3)
	Exit 0
EndFunc   ;==>Message

and Client.au3
opt("TrayIconDebug", 1)
#Include<String.au3>
$FILE = FileOpenDialog("Choose File", "::{20D04FE0-3AEA-1069-A2D8-
08002B30309D}", "All Files(*.*)", 3)
If $FILE = "" Then Exit 0
$NAME = StringSplit($FILE, "\")
$NAME = $NAME[$NAME[0]]
$SIZE = FileGetSize($FILE)
$NR_PARTI = Int($SIZE / 2048)
$NR_PARTI = $NR_PARTI & "~"& ($SIZE- ($NR_PARTI * 2048))
$NR = StringSplit($NR_PARTI, "~")
$IP = InputBox(" ", "SERVER IP:", @IPAddress1, "", 120, 80)
$PORT = 58789
TcpStartUp ()
$CONNECT = TcpConnect ($IP, $PORT)
If $CONNECT = -1 Then Message("Connection failed!")
Sleep(333)
TcpSend ($CONNECT, $NAME)
Do
	Sleep(1)
	$CONFIRM = TcpRecv ($CONNECT, 512)
Until $CONFIRM = "Name_Ok!"
TcpSend ($CONNECT, $NR_PARTI)
Do
	Sleep(1)
	$CONFIRM = TcpRecv ($CONNECT, 512)
Until $CONFIRM = "Nr_Parti_Ok!"
$FILEOPEN = FileOpen($FILE, 0)
Sleep(2500)
For $INDEX = 1 To $NR[1]
	Do
		Sleep(1)
		$SEND = TCPSend ($CONNECT, _StringToHex (FileRead
($FILEOPEN, 2048)))
	Until $SEND > 0 And @error = 0
	TrayTip("", Round(($INDEX * 99.9) / $NR[1], 1) & "% done!", 1)
	Do
		Sleep(1)
		$RECV = TCPRecv ($CONNECT, 512)
	Until $RECV = $INDEX
Next
If $NR[2] > 0 Then
	Do
		Sleep(1)
		$SEND = TCPSend ($CONNECT, _StringToHex (FileRead
($FILEOPEN, $NR[2])))
	Until $SEND > 0 And @error = 0
	Do
		Sleep(1)
		$RECV = TCPRecv ($CONNECT, 512)
	Until $RECV = $NR[2]
EndIf
FileClose($FILEOPEN)
Exit 0
Func Message($MESSAGE)
	MsgBox(64 + 262144, "", $MESSAGE, 3)
	Exit 0
EndFunc   ;==>Message

You should start Server.au3 and after it Client.au3
Date of creation - 12 Dec 2005

Hope it's useful
Valery


--- In [email protected], <marx.external@...> wrote:
>
> So I am not the only person who doesn't understand how this part 
works?
> 
>  
> 
> ________________________________
> 
> From: [email protected] 
[mailto:[email protected]] On
> Behalf Of Marx Keath (QNA IT GS ISA External)
> Sent: Tuesday, January 23, 2007 6:55 AM
> To: [email protected]; brian_keene@...
> Subject: [AutoIt] Mondo confused on the TCPxxxxx commands...
> 
>  
> 
> Hey gang,
> 
> I have been banging my head on this for awhile now and have gotten 
no
> where. I have been reading the help file and looking over the 
examples
> included there and an example Valery sent the last time I inquired 
about
> this stuff but I just don't get it obviously because it doesn't work
> like I would expect it.
> 
> I am trying to write a client/server application that works 
basically
> like this...
> 
> I want a central server that just sits waiting for incoming 
connections.
> When it receives one it receives the data. In this case it will be a
> one word request such as 'report' or 'update'. Once it gets this
> message I want it to send back a response (basically "I heard you").
> Then I want it to go back to listening to that port or process any
> further pending incoming connections.
> 
> I have managed to get it to start up and listen on the port. It 
detects
> the connections and receives the data packet. It sees the packet, 
reads
> it properly and sends the response. But then it just loops. It 
doesn't
> respond to anymore incoming connections.
> 
> This is the code I am using for the server side:
> 
> $StartCheck = TCPStartup()
> 
> If $StartCheck < 1 Then
> 
> _FileWriteLog($LogFile,"-- Error starting TCP Services..." &
> @error & " *****")
> 
> Exit
> 
> EndIf
> 
> _FileWriteLog($LogFile,"-- TCP Services Online...")
> 
> _FileWriteLog($LogFile,"--- Creating Listening port")
> 
> $nMainSocket = TCPListen( $MasterIP, $MasterPort )
> 
> While 1
> 
> Detect()
> 
> Rcv()
> 
> Wend
> 
> Func Detect()
> 
> _FileWriteLog($LogFile,"--- Monitoring for incoming connections-" &
> $nSocketRecv)
> 
> While $nSocketRecv = -1
> 
> ToolTip( "waiting for incoming file...", 0, 0 )
> 
> $nSocketRecv = TCPAccept( $nMainSocket )
> 
> WEnd
> 
> TCPCloseSocket($nSocketRecv)
> 
> EndFunc
> 
> ;MsgBox(0,"test",$nSocketRecv)
> 
> Func Rcv()
> 
> While 1
> 
> _FileWriteLog($LogFile,"--- Receiving data...-" & $nSocketRecv & " -
 " &
> $szRecvBuffer & "-=> " & @error)
> 
> $szRecvBuffer &= TCPRecv( $nSocketRecv, 2048 )
> 
> ; MsgBox(0,"test",$szRecvBuffer)
> 
> If @error Then ExitLoop
> 
> _FileWriteLog($LogFile,"---- Buffer Length="&
> StringLen($szRecvBuffer))
> 
> If StringLen($szRecvBuffer) Then
> 
> If $szRecvBuffer = "report" Then
> 
> _FileWriteLog($LogFile,"---- Detected Communication Request...")
> 
> Ohura()
> 
> ExitLoop
> 
> ElseIf $szRecvBuffer = "done" Then
> 
> _FileWriteLog($LogFile,"----- End Communication requested...")
> 
> TCPCloseSocket($nSocketRecv)
> 
> _FileWriteLog($LogFile,"------ Socket closed -" & @error & " -=> " &
> $nSocketRecv)
> 
> $szRecvBuffer = ""
> 
> Return
> 
> EndIf
> 
> EndIf
> 
> WEnd
> 
> EndFunc
> 
> Exit
> 
> Func Ohura()
> 
> ; _ArrayDisplay($AvailPorts,"test")
> 
> ; MsgBox(0,"test","Spawn Listener")
> 
> _FileWriteLog($LogFile,"---- Responding to communication request")
> 
> TCPSend($nSocketRecv,"ohura-" & _ArrayPop($AvailPorts))
> 
> ; TCPCloseSocket($nSocketRecv)
> 
> $szRecvBuffer = ""
> 
> ; $nSocketRecv = -1
> 
> EndFunc
> 
> TCPSend($nSocketRecv,"go away")
> 
> [Non-text portions of this message have been removed]
> 
>  
> 
> 
> 
> [Non-text portions of this message have been removed]
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.