AYUDA URGENTE !!!! COMO ENVIAR DATOS CON SOCKTES

Post Reply
prm_pedro
Posts: 46
Joined: Tue Oct 10, 2006 6:16 pm

AYUDA URGENTE !!!! COMO ENVIAR DATOS CON SOCKTES

Post by prm_pedro »

ME PREGUNTA ES COMO ENVIAR ALGUN VALOR DE UNA VARIABLE EN UN SOCKET

EL SIGUIENTE EJEMPLO ME ENVIA EN ARCHIVO DEL CLIENTE AL SERVER.. PERO COMO LE HAGO PARA ENVIAR VALOR EN ESPECIFICO DE VARIABLES....

:lol: POR LA PARTE DEL CLIENTE..

#include "FiveWin.ch"

static oWnd, oSocket

function Main()
local oBar
DEFINE WINDOW oWnd TITLE "Client socket"
DEFINE BUTTONBAR oBar OF oWnd _3D
DEFINE BUTTON OF oBar ACTION Client() TOOLTIP "Connect"
DEFINE BUTTON OF oBar ;
ACTION oSocket:SendData( "MSG This is a test" ) ;
TOOLTIP "Send data"
DEFINE BUTTON OF oBar ;
ACTION SendFile() TOOLTIP "Send file"
DEFINE BUTTON OF oBar ;
ACTION SendDb() TOOLTIP "Send Db"
ACTIVATE WINDOW oWnd
return nil
//*****************************************************
function Client()
oSocket = TSocket():New( 2000 )
oSocket:bRead = { | oSocket | MsgInfo( oSocket:GetData() ) }
// Never use a MsgInfo() here because it hangs Windows!!!
oSocket:bConnect = { || oWnd:SetText( "Connected!" ) }
oSocket:bClose = { || MsgInfo( "Server has closed!" ) }
oSocket:Connect( "128.50.1.179" ) // use the server IP address here
return nil
//**************************************************
function SendFile()
local cFileName := cGetFile( "*.*", "Select a file to send by Internet")
if ! Empty( cFileName ) .and. File( cFileName )
Socket:SendData( "SENDFILE " + cFileName( cFileName ))
oSocket:SendFile( cFileName )
oSocket:End()
MsgInfo( "archivo enviado" )
endif
return nil



:lol: POR PARTE DEL SERVER

#include "FiveWin.ch"

#define ST_COMMAND 1
#define ST_SENDFILE 2
#define ST_OPENDB 3

#define FILE_BLOCK 8000

static oWnd, oSocket, oClient
//------------------------------------------------------------------------//
function Main()
local oBar
DEFINE WINDOW oWnd TITLE "Server socket"
DEFINE BUTTONBAR oBar OF oWnd _3D
DEFINE BUTTON OF oBar ACTION Server() TOOLTIP "Listen"
ACTIVATE WINDOW oWnd
return nil

//------------------------------------------------------------------------//
function Server()
oSocket = TSocket():New( 2000 )
oSocket:bAccept = { | oSocket | oClient := TSocket():Accept( oSocket:nSocket ),;
oClient:Cargo := ST_COMMAND,;
oClient:bRead := { | oSocket | OnRead( oSocket ) },;
oClient:bClose := { | oSocket | OnClose( oSocket ) } }
oSocket:Listen()
return nil

//------------------------------------------------------------------------//

function OnRead( oSocket )
local cData := oSocket:GetData()
local cToken
LogFile( "sockserv.txt", { Len( cData ) } )
do case
case oSocket:Cargo == ST_COMMAND
cToken = StrToken( cData, 1 )
do case
case cToken == "SENDFILE"
oSocket:Cargo = ST_SENDFILE
oSocket:hFile = fcreate( StrToken( cData, 2 ) )
case cToken == "MSG"
MsgInfo( SubStr( cData, 5 ) )
endcase
case oSocket:Cargo == ST_SENDFILE
fwrite( oSocket:hFile, cData, Len( cData ) )
LogFile( "sockserv.txt", { "writting..." } )
if Len( cData ) < FILE_BLOCK
// fclose( oSocket:hFile )
// MsgInfo( Len( cData ) )
// oSocket:Cargo = ST_COMMAND
endif
endcase
return nil
//------------------------------------------------------------------------//

function OnClose( oSocket )
MsgInfo( "Client has closed!" )
do case
case oSocket:Cargo == ST_SENDFILE
fclose( oSocket:hFile )
endcase
oSocket:End()
return nil
User avatar
Antonio Linares
Site Admin
Posts: 42561
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 35 times
Been thanked: 81 times
Contact:

Post by Antonio Linares »

Pedro,

Tendrías que implementar un nuevo mensaje, como por ejemplo:

oSocket:SendData( "VAR C Hola que tal" )

VAR indicaría que envías un valor de una variable, C su tipo, y a continuación el valor.

El código del servidor has de modificarlo para que lo reconozca:

Code: Select all | Expand

case cToken == "VAR" 
   do case
        case SubStr( cData, 5, 1 ) == "C"
               MsgInfo( SubStr( cData, 7 ) )

        case SubStr( cData, 5, 1 ) == "N"
               ...
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply