File-Store

  • Hab hier ein kleines Script geschrieben, vergleichbar mit dem App-Store, aus Langeweile.

    Spoiler anzeigen


    Client:

    [autoit]


    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    ;upload-paket upload/ip/filename/code/note
    ;filelist-paket get_filelist/ip
    ;download-paket download/ip/filename
    $title = "File-Store"
    $ip_server = server ip
    $port_recv = 3000
    $port_send = 3001
    $delimeter = "/"
    $text_upload = "upload"
    $text_note = "_note.txt"
    $text_download = "download"
    $text_get_filelist = "get_filelist"
    $maxlen = 100000
    $text_download_fail = "fail"
    $connect_fail = "connection fail"
    $text_connect_fail = "Fehler beim Verbinden zum File-Store Server."
    $ip = eigene ip
    UDPStartup ()
    $open = UDPOpen ( $ip_server, $port_send )
    $bind = UDPBind ( $ip_server, $port_recv )
    OnAutoItExitRegister ( "_exit" )

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $filelist = _get_filelist ()
    If $filelist = $connect_fail Then
    MsgBox ( 16, $title, $text_connect_fail )
    Exit
    EndIf

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    #Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate( $title, 460, 329, 266, 173)
    $Edit1 = GUICtrlCreateEdit("", 8, 48, 441, 273)
    GUICtrlSetData(-1, $filelist )
    GUICtrlSetFont(-1, 11, 400, 0, "Comic Sans MS")
    GUICtrlSetState(-1, $GUI_DISABLE)
    $Button1 = GUICtrlCreateButton("Downloaden", 8, 8, 97, 33)
    GUICtrlSetFont(-1, 11, 400, 0, "Comic Sans MS")
    $Button2 = GUICtrlCreateButton("Uploaden", 358, 7, 97, 33)
    GUICtrlSetFont(-1, 11, 400, 0, "Comic Sans MS")
    $Button3 = GUICtrlCreateButton("Aktualisieren", 184, 8, 97, 33)
    GUICtrlSetFont(-1, 11, 400, 0, "Comic Sans MS")
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    While 1

    [/autoit] [autoit][/autoit] [autoit]

    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $Button1 ;downloaden

    [/autoit] [autoit][/autoit] [autoit]

    $filename_download = InputBox ( $title, "Dateinamen eingeben" )
    If $filename_download <> "" Then

    [/autoit] [autoit][/autoit] [autoit]

    UDPSend ( $open, $text_download & $delimeter & $ip & $delimeter & $filename_download )

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    Do
    $recv = UDPRecv ( $bind, $maxlen )
    Until $recv <> ""

    [/autoit] [autoit][/autoit] [autoit]

    $search_download = StringInStr ( $recv, $text_download )

    [/autoit] [autoit][/autoit] [autoit]

    If $search_download <> "" Then
    $split = StringSplit ( $recv, $delimeter )
    $path_download = FileSaveDialog ( $title, "", "*.* (Alle Dateien)", "", $filename_download )
    FileWrite ( $path_download, $split[2] )
    ExitLoop
    EndIf
    $search_download_fail = StringInStr ( $recv, $text_download_fail )

    [/autoit] [autoit][/autoit] [autoit]

    If $search_download_fail <> "" Then
    MsgBox ( 16, $title, "Datei nicht vorhanden" )
    ExitLoop
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    EndIf

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Case $Button2 ;uploaden
    $path_upload = FileOpenDialog ( $title, "", "*.* (Alle Dateien)", "", "*.*" )
    If $path_upload <> "" Then
    Do
    Do
    $filename = InputBox ( $title, "Dateinamen für den File-Store eingeben:" )
    Until $filename <> ""
    Until $filename <> $text_note
    Do
    $note = InputBox ( $title, "Bemerkung für diese Datei eingeben:" )
    Until $note <> ""

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $read = FileRead ( $path_upload )
    UDPSend ( $open, $text_upload & $delimeter & $ip & $delimeter & $filename & $delimeter & $read & $delimeter & $note )

    [/autoit] [autoit][/autoit] [autoit]

    $filelist = _get_filelist ()
    If $filelist = $connect_fail Then
    MsgBox ( 16, $title, $text_connect_fail )
    Else
    GUICtrlSetData ( $Edit1, $filelist )
    EndIf
    EndIf
    Case $Button3 ;aktualisieren
    $filelist = _get_filelist ()
    If $filelist = $connect_fail Then
    MsgBox ( 16, $title, $text_connect_fail )
    Else
    GUICtrlSetData ( $Edit1, $filelist )
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    EndSwitch
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func _exit ()
    UDPCloseSocket ( $open )
    UDPCloseSocket ( $bind )
    UDPShutdown ()
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _get_filelist ()
    UDPSend ( $open, $text_get_filelist & $delimeter & $ip )
    While 1
    $beginn = TimerInit ()
    Do
    $recv = UDPRecv ( $bind, $maxlen )
    $diff = TimerDiff ( $beginn )
    Until $recv <> "" Or $diff > 3000
    If $recv <> "" Then
    $search = StringInStr ( $recv, $text_get_filelist )
    If $search <> "" Then
    $split = StringSplit ( $recv, $delimeter )
    Return $split[2]
    ExitLoop
    EndIf
    Else
    Return $connect_fail
    EndIf
    WEnd
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit]

    Server:

    [autoit]


    $port_recv = 3001
    $port_send = 3000
    $delimeter = "/"
    $text_upload = "upload"
    $text_get_filelist = "get_filelist"
    $text_download = "download"
    $text_download_fail = "fail"
    $filename = "filelist.txt"
    $text_note = "_note.txt"
    $path_dir = @DesktopDir & "\files\"
    $note_extra = "<<<"
    $note_extra2 = ">>>"
    $maxlen = 1000000000
    UDPStartup ()
    $bind = UDPBind ( "", $port_recv )
    OnAutoItExitRegister ( "_exit" )
    If Not FileExists ( $path_dir ) Then
    DirCreate ( $path_dir )
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    While 1

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Do
    $recv = UDPRecv ( $bind, $maxlen )
    Until $recv <> ""

    [/autoit] [autoit][/autoit] [autoit]

    $search = StringInStr ( $recv, $text_upload )

    [/autoit] [autoit][/autoit] [autoit]

    If $search <> "" Then ;upload
    $split = StringSplit ( $recv, $delimeter )
    FileWrite ( $path_dir & $split[3], $split[4] )
    FileWrite ( $path_dir & $split[3] & $text_note, $note_extra & $split[5] & $note_extra2 )
    FileDelete ( $filename )
    $search_file = FileFindFirstFile( $path_dir & "*.*" )

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $file = FileFindNextFile ( $search_file )
    $search_note = StringInStr ( $file, $text_note )
    If $search_note = "" Then
    If $file = "" Then
    ExitLoop
    EndIf
    $read_note = FileRead ( $path_dir & $file & $text_note )
    FileWrite ( $filename, $file & @CRLF & $read_note & @CRLF & @CRLF )
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    FileClose( $search_file )

    [/autoit] [autoit][/autoit] [autoit]

    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    $search2 = StringInStr ( $recv, $text_get_filelist )

    [/autoit] [autoit][/autoit] [autoit]

    If $search2 <> "" Then ;get_filelist
    $split = StringSplit ( $recv, $delimeter )
    $read_filelist = FileRead ( $filename )
    $open = UDPOpen ( $split[2], $port_send )
    UDPSend ( $open, $text_get_filelist & $delimeter & $read_filelist )
    UDPCloseSocket ( $open )
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    $search3 = StringInStr ( $recv, $text_download )
    If $search3 <> "" Then ;download
    $split = StringSplit ( $recv, $delimeter )
    $read_download = FileRead ( $path_dir & $split[3] )
    $open = UDPOpen ( $split[2], $port_send )

    [/autoit] [autoit][/autoit] [autoit]

    If $read_download = "" Then
    UDPSend ( $open, $text_download_fail )
    Else

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    UDPSend ( $open, $text_download & $delimeter & $read_download )
    EndIf
    UDPCloseSocket ( $open )
    EndIf

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _exit ()
    UDPCloseSocket ( $bind )
    UDPShutdown ()
    EndFunc

    [/autoit]
  • Der Client kann Daten jederzeit up - und downloaden vom Server.
    Also z.B.

    ClientA lädt was auf den Server
    Dann kann jeder andere Client sich auch das Was ClientA geuploadet hat, laden.

    Das setzt natürlich voraus das der Server immer an ist.