Page 1 sur 1
[..] Problème avec les fonctions TCP
Posté : lun. 13 août 2012 13:32
par jresine
Bonjour, voila mon souci c'est que le script fonctionne à moitier. Je voudrai afficher un bout de code html avec un "mini serveur" http local.
Lorsque je me connecte en 127.0.0.1, j'ai une erreur comme quoi la connexion a été réinitialiser...
► Afficher le texteserver
$Header = 'HTTP/1.0 200 OK' & @CRLF & 'Content-Type: ' & 'text/html' & @CRLF & @CRLF
TCPStartup()
$MainSocket = TCPListen("127.0.0.1",80,100)
If @error Then
MsgBox(0,0,"error")
Exit
EndIf
While 1
Local $ConnectedSocket = TCPAccept($MainSocket)
If $ConnectedSocket >= 0 Then
MsgBox(0, "", "my server - Client Connected" & $MainSocket)
TCPSend($ConnectedSocket,$Header & "<br><center>123</center><br/>")
EndIf
TCPCloseSocket($ConnectedSocket)
WEnd
Merci de votre aide.
Re: [..] Problème avec les fonctions TCP
Posté : mar. 14 août 2012 10:26
par jresine
Après une journée de test je n'arrive toujours pas a afficher une simple page html ou code html, personne peut m'aider s'il vous plait?
Re: [..] Problème avec les fonctions TCP
Posté : mar. 14 août 2012 10:53
par PandiPanda
Bonjour ceci? ^^
TCP :
► Afficher le texte
Code : Tout sélectionner
TCPStartup ( )
Local $g_IP = TCPNameToIP("www.google.be")
MsgBox(0,'',$g_IP)
$socket = TCPConnect( $g_IP, 80 )
If $socket = -1 Then Exit
Local $str = ""
TCPSend ($socket,"GET "&"www.google.be"&@CRLF)
While 1
$str = TCPRecv($socket,2048)
If @error Then ExitLoop
If $str <> "" Then MsgBox(0,'',$str)
WEnd
TCPShutdown()
_InetGetSource:
► Afficher le texte
Code : Tout sélectionner
#include <INet.au3>
Local $url = "http://www.google.be"
MsgBox(0,'',_INetGetSource($url))
winhhtp:
► Afficher le texte
Code : Tout sélectionner
Local $url = "http://www.google.be"
$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("GET",$url)
$oHTTP.Send()
$HTMLSource = $oHTTP.Responsetext
MsgBox(0,'',$HTMLSource)
Et une multitude d'udf&fonction dispo pour faire ca ^^
Re: [..] Problème avec les fonctions TCP
Posté : mar. 14 août 2012 11:10
par timmalos
Je crois qu'il veut plutot faire l'inverse, et utiliser AutoIt pour créer un serveur HTTP.
Vous pouvez jeter un coup d'oeil à ceci:
http://www.autoitscript.com/forum/topic ... re-autoit/
Re: [..] Problème avec les fonctions TCP
Posté : mar. 14 août 2012 11:15
par jresine
Bonjour, non pas vraiment, je cherche à faire un mini web serveur pour une seul page ou code html. Si le navigateur pointe sur 127.0.0.1, alors ca affiche une unique page en html.
Pour résumer, je vais créer une script qui permet de bloquer des site internet grâce au fichier HOSTS et de rediriger le 127.0.0.1 sur une page qui indique comme quoi l’accès au site n'est pas autoriser.
Re: [..] Problème avec les fonctions TCP
Posté : mar. 14 août 2012 14:06
par jresine
Merci le code est pas mal mais... il y a plein de chose qui ne me serve pas comme le css, la page erreur 404, je voudrai le code le plus cour et simple possible

Re: [..] Problème avec les fonctions TCP
Posté : mar. 14 août 2012 15:12
par PandiPanda
en reprenant le code citez plus haut en faisant de la modification un peu à l'arrache, y'a ceci ...
► Afficher le texte
Code : Tout sélectionner
Local $sIP = "127.0.0.1"
Local $iPort = 80
Local $iMaxUsers = 15
Local $aSocket[$iMaxUsers]
Local $sServerName = "trolol"
For $x = 0 to UBound($aSocket)-1 ; Fills the entire socket array with -1 integers, so that the server knows they are empty.
$aSocket[$x] = -1
Next
TCPStartup()
$iMainSocket = TCPListen($sIP,$iPort) ;create main listening socket
If @error Then ; if you fail creating a socket, exit the application
MsgBox(0x20, "AutoIt Webserver","erreur")
Exit
EndIf
while 1
$iNewSocket = TCPAccept($iMainSocket) ; Tries to accept incoming connections
If $iNewSocket >= 0 Then ; Verifies that there actually is an incoming connection
For $x = 0 to UBound($aSocket)-1 ; Attempts to store the incoming connection
If $aSocket[$x] = -1 Then
$aSocket[$x] = $iNewSocket ;store the new socket
ExitLoop
EndIf
Next
EndIf
For $x = 0 to UBound($aSocket)-1 ; A big loop to receive data from everyone connected
If $aSocket[$x] = -1 Then ContinueLoop ; if the socket is empty, it will continue to the next iteration, doing nothing
$sNewData = TCPRecv($aSocket[$x],1024) ; Receives a whole lot of data if possible
If @error Then ; Client has disconnected
$aSocket[$x] = -1 ; Socket is freed so that a new user may join
ContinueLoop ; Go to the next iteration of the loop, not really needed but looks oh so good
ElseIf $sNewData Then ; data received
_HTTP_SendData($aSocket[$x],"<center>123</center>", "text/html")
$aSocket[$x] = -1 ; the socket is automatically closed so we reset the socket so that we may accept new clients
EndIf
Next
WEnd
TCPShutdown()
Func _HTTP_SendData($hSocket, $bData, $sMimeType, $sReply = "200 OK")
$sPacket = Binary("HTTP/1.1 " & $sReply & @CRLF & _
"Server: " & $sServerName & @CRLF & _
"Connection: close" & @CRLF & _
"Content-Lenght: " & BinaryLen($bData) & @CRLF & _
"Content-Type: " & $sMimeType & @CRLF & _
@CRLF)
TCPSend($hSocket,$sPacket) ; Send start of packet
While BinaryLen($bData) ; Send data in chunks (most code by Larry)
$a = TCPSend($hSocket, $bData) ; TCPSend returns the number of bytes sent
$bData = BinaryMid($bData, $a+1, BinaryLen($bData)-$a)
WEnd
$sPacket = Binary(@CRLF & @CRLF) ; Finish the packet
TCPSend($hSocket,$sPacket)
TCPCloseSocket($hSocket)
EndFunc