ayuda con TCP

Pregunta Sin Miedo no te cortes cualquier cosa para empezar - Autoit se comienza facilmente.Para Ordenes o Comandos sueltos. Ver nota como preguntar.
Responder
xapu
Hacker del Foro
Mensajes: 82
Registrado: 12 Dic 2009, 15:42

ayuda con TCP

Mensaje por xapu »

bueno.... :smt012
haber mi intencion era crear un programa para transmitir imagenes por tcp, por aora e provado con archivos.txt y va pero no kon imagenes!
aqui el codigo que funciona con lor arxivos txt(para intentar transmitir imagenes lo k ago es cambiar la direccion del arxivo por el de una imagen:
CLIENTE

Código: Seleccionar todo

 #include <Inet.au3>
Opt('MustDeclareVars', 1)

;==============================================
;==============================================
;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
    ; Set Some reusable info
    ;--------------------------
    Local $ConnectedSocket, $szData, $arsivo
    ; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
;   Local $szServerPC = @ComputerName
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = "127.0.0.1"
    Local $nPORT = 33891

    ; Start The TCP Services
    ;==============================================
    TCPStartup()

    ; Initialize a variable to represent a connection
    ;==============================================
    $ConnectedSocket = -1

    ;Attempt to connect to SERVER at its IP and PORT 33891
    ;=======================================================
    $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)

    ; If there is an error... show it
    If @error Then
        MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
        ; If there is no error loop an inputbox for data
        ;   to send to the SERVER.
    Else
        ;Loop forever asking for data to send to the SERVER
       
		; data to transmit convertido a binario kon el 16
		$arsivo = FileOpen(@MyDocumentsDir & "\ip.txt",16)
		$szData = FileRead($arsivo) 
		
		; If they cancel the InputBox or leave it blank we exit our forever loop
		If @error Or $szData = "" Then Exit

		; We should have data in $szData... lets attempt to send it through our connected socket.
		TCPSend($ConnectedSocket, $szData)

		; If the send failed with @error then the socket has disconnected
		;----------------------------------------------------------------
		If @error Then Exit
        
    EndIf
EndFunc   ;==>Example

SERVIDOR

Código: Seleccionar todo

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

;==============================================		;==============================================
;==============================================		;==============================================
;SERVER!! Start Me First !!!!!!!!!!!!!!!	   		;SERVER!! iniciame primero !!!!!!!!!!!!!!
;==============================================		;==============================================
;==============================================		;==============================================

Example()

Func Example()
    ; Set Some reusable info							
    ; Set your Public IP address (@IPAddress1) here.	
;   Local $szServerPC = @ComputerName					
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)		;nombre tcp a ip
    Local $szIPADDRESS = "127.0.0.1"
    Local $nPORT = 33891
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg, $recv
	local $lolix
    ; Start The TCP Services
    ;==============================================    ;==============================================
    TCPStartup()

    ; Create a Listening "SOCKET".						;creando un hueco o agujero que escuche
    ;   Using your IP Address and Port 33891.			;	usando tu IP y el puerto 33891
    ;==============================================		;==============================================
    $MainSocket = TCPListen($szIPADDRESS, $nPORT)

    ; If the Socket creation fails, exit.				; si la creacion del hueco falla sale del programa
    If $MainSocket = -1 Then Exit


    ; Initialize a variable to represent a connection	; iniciando una variable para representar una conexion
    ;==============================================		;==============================================
    $ConnectedSocket = -1


    ;Wait for and Accept a connection					;esperando a aceptar una conexion
    ;==============================================		;==============================================
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1


    ; Get IP of client connecting					    ; cogiendo IP de el ordenador conectado
    $szIP_Accepted = SocketToIP($ConnectedSocket)

    ; GUI Message Loop
    ;==============================================
    While 1

        ; Try to receive (up to) 2048 bytes
        ;----------------------------------------------------------------
        $recv = TCPRecv($ConnectedSocket, 248)

        ; If the receive failed with @error then the socket has disconnected
        ;----------------------------------------------------------------
        If @error Then ExitLoop

        ; Update the edit control with what we have received
        ;----------------------------------------------------------------
        If $recv <> "" Then 
			$lolix = Fileopen("C:\ola.txt",9)
			FileWrite($lolix,$recv)
			FileClose($lolix)
		EndIf
		
    WEnd


    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

    TCPShutdown()
EndFunc   ;==>Example

; Function to return IP Address from a connected socket.							<-<-<-<-<-<- MUY INTERESANTE!!!!! COGER LA IP DEL CONECTADO
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet
   
    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc   ;==>SocketToIP
- 0 error(s), 0 warning(s) :smt098 FUCK YEA!
http://xapus.blogspot.com/
Avatar de Usuario
ainurzzz
Mensajes: 27
Registrado: 09 Sep 2007, 15:04

Re: ayuda con TCP

Mensaje por ainurzzz »

Creo que deberías de usar el StringToBinary y BinaryLen . Es lo que he visto por los foros guiriss.

Es decir, debes convertir a binario e indicar el tamaño de datos binarios que pasas. No tengo codigo hecho de esto.

Cuando lo tengas nos avisas.
Avatar de Usuario
Chefito
Profesional del Autoit
Mensajes: 2035
Registrado: 21 Feb 2008, 18:42
Ubicación: Albacete/Cuenca (España)

Re: ayuda con TCP

Mensaje por Chefito »

Mira este post del foro de habla inglesa: http://www.autoitscript.com/forum/index ... opic=46341
Ahí tienes las funciones hechas (_filesend y _filerecieve).

Saludos.
Cita vista en algún lugar de la red: En este mundo hay 10 tipos de personas, los que saben binario y los que no ;).
Responder