Hallo ich wollte nachfragen wie es möglich ist oder ob es möglich ist wen ich zb ein Client habe ihn mit den server verbinde und dan nachher an ein 2 server weiterverbinden möchte ob man dies mit den Sockets machen kann oder ob ich den client sagen muss das er sich auf ein neuen port einwählen muss.
Tcp verbindung von ein programm an ein anderes weitergeben.
-
- [ offen ]
-
sedas19 -
1. Januar 2015 um 14:53 -
Erledigt
-
-
So einfach ist das nicht, sonst könnte man ja einfach die Pakete von anderen Programmen umlenken.
Du kannst dem Server bzw. dem Clienten ja eine spezielle Anfrage schicken wie: REDIRECT TO 192.168.178.100 AT 9000 damit der Client/Server das verarbeitet (musst du selber coden) und dorthin connected.
Das wäre wohl die beste Lösung. -
ja habe ich auch schon dran gedacht blos wen nacher 100 leute sich verbinden und jeder sein eigene "server" erhält muss ich ja auch 100 ports aktivieren. in der hinsicht ist es wieder unproduktiv^^
-
Nein musst du nicht. Du kannst ja mehrer Clienten an einen Port schicken. Wie funktionieren denn HTTP-Server?
Solange der andere Client nicht gleichzeitig TCPAccept verwendet kommen da eigentlich keine Clienten durcheinander. -
Server:
Spoiler anzeigen
[autoit]#include-once
[/autoit]
while 1
_FileReceive();
If @error Then
MsgBox(48,"","Error: "&@error)
Exit
EndIf
Sleep(100)
WEnd
;===============================================================================
;
; Function Name: _FileReceive
; Description:: Receives a File from an Client (See _FileSend)
; Parameter(s):
;$sFileName [Optional] : The Filename to save (if default or "" then using the original File Name)
;$IP [Optional] : The IP where the Server run, Default = @IPAdress1
;$PORT [Optional] : The Port to use (must be the same as by _FileSend!!)
;$iSplash [Optional] : 1(defaul) = show SplashText
; Requirement(s): -
; Return Value(s): On Succes Return 1
; On faild Return -1 and @error code 1-3:
; @error = 1: Error creating Listening socket on IP
; @error = 2: Faild to open file
; @error = 3: Faild to write file
; Author(s): GtaSpider
; Modified by: Texos
;
;===============================================================================
Func _FileReceive($sFileName = '', $IP = @IPAddress1, $PORT = 4324,$iSplash=1)
Local $iMainSocket, $iAccSocket = -1, $sBuff, $sRecv = "", $i = 0
TCPStartup()
$iMainSocket = TCPListen($IP, $PORT)
If @error Then Return SetError(1,0,-1)
While $iAccSocket = -1
$iAccSocket = TCPAccept($iMainSocket)
Sleep(50)
WEnd
If $iSplash Then ProgressOn("Empfangsbereit","","Warte auf Dateien...",-1,-1,16)
Do
$Recv = TCPRecv($iAccSocket,1024)
Sleep(100)
Until $Recv <> ""
$Recv = StringSplit($Recv,Chr(3))
$iFileTrimcount = Round($Recv[3]/8)
$iFilesizeByte = $Recv[2]/8
$SFilename = $Recv[1]
$sBuff = ""
$len = 0
Do
$Recv = TCPRecv($iAccSocket,_Max(1024*16,$iFileTrimcount/4))
$sBuff &= $Recv
If $iSplash Then
$len += BinaryLen(Binary("0x"&$Recv))
ProgressSet(_Min(100,Round($len/$iFilesizeByte,2)*100),"Dateitransfer", _Min(100,Round($len/$iFilesizeByte,2)*100)&"% abgeschlossen.")
EndIf
Sleep(20)
Until StringRight($Recv,4) = "DONE" ; DONE = 444F4E45
$sBuff = StringTrimRight($sBuff,4)
$sBuff = Binary("0x"&$sBuff)
$sBuff = BinaryToString($sBuff)
If $iSplash Then ProgressSet(100,"Schreibe Datei...")
If FileExists("Incoming\"&$sFileName) Then
$sTmp = StringSplit($sFileName, ".")
If $sTmp[0] < 2 Then
While 1
$i += 1
If Not FileExists($sFileName & "(" & $i & ")") Then
$sFileName = $sFileName & "(" & $i & ")"
ExitLoop
EndIf
WEnd
Else
While 1
$i += 1
If Not FileExists($sTmp[1] & "(" & $i & ")" & $sTmp[2]) Then
$sFileName = $sTmp[1] & "(" & $i & ")." & $sTmp[2]
ExitLoop
EndIf
WEnd
EndIf
EndIf
$iFileOp = FileOpen("Incoming\"&$sFileName, 16 + 2)
If @error Then Return SetError(2,0,-1)
FileWrite($iFileOp, $sBuff)
If @error Then Return SetError(3,0,-1)
FileClose($iFileOp)
If $iSplash Then ProgressSet(100,"Datei erfolgreich empfangen!")
TCPSend($iAccSocket, "Finish")
If $iSplash Then Sleep(2000)
TCPCloseSocket($iAccSocket)
If $iSplash Then ProgressOff()
TCPShutdown()
Sleep(1000)
Return 1
EndFunc ;==>_FileReceive
Func _Min($nNum1, $nNum2)
; Check to see if the parameters are indeed numbers of some sort.
If (Not IsNumber($nNum1)) Then Return SetError(1, 0, 0)
If (Not IsNumber($nNum2)) Then Return SetError(2, 0, 0)
If $nNum1 > $nNum2 Then
Return $nNum2
Else
Return $nNum1
EndIf
EndFunc ;==>_Min
Func _Max($nNum1, $nNum2)
; Check to see if the parameters are indeed numbers of some sort.
If Not IsNumber($nNum1) Then Return SetError(1, 0, 0)
If Not IsNumber($nNum2) Then Return SetError(2, 0, 0)
If $nNum1 > $nNum2 Then
Return $nNum1
Else
Return $nNum2
EndIf
EndFunc ;==>_MaxClient:
Spoiler anzeigen
[autoit]#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
[/autoit] [autoit][/autoit] [autoit]
#AutoIt3Wrapper_Outfile=Clien3t.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
IniWrite("Settings\Conf","Server","IP","")
;~ Global $server = IniRead("Settings\Conf","Server","IP","")
;~ If $server <> "" Then
$server = @IPAddress1
;~ EndIf
#include <ButtonConstants.au3>
#include <GuiListView.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <FileConstants.au3>
#include <Array.au3>
#include <WindowsConstants.au3>
#include <Crypt.au3>
#include <File.au3>
Global $akhandel
Global $handlela = 1,$coloum = 8
Dim $listviewhandle[1][$coloum]
Global Const $sMessage = "Hold down Ctrl or Shift to choose multiple files."
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("", 268, 461, 195, 123)
GUICtrlCreateLabel("Hash-Key:", 8, 8, 92, 17, $SS_CENTER)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$hash1 = GUICtrlCreateInput("", 112, 8, 145, 21)
$ListView1 = GUICtrlCreateListView("Filename|Status", 8, 64, 250, 358)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 150)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 75)
$sefile = GUICtrlCreateButton("Select File", 8, 32, 251, 25)
$upload = GUICtrlCreateButton("Upload", 8, 428, 251, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
DirCreate("Upload")
;~ _FileSend("1mb.txt", $server)
;~ _FileSend("10mb.txt", $server)
_FileSend("100mb.txt", $server)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $upload
_Crypt()
Case $sefile
Local $sFileOpenDialog = FileOpenDialog($sMessage, @ScriptDir & "\", "All (*.*)|", $FD_FILEMUSTEXIST + $FD_MULTISELECT)
_File($sFileOpenDialog)
Case $GUI_EVENT_CLOSE
ExitEndSwitch
[/autoit] [autoit][/autoit] [autoit]
WEndFunc _Crypt()
[/autoit] [autoit][/autoit] [autoit]
$key = GUICtrlRead($hash1)
If $key = "" Then
$key = ""
For $x = 1 to 256
$key &= Random(0,9,1)
Next
EndIf
For $x = 1 to $listviewhandle[0][0]
$file = StringReplace($listviewhandle[$x][2],".",",")
ConsoleWrite($file&@CRLF)
$akhandel = $listviewhandle[$x][2]
$a = _Crypt_EncryptFile2 ( $listviewhandle[$x][1]&$listviewhandle[$x][2], @ScriptDir&"\Upload\"&$file,$key, $CALG_AES_256 )
If @error <> 0 Then
GUICtrlSetBkColor($listviewhandle[$x][0],0xFF0000)
ConsoleWrite($a&" "&@error&@CRLF)
Else
GUICtrlSetBkColor($listviewhandle[$x][0],0x00FFFF)
_FileWriteToLine(@ScriptDir&"\Upload\"&$file,1,$key)
EndIf
Next
EndFuncFunc _File($filedialog)
[/autoit] [autoit][/autoit] [autoit]
_Crypt_Startup()
If $filedialog < "" Then
Return False
ElseIf StringInStr($filedialog,"|") Then
$splittfiledialog = StringSplit($filedialog,"|")
For $x = 2 to $splittfiledialog[0]
$hash = _Crypt_HashFile($splittfiledialog[1]&"\"&$splittfiledialog[$x], $CALG_MD5)
$checkli = _checkList($hash)
If $checkli <> "" Then
MsgBox(48,"File is already in the List",$splittfiledialog[1]&"\"&$splittfiledialog[$x]&@CRLF&"[MD5-Hash: "&$hash&"]"&@CRLF&@CRLF&@CRLF&$listviewhandle[$checkli][1]&$listviewhandle[$checkli][2]&@CRLF&"[MD5-Hash: &"&$listviewhandle[$checkli][3]&"]")
Else
$handle = GUICtrlCreateListViewItem($splittfiledialog[$x]&"|0%",$ListView1)
ReDim $listviewhandle[$handlela+1][$coloum]
$listviewhandle[0][0] = $handlela
$listviewhandle[$handlela][0] = $handle
$listviewhandle[$handlela][1] = $splittfiledialog[1]&"\"
$listviewhandle[$handlela][2] = $splittfiledialog[$x]
$listviewhandle[$handlela][3] = $hash
$handlela = $handlela+1
EndIf
Next
ElseIf StringInStr($filedialog,"\") Then
$hash = _Crypt_HashFile($filedialog, $CALG_MD5)
$checkli = _checkList($hash)
If $checkli <> "" Then
MsgBox(48,"File is already in the List",$filedialog&@CRLF&"[MD5-Hash: "&$hash&"]"&@CRLF&@CRLF&@CRLF&$listviewhandle[$checkli][1]&$listviewhandle[$checkli][2]&@CRLF&"[MD5-Hash: &"&$listviewhandle[$checkli][3]&"]")
Else
$splittfiledialog = StringSplit($filedialog,"\")
$max = $splittfiledialog[0]
$handle = GUICtrlCreateListViewItem($splittfiledialog[$max]&"|0%",$ListView1)
ReDim $listviewhandle[$handlela+1][$coloum]
$listviewhandle[0][0] = $handlela
$listviewhandle[$handlela][0] = $handle
$listviewhandle[$handlela][1] = StringTrimRight($filedialog,StringLen($splittfiledialog[$max]))
$listviewhandle[$handlela][2] = $splittfiledialog[$max]
$listviewhandle[$handlela][3] = $hash
$handlela = $handlela+1
EndIf
EndIf
_Crypt_Shutdown()
EndFuncFunc _checkList($hash)
[/autoit] [autoit][/autoit] [autoit]
For $x = 1 to $listviewhandle[0][0]
If $listviewhandle[$x][3] = $hash Then
Return $x
ExitLoop
EndIf
Next
EndFunc
;===============================================================================
;
; Function Name: _FileSend
; Description:: Sending a File to an Server (See _FileReceive)
; Parameter(s):
;$sFile : The File to send
;$IP : The target IP Adress
;$PORT [Optional] : The Port to use (must be the same as by _FileRecevie!!)
;$iSplash [Optional] : 1(defaul) = show SplashText
; 0 = dont show SplashText
;$iWaitWhileConnected [Optional] : 0 = If TCPConnect faild (@error) Return with error Code 2
; 1(Default) = If TCPConnect faild put it in a while until connected
;$iWaitForServer [Optional] : 1(defaul) = wait for server to write data on hd
; 0 = dont wait for server
; Requirement(s): -
; Return Value(s): On Succes Return 1
; On faild Return -1 and @error code 1-3:
; @error = 1: File not exist
; @error = 2: TCPConnect faild (only able if $iWaitWhileConnected <> 1)
; @error = 3: Faild to Open File
; @error = 4: A Connectionproblem while sending
; Author(s): GtaSpider
; Modified by: Texos
;
;===============================================================================Func _FileSend($sFile, $IP, $PORT = 4324,$iSplash=1, $iWaitWhileConnected=1, $iWaitForServer=1)
[/autoit] [autoit][/autoit] [autoit]
Local $iMainSocket = -1, $sBuff, $iFileOp,$sRecv, $sendfull = False
If Not FileExists($sFile) Then Return SetError(1,0,-1)
If $iSplash Then ProgressOn("Dateitransfer","","Warte auf Server...",-1,-1,16)
TCPStartup()
If Number($iWaitWhileConnected) = 1 Then
While $iMainSocket = -1
$iMainSocket = TCPConnect($IP, $PORT)
WEnd
Else
$iMainSocket = TCPConnect($IP, $PORT)
If @error Then Return SetError(2, 0, -1)
EndIf
If $iSplash Then ProgressSet(0,"Dateitransfer", "Lese Datei ein...")
$iFileOp = FileOpen($sFile, 16)
If @error Then Return SetError(3,0,-1)
$sFileread = FileRead($iFileOp)
$iFilesizeBIT = BinaryLen($sFileread)*8
$sFileread = StringTrimLeft($sFileread,2)
$trimcount = Floor($iFilesizeBIT/100)
If $trimcount < 100 OR $iFilesizeBIT <= 4350000 Then $sendfull = True
FileClose($iFileOp)
$sFile = StringTrimLeft($sFile,StringInStr($sFile,"\",-1,-1))
Do
$TCPSend = TCPSend($iMainSocket,$sFile&Chr(3)&$iFilesizeBIT&Chr(3)&$trimcount)
Sleep(100)
Until $TCPSend > 0
If $sendfull = False Then
For $i = 1 To 100
$check = StringLeft($sFileread,$trimcount/4)
$sFileread = StringTrimLeft($sFileread,$trimcount/4)
TCPSend($iMainSocket, $check)
If $iSplash Then ProgressSet($i,"Dateitransfer", $i&"% abgeschlossen.")
Next
TCPSend($iMainSocket, $sFileread)
Else
TCPSend($iMainSocket, $sFileread)
EndIf
TCPSend($iMainSocket, "DONE")
If $iSplash Then ProgressSet(100,"Dateitransfer", "Dateitransfer erfolgreich abgeschlossen.")
Sleep(1000)
If $iSplash Then ProgressSet(100,"Dateitransfer", "Warte auf Server...")
If $iWaitForServer Then
Do
$Recv = TCPRecv($iMainSocket,1024)
Sleep(100)
Until $Recv <> ""
EndIf
TCPCloseSocket($iMainSocket)
TCPShutdown()
ProgressOff()
Return 1
EndFuncFunc _Crypt_EncryptFile2($sSourceFile, $sDestinationFile, $vCryptKey, $iALG_ID)
[/autoit] [autoit][/autoit] [autoit]
Local $bTempData = 0, _
$hInFile = 0, $hOutFile = 0, _
$iError = 0, $iExtended = 0, $iFileSize = FileGetSize($sSourceFile), $iRead = 0, _
$bReturn = True_Crypt_Startup()
[/autoit] [autoit][/autoit] [autoit]
;~ $bTempData = FileRead($hInFile, 1024 * 1024)
;~ $iRead += BinaryLen($bTempData)
ConsoleWrite("Cal: "&Round($iFileSize/1024/1024,0)&@CRLF)
$rounds = Round($iFileSize/1024/1024,0)$c = 0
[/autoit] [autoit][/autoit] [autoit]
DoIf $iALG_ID <> $CALG_USERKEY Then
[/autoit] [autoit][/autoit] [autoit]
$vCryptKey = _Crypt_DeriveKey($vCryptKey, $iALG_ID)
If @error Then
$iError = @error
$iExtended = @extended
$bReturn = False
ExitLoop
EndIf
EndIf$hInFile = FileOpen($sSourceFile, $FO_BINARY)
[/autoit] [autoit][/autoit] [autoit]
If @error Then
$iError = 2
$bReturn = False
ExitLoop
EndIf
$hOutFile = FileOpen($sDestinationFile, $FO_OVERWRITE + $FO_CREATEPATH + $FO_BINARY)
If @error Then
$iError = 3
$bReturn = False
ExitLoop
EndIfDo
[/autoit] [autoit][/autoit] [autoit]$bTempData = FileRead($hInFile, 1024 * 1024)
[/autoit] [autoit][/autoit] [autoit]
$iRead += BinaryLen($bTempData)
If $iRead = $iFileSize Then
$bTempData = _Crypt_EncryptData2($bTempData, $vCryptKey, $CALG_USERKEY, True)
If @error Then
$iError = @error + 400
$iExtended = @extended
$bReturn = False
EndIf
FileWrite($hOutFile, $bTempData)
ExitLoop 2
Else
$bTempData = _Crypt_EncryptData2($bTempData, $vCryptKey, $CALG_USERKEY, False)
If @error Then
$iError = @error + 500
$iExtended = @extended
$bReturn = False
ExitLoop 2
EndIf
FileWrite($hOutFile, $bTempData)
EndIf$c = $c+1
[/autoit] [autoit][/autoit] [autoit]
$per = (100/$rounds)*$c
;~ _setper($per)
ConsoleWrite("PER &"&$per&"%"&@CRLF)
Until FalseUntil True
[/autoit] [autoit][/autoit] [autoit]If $iALG_ID <> $CALG_USERKEY Then _Crypt_DestroyKey($vCryptKey)
[/autoit] [autoit][/autoit] [autoit]
_Crypt_Shutdown()
If $hInFile <> -1 Then FileClose($hInFile)
If $hOutFile <> -1 Then FileClose($hOutFile)
;~ ConsoleWrite("PER &"&$per&"%"&@CRLF)
Return SetError($iError, $iExtended, $bReturn)
EndFunc ;==>_Crypt_EncryptFile;~ Func _setper($per)
[/autoit] [autoit][/autoit] [autoit]
;~ $handle = $akhandel
;~ $c = _GUICtrlListView_GetItemCount ( $ListView1 )
;~ For $x = 0 to $c
;~ $nam = _GUICtrlListView_GetItemText ( $ListView1, $x)
;~ $nam = _GUICtrlListView_GetItemText ( $ListView1, $x)
;~ If $name = $handle Then
;~ If
;~ _GUICtrlListView_SetItemText ( $ListView1, $handle, $per&"%", 1)
;~ EndFuncFunc _Crypt_EncryptData2($vData, $vCryptKey, $iALG_ID, $bFinal = True)
[/autoit] [autoit][/autoit] [autoit]
Local $iReqBuffSize = 0, _
$aRet = 0, _
$hBuff = 0, _
$iError = 0, $iExtended = 0, _
$vReturn = 0_Crypt_Startup()
[/autoit] [autoit][/autoit] [autoit]
$a = 1
Do
If $iALG_ID <> $CALG_USERKEY Then
$vCryptKey = _Crypt_DeriveKey($vCryptKey, $iALG_ID)
If @error Then
$iError = @error + 100
$iExtended = @extended
$vReturn = -1
ExitLoop
EndIf
EndIf$aRet = DllCall(__Crypt_DllHandle(), "bool", "CryptEncrypt", "handle", $vCryptKey, "handle", 0, "bool", $bFinal, "dword", 0, "ptr", 0, _
[/autoit] [autoit][/autoit] [autoit]
"dword*", BinaryLen($vData), "dword", 0)
If @error Or Not $aRet[0] Then
$iError = @error + 20
$iExtended = @extended
$vReturn = -1
ExitLoop
EndIf$iReqBuffSize = $aRet[6]
[/autoit] [autoit][/autoit] [autoit]
$hBuff = DllStructCreate("byte[" & $iReqBuffSize & "]")
DllStructSetData($hBuff, 1, $vData)
$aRet = DllCall(__Crypt_DllHandle(), "bool", "CryptEncrypt", "handle", $vCryptKey, "handle", 0, "bool", $bFinal, "dword", 0, "struct*", $hBuff, _
"dword*", BinaryLen($vData), "dword", DllStructGetSize($hBuff))
If @error Or Not $aRet[0] Then
$iError = @error + 30
$iExtended = @extended
$vReturn = -1
ExitLoop
EndIf
$vReturn = DllStructGetData($hBuff, 1)
$a = $a+1
Until TrueIf $iALG_ID <> $CALG_USERKEY Then _Crypt_DestroyKey($vCryptKey)
[/autoit]
_Crypt_Shutdown()
ConsoleWrite($a&@CRLF)
Return SetError($iError, $iExtended, $vReturn)
EndFunc ;==>_Crypt_EncryptData