Ver Mensaje Individual
  #12  
Antiguo 20-05-2004
Avatar de Sr_Sombrero
Sr_Sombrero Sr_Sombrero is offline
Miembro
 
Registrado: may 2003
Posts: 49
Reputación: 0
Sr_Sombrero Va por buen camino
Luego de tomar distancia del proyecto volver logré hacerlo funcionar y sin usar los INDY.
Lo he probado a fondo y anda muy bien, aunque con las INDY tal vez hubiera quedado más prolijo.
Acá les dejo la solución por si a alguien le sirve.

Usando Socket.RemoteAddress te devuelve un entero que es el sock y que es único por cada conexión.


Código:
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
var
ali: TListItem;
strIndex: string;
begin

  aLi := ListView1.Items.Add;
  with aLi do
     begin
      Caption:= IntToStr(Socket.SocketHandle);
      SubItems.Add(Socket.RemoteAddress);
      SubItems.Add('');
      SubItems.Add('');
      SubItems.Add('');
      SubItems.Add('');
      SubItems.Add('');
      SubItems.Add('');
end;
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
  var
lvItem: TListItem;
s: string;

begin
// GetToken es una funcion para separ el texto que llega al socket
s:= Socket.ReceiveText;

if copy(s,1,7)='details' then begin
lvItem:= ListView1.FindCaption(0,intToStr(Socket.SocketHandle),true,true,false);
  if lvItem <> nil then begin
  lvItem.SubItems[1]:=(GetToken(S,';',2));
  lvItem.SubItems[2]:=(GetToken(S,';',3));
  lvItem.SubItems[3]:=(GetToken(S,';',4));
  lvItem.SubItems[4]:=(GetToken(S,';',5));
  lvItem.SubItems[5]:=(GetToken(S,';',6));
  lvItem.SubItems[6]:=(GetToken(S,';',7));
  end;
  end;
end;

procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
var
lvItem: TListItem;
begin
//busca el sock que se desconectó y la elimina de la lista
lvItem:=ListView1.FindCaption(0,intToStr
Socket.SocketHandle),true,true,false);
if lvItem <> nil then
lvItem.Delete;
end;

//esto es para enviar un comando al cliente desde la Listview
// sabiendo el sock y no el numero de conexión

procedure TForm1.Connect1Click(Sender: TObject);
var
intNumConnect, intSocket: integer;
begin
intNumConnect:=-1;

if listView1.ItemIndex = -1 then begin
    showmessage('There is no server selected');
    exit;
   end
   else
   begin
   intSocket:= StrToInt(listView1.Selected.Caption);
   repeat
   intNumConnect:= intNumConnect + 1;
   if
   ServerSocket1.Socket.Connections[intNumConnect].SocketHandle=IntSocket then
   ServerSocket1.Socket.Connections[intNumConnect].SendText('connect');
   until
   (intNumConnect + 1) = ServerSocket1.Socket.ActiveConnections;
   end
end;
Saludos

Última edición por Sr_Sombrero fecha: 20-05-2004 a las 22:17:01.
Responder Con Cita