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
Para realizar la conexi贸n hago lo siguiente:
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
#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 nilPara realizar la conexi贸n hago lo siguiente:
...
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