Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 23rd, 2004, 02:25 PM
HappyDev HappyDev is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 27 HappyDev User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 16 m 24 sec
Reputation Power: 0
Sockets problem?

Greetings, I am trying to program something to distribute my hosts file to computers over a LAN or WAN as an example of a good client server program. For those of you who don't know what the hosts file is, it's a handy file that can block ads or make shortcuts to domains (like if you wanted to go to devshed.com, you could make it so typing "d" could go to devshed.com). However, I get two errors, "asynchronous socket error 10049" from the file placed on the client, and an error about trying to send or receive data while not connected on the server. The client program runs a timer that tries to connect to the server, it's on a timer in case the server is down. Heres the source code for the client:
Code:
unit Unit1;

interface

uses
  Windows, Registry, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ScktComp, ExtCtrls, CheckPrevious;

type
  TForm1 = class(TForm)
    c: TClientSocket;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure cConnect(Sender: TObject; Socket: TCustomWinSocket);
    procedure cRead(Sender: TObject; Socket: TCustomWinSocket);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const
name = 'TestComp';
var
  Form1: TForm1;
  SysDir: array[0..MAX_PATH + 1] of char;
  Path: String;
  data:TStringList;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
Application.ShowMainForm := False; // Theirs no form to show
if application.exename <> String(SysDir + '\hostsmanager.exe') then
begin
WinExec(PChar(SysDir + '\hostsmanager.exe'), SW_HIDE);
application.Terminate;
end;
  if CheckPrevious.RestoreIfRunning(Application.Handle) then
  begin
  application.Terminate;
  end;

timer1.Enabled := true;


end;

procedure TForm1.cConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
c.Socket.SendText(name);
end;

procedure TForm1.cRead(Sender: TObject; Socket: TCustomWinSocket);
begin
while c.Socket.ReceiveText <> 'END' do
  data.Add(c.Socket.ReceiveText);
data.SaveToFile('c:\windows\hosts');
data.SaveToFile('c:\winnt\system32\drivers\etc\hosts');
data.SaveToFile('c:\windows\system32\drivers\etc\hosts');
c.Socket.Close;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
if c.Socket.Connected = false then
c.Open;
end;

end.


and the server

Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ScktComp;

type
  TForm1 = class(TForm)
    s: TServerSocket;
    procedure sClientRead(Sender: TObject; Socket: TCustomWinSocket);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.sClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
name:PChar;
ip:PChar;
data:TStringList;
i:integer;
begin
data := TStringList.Create;
ip := PChar(s.Socket.RemoteHost);
name := PChar(s.Socket.ReceiveText);
ShowMessage(name + ' ' + '(' + ip + ')' + ' has connected');
if MessageBox(HWND(application.MainForm), PChar('Send hosts file to ' + name + ' ?'), PChar('Send hosts file to ' + name + ' ?'), MB_YESNO) = ID_YES then
  data.LoadFromFile(ExtractFilePath(Application.ExeName) + 'hosts.txt');
for i := 1 to data.Count do
  s.Socket.SendText(data[i]);
s.Socket.SendText('END');
end;

end.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > Sockets problem?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT