Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Eliminar Buffer TCP (https://www.clubdelphi.com/foros/showthread.php?t=68443)

jlsc 14-06-2010 19:33:34

Eliminar Buffer TCP
 
Buenas a todos,

Alguna idea de como vaciar el buffer de TCP con Sockets. Tengo un ClientSocket que se intenta conectar a un ServerSockets cuando pierde conexión con este, La idea es si el SocketServer está conectado entonces me conecto en cuanto el ServerSocket se desconecta activo un timer para verificar cada cierto tiempo si el Server socket se ha vuelto a conectar y entonces me conecto, el problema es que cuando el cliente está mucho tiempo sin poder conectar con el ServerSocket, me genera un error :

Windows socket error: No se puede ejecutar la operación en un socket ya que el sistema no tiene suficiente espacio en buffer o porque la cola estaba llena(10055) on Api connect.

Me imagina que es que el buffer se llena de todos los intentos que hace para conectarse pero no se como poder solucionar el error. ¿Alguna idea?

Muchas gracias de antemano

un saludo

Jorge

//---------------------------Código Fuente-----------------------------
unit UMensaCliente;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp, Mask, DBCtrls, ExtCtrls, jpeg, IdBaseComponent, IdComponent, IdTCPConnection,
IdSimpleServer, IdTCPServer, IdMappedPortTCP, IdTCPClient, Sockets, Menus, Gauges, Buttons,
Grids, DirOutln, ComCtrls, Spin, ShellApi, Winsock, IdIPWatch, ImgList;

type
TThePhoneHouseForm = class(TForm)
ClientSocket1: TClientSocket;
IdIPWatch1: TIdIPWatch;
Image3: TImage;
mMensaje: TMemo;
StatusBar1: TStatusBar;
Panel1: TPanel;
MainMenu1: TMainMenu;
Menu1: TMenuItem;
Cerrar1: TMenuItem;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure FormDestroy(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure Timer1Timer(Sender: TObject);
procedure Cerrar1Click(Sender: TObject);
private
{ Private declarations }
IconData : TNotifyIconData;
procedure WMQueryEndSession(var Msg: TWMQueryEndSession); message WM_QUERYENDSESSION;
procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;
procedure Espabila(var Msg : TMessage); message WM_USER+1 ;
public
{ Public declarations }
end;

var
ThePhoneHouseForm: TThePhoneHouseForm;
ControlaError:Integer;

implementation

uses Acerca;
{$R *.dfm}
function GetUserName : string;
var
tamanoBuffer: Cardinal;
bufferUsuario: array[0..MAX_PATH] of Char;
begin
tamanoBuffer := SizeOf(bufferUsuario);
Windows.GetUserName(bufferUsuario, tamanoBuffer);
Result := bufferUsuario;
end;

procedure TThePhoneHouseForm.WMQueryEndSession(var Msg: TWMQueryEndSession);
begin
ClientSocket1.Active:=false;
Application.Terminate;
ExitWindowsEx(EWX_FORCE,0);
end;

function IPAddrToName(IPAddr: string): string;
var
SockAddrIn: TSockAddrIn;
HostEnt: PHostEnt;
WSAData: TWSAData;
begin
WSAStartup($101, WSAData);
SockAddrIn.sin_addr.s_addr := inet_addr(PChar(IPAddr));
HostEnt := gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET);
if HostEnt <> nil then
Result := StrPas(Hostent^.h_name)
else
Result := '';
end;

procedure TThePhoneHouseForm.FormCreate(Sender: TObject);
begin
ClientSocket1.Port:= 1500;
ClientSocket1.Host:='10.34.18.125';
ClientSocket1.Active:=true;
ControlaError:=1;
end;

procedure TThePhoneHouseForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ClientSocket1.Active:=False;
end;

procedure TThePhoneHouseForm.WMSysCommand(var Msg: TWMSysCommand);
begin
if (Msg.CmdType = SC_Close) then begin
with IconData do
begin
cbSize := sizeof(IconData);
Wnd := Handle;
uID := 100;
uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
uCallbackMessage := WM_USER + 1;
{Usamos de icono el mismo de la aplicacion}
{We use the same icon as the application}
hIcon := Application.Icon.Handle;
{Como Hint del icono, el nombre de la aplicacion}
{The name of te app for the hint of the icon}
StrPCopy(szTip, Application.Title);
end;
{Ponemos el icono al lado del reloj}
{Place the icon next to the clock}
Shell_NotifyIcon(NIM_ADD, @IconData);
{Ocultamos la Form...}
{Hide the form...}
Hide;
end else DefaultHandler(Msg);
end;
procedure TThePhoneHouseForm.Espabila(var Msg : TMessage);
{Aqui se recibe la pulsacion sobre el icono}
{Here we recieve the click on the icon}
var
p : TPoint;
begin
if Msg.lParam = WM_LBUTTONDOWN then begin
ThePhoneHouseForm.Show;
ShowWindow(Application.Handle, SW_HIDE);
{Y nos cargamos el icono de la system tray}
{Destroy the systray icon}
Shell_NotifyIcon(NIM_DELETE, @IconData);
IconData.Wnd:=0;
end;
end;

procedure TThePhoneHouseForm.Button1Click(Sender: TObject);
begin
if ClientSocket1.Active=true then
ClientSocket1.Socket.SendText(mMensaje.text);
end;

procedure TThePhoneHouseForm.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
var
bTemp:TBitmap;
begin
ClientSocket1.Socket.SendText(ClientSocket1.Socket.LocalHost+','+ClientSocket1.Socket.LocalAddress+' ,'+GetUserName+','+''+','+'Conectado'+',');
Statusbar1.Panels[0].Text :='Conectado';
Statusbar1.Panels[1].Text :='';
Timer1.Enabled:=false;
ControlaError:=1;
end;

procedure TThePhoneHouseForm.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ClientSocket1.Active:=false;
Statusbar1.Panels[1].Text :='Desconectado: Imposible conectar a ' + ClientSocket1.Host + ' ';
Statusbar1.Panels[0].Text :='';
ControlaError:=0;
ErrorCode := 0;
Timer1.Enabled:=true;
end;


procedure TThePhoneHouseForm.FormDestroy(Sender: TObject);
begin
ClientSocket1.Active:=False;
end;

procedure TThePhoneHouseForm.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
begin
ThePhoneHouseForm.Show;
ShowWindow(Application.Handle, SW_HIDE);
{Y nos cargamos el icono de la system tray}
{Destroy the systray icon}
Shell_NotifyIcon(NIM_DELETE, @IconData);
IconData.Wnd:=0;
mMensaje.Clear;
mMensaje.Lines.Add(ClientSocket1.Socket.ReceiveText+' '+ TimeToStr(now));
end;

procedure TThePhoneHouseForm.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
if ((ClientSocket1.Active=false) and (ControlaError<>0 ))then
ClientSocket1.Socket.SendText(ClientSocket1.Socket.LocalHost+','+ClientSocket1.Socket.LocalAddress+' ,'+GetUserName+','+''+'*'+'Desconectado'+',');
ClientSocket1.Active:=false;
Statusbar1.Panels[1].Text :='Desconectado: Imposible conectar a ' + ClientSocket1.Host + ' ';
Statusbar1.Panels[0].Text :='';
Timer1.Enabled:=true;
ControlaError:=1;
end;

procedure TThePhoneHouseForm.Timer1Timer(Sender: TObject);
begin
if (ClientSocket1.Active=false) then
ClientSocket1.Active:=True
end;

procedure TThePhoneHouseForm.Cerrar1Click(Sender: TObject);
begin
Acercade.Show;
Acercade.Timer1.Enabled:=true;
end;

end.

Archer 16-06-2010 19:26:50

Hola, supongo que alguien se encargara de "reñirte" por no poner el codigo con las etiquetas correspondientes, yo me limito a darte la solucion:

Primera, deberias de usar los metodos Open() y Close() en lugar de poner "Active" a true o a false.

Segunda, antes de hacer los Open() (recuerda que estos sustituyen a tus Active := true) haz un Close() pero no del ClientSocket si no del Socket que contiene:

Código Delphi [-]
ClientSocket1.Close(); //NOOOOOO

ClientSocket1.Socket.Close(); //SIIIII

Luego el Open() lo puedes hacer como hasta ahora:

Código Delphi [-]
ClientSocket1.Open();

Saludos.

jlsc 16-06-2010 22:13:00

Hola Archer,

Muchas gracias, he probado tu solucion y funciona perfectamente, eres lo maximo. y lo siento el no haber puesto la etiquetas, para la proxima lo tendre muy en cuenta.

Muchas gracias de verdad, me he pasado horas tratando de encontrar una solucion y muchas gracias tambien a este magnifico foro.

Un saludo

Jorge

:D


La franja horaria es GMT +2. Ahora son las 14:55:56.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi