Creando un dummy Server con idTCPServer (Indy 9)

Solo tenemos que crear una nueva aplicación, y utilizar los siguientes componentes:

procedure TForm3.ConnectClick(Sender: TObject); var idSocket: TIdSocketHandle; begin idSocket := IdTcpServer1.Bindings.Add; idSocket.IP := Edit2.text; idSocket.Port := StrToInt(Edit1.text); Log('Binding IP: ' + Edit2.text + ':' + Edit1.text); IdTcpServer1.Active := true; end; procedure TForm3.DisconnectClick(Sender: TObject); begin IdTcpServer1.Bindings.Clear; IdTcpServer1.Active := false; Log('Disconnected'); end; procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction); begin DisconnectClick(Sender); end; procedure TForm3.IdTCPServer1Connect(AThread: TIdPeerThread); begin Log('Connected at : ' + AThread.Connection.Socket.Binding.PeerIP + ':' + IntToStr(AThread.Connection.Socket.Binding.Port)); end; procedure TForm3.IdTCPServer1Disconnect(AThread: TIdPeerThread); begin Log('Disconnected from : ' + AThread.Connection.LocalName + ':' + IntToStr(AThread.Connection.Socket.Binding.Port)); end; procedure TForm3.IdTCPServer1Execute(AThread: TIdPeerThread); var CommBlock: string; begin if not AThread.Terminated and AThread.Connection.Connected then begin AThread.Connection.WriteLn(1000); //default value CommBlock := AThread.Connection.ReadLn; //read from client Log('Received : ' + CommBlock + ' Sending value: ' + IntToStr(1000)); end; end; procedure TForm3.Log(s: string); begin Memo1.Lines.Add(DateTimeToStr(now) + ' ' + s); end;
Una vez hecho ésto y asignados los eventos en el componente, podemos iniciar nuestro servidor y darle al botón conectar para iniciar el servidor. Aqui tenemos que utilizar la IP que hará de servidora y su puerto.
Si iniciamos una comunicación por ejemplo con Firefox (poniendo la IP y el puerto en la barra de navegación xxx.xxx.xxx.xxx : xxxx (en mi caso 192.168.1.1:8080) entonces recibiremos la siguiente respuesta en nuestra aplicación:
08/09/2009 15:33:24 Binding IP: 192.168.1.1:8080 08/09/2009 15:33:28 Connected at : 192.168.1.1:8080 08/09/2009 15:33:28 Received : GET / HTTP/1.1 Sending : 1000 08/09/2009 15:33:28 Received : Via: 1.1 SRVISA01 Sending : 1000 08/09/2009 15:33:28 Received : User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.9.0.13) Gecko/2009073022 Firefox/3.0.13 Sending : 1000 08/09/2009 15:33:28 Received : Host: 192.168.1.1:8080 Sending : 1000 08/09/2009 15:33:28 Received : Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Sending : 1000 08/09/2009 15:33:28 Received : Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3 Sending : 1000 08/09/2009 15:33:28 Received : Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Sending : 1000 08/09/2009 15:33:28 Received : Keep-Alive: 300 Sending : 1000 08/09/2009 15:33:28 Received : Connection: Keep-Alive Sending : 1000 08/09/2009 15:33:28 Received : Sending : 1000
Sabes como mandar una imagen al navegador?
ReplyDeleteTienes que enviar un Stream. Hay varias maneras de hacerlo.
DeleteJordi
Hola amigo, se puede ejecutar una acción al momento de que reciba texto?. Por ejemplo mostrar el texto por medio de un showMessage
ReplyDeleteHola Hugo,
Deletesi que se puede, solo tienes que usar la unidad del showMessage y mostrar el mensaje.
Jordi