Jump to content

TCPServer/Client issue


 Share

Recommended Posts

is there anything i have to do in order to get a client to be able to check BACK into the server after it exits? ive tried resetting the arravy[$0][0] value back to 0 and even tried closing the socket, but after the client exits, i can not get it to come back in without starting the server program over again. Some stupid little trick i dont know about?

Link to comment
Share on other sites

maybe, can you geave us test code to replicate problem?

edit: that FF beat my IE this time ;)

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

heres what im working with. TBH i feel like its a bit over my head. my boss saw the last thing i was working with to pull in MS Office licenses and now he wants me to write up a license server for something hes writing (w.e... hes the boss)

Heres my server program. Copied then modified from here:

TCPStartup()

$IP = @IPAddress1
$Port = 1018
$max_connections = 500
$datafile = @WorkingDir & "records.txt"
$n = 1
$Listen = TCPListen( $IP, $Port, $max_connections)
If $Listen = -1 Then Exit

Global $Accept
Dim $Socket_Data[$max_connections + 1][3]
;add more to the second array to add data.
;(ex) 2 would have another column for data.
;you could make Usernames or ClientIP's in it... anything you want.
;Socket_data EXAMPLE
;$Socket_Data[0][0] = AMOUNT OF CURRENT CLIENTS
;$Socket_Data[1][0] = client 1
;$Socket_Data[2][0] = client 2
;$Socket_Data[n][0] = client n
   
ProgressOn ('Socket Data', 'Reseting Socket Data...')
For $0 = 0 To $max_connections
   $Socket_Data[$0][0] = 0
   ;To reset the Socket array.
   ProgressSet (Round (($0 / $max_connections) * 100),'','Reseting Socket Data...')
   Sleep (10)
Next
ProgressOff ()

FileOpen($datafile, 1)

While 1
   _AcceptData()
   Sleep(100)
   If ProcessExists("EndLicSrv.exe") <>0 Then _Exit_()
WEnd

Func _AcceptData()
   $Accept = TCPAccept($Listen)
   If $Accept = -1 Then Return
   For $0 = 1 to $max_connections
      If $Socket_Data[$0][0] = 0 Then
         $Socket_Data[$0][0] = $Accept
         Do
            $Recv = TCPRecv( $Accept, 1000000)
            If ProcessExists ("EndLicSrv.exe") <> 0 Then _Exit_()
            Sleep(50)
         Until $Recv <> ""
         MsgBox(0,"","exited Do Loop, $Recv = " & $Recv)
         ;$data = _AnalyzeRecv($Recv)
         ;MsgBox(0,"", "$Data = " & $data)
         ;If $data = $Recv Then
         ;Else
         ;EndIf         $exitDO = 1
         While 2
            $line = FileReadLine($datafile, $n)
            If @error = -1 Then ExitLoop
            If $line = $Recv Then
               TCPSend($Accept, "whoahwhoahwhoah, not cool brah, old news." & @CRLF & "whachu tryin'a do son?!?!")
               ExitLoop
               Sleep(650)
            EndIf
            $n = $n + 1
         Wend
         If @Error = -1 Then
            FileWriteLine($datafile, $Recv)
            TCPSend($Accept, "s'all good brah, i got you now")
         EndIf
         $confirm = TimerInit()
         Do
            If TimerDiff($confirm) = 10000 Then ExitLoop
            $RecvCon = TCPRecv( $Accept, 1000000)
            If ProcessExists ("EndLicSrv.exe") <> 0 Then _Exit_ ()
         Until $RecvCon <> ""
         $n = 1
         $Socket_Data[$0][0] = 0
      EndIf
   Next
EndFunc

Func _Exit_ ()
   For $0 = 1 To $max_connections
      TCPCloseSocket ($Socket_Data[$0][0])
   Next
   TCPCloseSocket ($Accept)
   TCPShutdown ()
   Exit
 EndFunc
 
 Func _AnalyzeRecv($DataRAW)
   $RecvArray = StringSplit($DataRAW, "^")
   If $RecvArray[$RecvArray[0]] = 0 Then
      Return $DataRAW
   Else
      MsgBox(0, "", "analyze recv else1" & @CRLF & $RecvArray[0])
   EndIf
EndFunc

yea i know i have some stuff commented out, but im just trying to get this to work before i worry about the other stuff.

Problem is that client can check in, but cant check back in without restarting the server. Client below:

#Include <Timers.au3>

TCPStartup()

$IP = TCPNameToIP(InputBox("what IP", "Please input the IP address you would like to send to"))
$Port = 1018

For $0 = 0 To 10
   $Socket = TCPConnect($IP, $Port)
   If $Socket <> -1 then ExitLoop
   TCPCloseSocket($Socket)
   Sleep(100)
Next
If $Socket = -1 Then _Exit()
TCPSend($Socket, $License & " , " & @ComputerName & " , " & DriveGetSerial(@HomeDrive))
$Timeout = TimerInit()
Do
   $Recv = TCPRecv ($Socket, 1000000)
   Sleep(300)
   If TimerDiff($Timeout) >= 30000 Then ExitLoop
Until $Recv <> ""
If $Recv = "" Then _Exit()

_Exit ()
   
   
Func _Exit ()     
   TCPCloseSocket ($Socket)     
   TCPShutdown()     
   Exit
EndFunc   ;==>_Exit_

thanks guys in advance. im about shot working on this one

Link to comment
Share on other sites

if not problem solved then

@ op, if your main goal is to only send data from server once the client is connected than i dont see why you need to use 2D array, well even if that isnt your plan i hardly think that you need all that mess in there

Simple server

#include <Array.au3>
TCPStartup()
Global $Socket_Data[1], $Recv
Global $Listen = TCPListen(@IPAddress1, 1018, 500)
If @error Then
    MsgBox(0,'',@error)
    Exit
EndIf
$Socket_Data[0] = 0
While 1
    For $x = $Socket_Data[0] To 1 Step -1;front to bck loop cos of _ArrayDelete
         $Recv = TCPRecv($Socket_Data[$x], 1000000)
         If $Recv Then
             MsgBox(0, $x, "exited Do Loop, $Recv = " & $Recv,3)
;~         TCPSend($Socket_Data[$x], 'haha' );send some response if needed or additional code before closing that socket
             TCPCloseSocket($Socket_Data[$x])
             _ArrayDelete($Socket_Data,$x)
             $Socket_Data[0] = $Socket_Data[0] - 1
         EndIf
    Next
    _Accept()
WEnd
Func _Accept()
    $Accept = TCPAccept($Listen)
    If $Accept <> -1 Then
         MsgBox(0,'Connected',$Accept,1)
         _ArrayAdd($Socket_Data,$Accept)
         $Socket_Data[0] = $Socket_Data[0] + 1
    EndIf
EndFunc

client

#Include <Timers.au3>
TCPStartup()
$Socket = TCPConnect(@IPAddress1, 1018)
If $socket = -1 Then Exit
TCPSend($Socket, 'aasad' & " , " & @ComputerName & " , " & DriveGetSerial(@HomeDrive))
;gona try if needed to recive last msg from server (named 'haha' in server script) before exiting
;~ Do
;~   $Recv = TCPRecv($Socket, 1000000)
;~ Until $Recv
;~ MsgBox(0,'Recived from server:',$Recv)
Sleep(1000)

so the server will w8 for connection and recive msg, send response to client if needed and kill socket with its array pointer after its done.

so nothing 2 classy but still working and no problems on running identical client more than 1 time

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

@firefox - no problem wasnt solved (at that time)

@bogQ - ill look into that rather than clean up mine.

I realized at like 11 pm last night that the problem was i was looking at the fundamentals of the original program wrong. Once inside the main function, it just kinda waited and sat there. Added a Return at the end of the For/Next Loop and VIOLA!!! Now just need to clean up my code and test or run bog's version and test and should finally be finished!

Looks like the problem was just the fact that i didnt 100% understand the code i was trying to modify and once it clicked i got it. Thanks for the code bog. ill definitely look into that one

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...