Ver Mensaje Individual
  #10  
Antiguo 04-08-2015
Avatar de dec
dec dec is offline
Moderador
 
Registrado: dic 2004
Ubicación: Alcobendas, Madrid, España
Posts: 13.107
Reputación: 34
dec Tiene un aura espectaculardec Tiene un aura espectacular
Hola,

Probando el archivo adjunto de mi último mensaje el ejemplo no parece funcionar bien, al menos aquí en Windows 10. Sin embargo, probando el programa en que estoy usando "Sockets" y que originó este hilo sí que puedo enviar y recibir cadenas de un cliente a un servidor y viceversa. Lo cierto es que ahora mismo on sabría bien qué responderte. Quizá el código de abajo, usado en mi programa, te dé una idea y te sirva de algo:

Código Delphi [-]
unit USocketsCommom;

{
  The code of this unit is mainly based in Socket's
  related code found on Internet and who's author
  is Remy Lebeau, a TeamB team and Delphi developer.
}

interface

uses
  ScktComp;

procedure SendStringToSocket( Socket:
 TCustomWinSocket; Buffer: string );

implementation

uses
  Forms, SysUtils, Classes, WinSock;

procedure SendMemoryStreamToSocket( socket :
 TCustomWinSocket; buffer : TMemoryStream);
var
  dataPtr: PByte;
  numSent: integer;
begin
  buffer.Position := 0;
  dataPtr := PByte( buffer.Memory );
  while buffer.Position < buffer.Size do
  begin
    numSent := socket.SendBuf
    (
      dataPtr^,
      buffer.Size - buffer.Position
    );

    if numSent < 0 then
    begin
      if WSAGetLastError() = WSAEWOULDBLOCK then
      begin
        Sleep( 50 );
        Continue;
      end;
      Exit;
    end;

    if numSent = 0 then
    begin
      Exit; // socket disconnected
    end;

    Inc(dataPtr, numSent);
    buffer.Seek(numSent, soFromCurrent);
    Application.ProcessMessages();
  end;
end;

procedure SendStringToSocket( socket:
 TCustomWinSocket; buffer: string );
var
  ms : TMemoryStream;
begin
  ms := TMemoryStream.Create();
  try
    ms.WriteBuffer( Pointer( buffer )^, Length( buffer ) );
    SendMemoryStreamToSocket( socket, ms );
  finally
    ms.Free();
  end;
end;

end.

Ese es el código que uso en mi programa para enviar cadenas en ambos cliente y servidor. Dicho código parece el resumen de este hilo, en el sentido de que es el que al final estoy usando en mi programa, y, como digo, este funciona como se espera incluso aquí en Windows 10.
__________________
David Esperalta
www.decsoftutils.com
Responder Con Cita