FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour SOCKETS
Posts: 203
Joined: Tue Oct 11, 2005 02:17 PM
SOCKETS
Posted: Thu Oct 10, 2019 06:14 PM
Hola estoy haciendo unas pruebas para poner conectar una aplicaci贸n mediante el uso de sockets y me encuentro que cuando creo el socket en el cliente e intento realizar la conexi贸n con el servidor pero el servidor no est谩 iniciado
el sistema se queda colgado. y aunque se cierra la ventana del programa este sigue corriendo en memoria y tengo que cerrar el proceso desde el administrador de tareas
Sin embargo si el servidor esta ejecutandose todo funciona correctamente.

驴Como puedo verificar que el servidor se encuentra escuchando las peticiones en la ip y el puerto seleccionado?

En equipo corro un peque帽o ejemplo para que actue como servidor
Code (fw): Select all Collapse
#include "FiveWin.Ch"

STATIC oWnd, oSocket, oClient, oSay, cLog

//------------------------------------------------------------------------//
Function main(cModo)
聽 聽ModoServidor()
RETURN NIL

#DEFINE PUERTO聽 聽 聽 2019

#define ST_COMMAND 聽1
#define ST_SENDFILE 2
#define FILE_BLOCK 8000

//------------------------------------------------------------------------//
FUNCTION GetIpMaquina()
Local cIp

If WSAStartup() != 0
聽 聽MsgAlert( "WSAStartup error" )
endif
cIp := 聽GetHostByName( GetHostName() )
WSACleanUp()
return cIp

//------------------------------------------------------------------------//
FUNCTION ModoServidor()
Local cTitle, oBtn

cLog := ""

DEFINE WINDOW oWnd TITLE cTitle

cTitle := "Servidor de Sockets "
cTitle += GetIPMaquina()
cTitle += ":" + NTRIM(PUERTO)

DEFINE WINDOW oWnd TITLE cTitle FROM 0, 0 TO 24, 80
@ 1,1 SAY oSay PROMPT cLog Size 聽600,300 BORDER
//@ 0,0 BUTTON oBtn PROMPT "pulsar" ACTION oSay:SetText("Hola")//Pulsado(oSay)

ACTIVATE WINDOW oWnd;
聽 聽 聽 聽 聽 ON INIT Iniciar() ;
聽 聽 聽 聽 聽 VALID Cerrar()

RETURN NIL
//------------------------------------------------------------------------//
STATIC FUNCTION Iniciar()
oWnd:Move(50,50)
Server()
RETURN .F.

//------------------------------------------------------------------------//
STATIC FUNCTION Cerrar()
oSocket:End()
sayLog("Cerrando servidor")
RETURN .T.

//------------------------------------------------------------------------//
STATIC FUNCTION SayLog(cValue)
cLog += cValue + CRLF
//Depura(cLog)
//Depura(oSay)
oSay:SetText(cLog)
RETURN NIL


//------------------------------------------------------------------------//
STATIC function Server()

聽 聽oSocket = TSocket():New( PUERTO )

聽 聽oSocket:bAccept = { | oSocket | oClient := TSocket():Accept( oSocket:nSocket ),;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽oClient:Cargo := ST_COMMAND,;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽oClient:bRead := { | oSocket | OnRead( oSocket ) },;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽oClient:bClose := { | oSocket | OnClose( oSocket ) } }

聽 聽oSocket:Listen()
聽 聽SayLog("Servidor iniciado")
聽 聽SayLog("Esperando peticiones")

return nil


//------------------------------------------------------------------------//
STATIC function OnRead( oSocket )

聽 聽local cData := oSocket:GetData()
聽 聽local cToken, cRespuesta

聽 聽SayLog("Datos recibidos: " + 聽cData)

聽 聽If cData == "GET_SRV"
聽 聽 聽 SayLog("Solicitud de conexi贸n recibida")
聽 聽 聽 oSocket:SendData(cData + 聽"_OK")
聽 聽 聽 RETURN NIL
聽 聽EndIf

聽 聽SayLog("Petici贸n desconocida: " + 聽cData)

return nil

//------------------------------------------------------------------------//
STATIC function OnClose( oSocket )

聽 聽sayLog("El cliente ha cerado el socket")
聽 聽//MsgInfo( "Client has closed!" )
聽 聽oSocket:End()
return nil


Para realizar la conexi贸n hago lo siguiente:

Code (fw): Select all Collapse
...

STATIC FUNCTION Crearconexion()
聽 聽Local nRet

聽 聽SayLog("Creando socket contra la IP " + cIpSrv )

聽 聽oSocket = TSocket():New( nPuerto )
聽 聽oSocket:bRead 聽 聽= { | oSocket | ProcesaRespuesta( oSocket ) }

聽 聽// Never use a MsgInfo() here because it hangs Windows!!!
聽 聽oSocket:bConnect = { || Depura( "Conectado") }
聽 聽oSocket:bClose 聽 = { || SrvClose() }
聽 聽nRet := oSocket:Connect( cIpSrv ) // use the server IP address here
聽 聽Depura(nRet, "NRet")
聽 聽Depura(oSocket:nRetCode, "osocket:nRetcode")
聽 聽//ComprobarConexion()
聽 聽SayLog("Fin creando socket contra IP")

RETURN .F.


//------------------------------------------------------------------------//
STATIC FUNCTION Cerrar()
聽 聽SayLog("oSocket:Close()")
聽 聽If lConectado
聽 聽 聽 oSocket:Close()
聽 聽 聽 oSocket:End()
聽 聽Else
聽 聽 聽 SayLog("No estamos conectados")
聽 聽EndIf
聽 聽SayLog("socket cerrado. Esperndo 1 segundo")
聽 聽syswait(1)
RETURN .T.



STATIC FUNCTION ProcesaRespuesta( cRespuesta)
// Comprobando conexi贸n
If oSocket:Cargo = "GET_SRV"
聽 聽If cRespuesta == oSocket:Cargo + "_OK"
聽 聽 聽 lConectado := .T.
聽 聽 聽 RETURN .T.
聽 聽EndIf
EndIf

RETURN NIL
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: SOCKETS
Posted: Fri Oct 11, 2019 04:24 AM

Revisa los ejemplos sockserv.prg y sockcli.prg en FWH\samples

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 203
Joined: Tue Oct 11, 2005 02:17 PM
Re: SOCKETS
Posted: Fri Oct 11, 2019 06:04 AM
Antonio, gracias por responder

He intentado ejecutar este ejemplo pero se produce un error de ejecuci贸n



Code (fw): Select all Collapse
 聽Time from start: 0 hours 0 mins 1 secs 
聽 聽Error occurred at: 10/11/19, 08:00:59
聽 聽Error description: Error BASE/1004 聽No exported method: SENDDATA
聽 聽Args:
聽 聽 聽[ 聽 1] = U 聽 
聽 聽 聽[ 聽 2] = C 聽 Hello from server!

Stack Calls
===========
聽 聽Called from: 聽=> SENDDATA( 0 )
聽 聽Called from: sockserv.prg => (b)MAIN( 23 )
聽 聽Called from: .\source\classes\BTNBMP.PRG => TBTNBMP:CLICK( 471 )
聽 聽Called from: .\source\classes\BTNBMP.PRG => TBTNBMP:LBUTTONUP( 662 )
聽 聽Called from: .\source\classes\CONTROL.PRG => TCONTROL:HANDLEEVENT( 1723 )
聽 聽Called from: .\source\classes\BTNBMP.PRG => TBTNBMP:HANDLEEVENT( 1465 )
聽 聽Called from: .\source\classes\WINDOW.PRG => _FWH( 3234 )
聽 聽Called from: 聽=> WINRUN( 0 )
聽 聽Called from: .\source\classes\WINDOW.PRG => TWINDOW:ACTIVATE( 1003 )
聽 聽Called from: sockserv.prg => MAIN( 25 )


Ejecuto el programa, pulso en bot贸n escuchar y al pulsar el bot贸n para hablar con el cliente se produce este error
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: SOCKETS
Posted: Fri Oct 11, 2019 10:40 AM

Que versi贸n de FWH est谩s usando ?

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 203
Joined: Tue Oct 11, 2005 02:17 PM
Re: SOCKETS
Posted: Fri Oct 11, 2019 11:31 AM

Fivewin Harbour 13.06

Posts: 563
Joined: Sun Oct 09, 2005 07:23 PM
Re: SOCKETS
Posted: Fri Oct 11, 2019 05:13 PM
En el m茅todo SendData( cData ) de la clase solucion茅 el problema con la siguiente modificaci贸n.

En la clase TSocket me ha dado un fallo que dejaba colgado el programa metido en un bucle infinito.
Esto ocurr铆a al enviar datos cuando se perd铆a la conexi贸n durante el proceso la conexi贸n de red o el ordenador que hace de servidor estaba desconectado, lo que viene a ser lo mismo.

El posible error est谩 en el m茅todo SenData, en concreto en la siguiente secci贸n de c贸digo:

Code (fw): Select all Collapse
if WSAGetLastError() != WSAEWOULDBLOCK
                // exit
             endif


Conviene descomentar la l铆nea donde dice "//exit", o bien hacer una gesti贸n del error en esa parte, si no se descomenta se puede quedar en un bucle sin fin, que es lo que te podr铆a estar sucediendo.
Posts: 203
Joined: Tue Oct 11, 2005 02:17 PM
Re: SOCKETS
Posted: Sat Oct 12, 2019 07:31 AM

Gracias voy a probar

Posts: 731
Joined: Fri Oct 07, 2005 07:42 AM
Re: SOCKETS
Posted: Sun Oct 13, 2019 05:36 PM

Mi consejo es que si vas hacer un servidor y un cliente, olvides el tema de los sockets, y montate un servidor resfull, tu vida te lo agraceder谩 :lol:

Saludos

Rafa Carmona ( rafa.thefullARROBAgmail.com___quitalineas__)
Posts: 203
Joined: Tue Oct 11, 2005 02:17 PM
Re: SOCKETS
Posted: Mon Oct 21, 2019 06:36 PM

No se a que te refieres,
Podr铆as darme m谩s informaci贸n.

De momento he conseguido la comunicaci贸n contra un dispositivo ANDROID y el programa funciona correctamente. Pero el cliente ya est谩 pensando en nuevas funcionalidades y a lo mejor me interesar铆a otra soluci贸n.
Un saludo.

Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: SOCKETS
Posted: Mon Oct 21, 2019 06:48 PM
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noci贸n del tiempo

El secreto de la felicidad no est谩 en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 203
Joined: Tue Oct 11, 2005 02:17 PM
Re: SOCKETS
Posted: Mon Oct 21, 2019 07:04 PM

Gracias voy a ver los videos
Un saludo

Continue the discussion