[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Disable Internet After Some Time.

Posted on 2004-11-14
17
Medium Priority
?
229 Views
Last Modified: 2010-04-05
Hey,

I need a code to disable Internet access after some time, lets say 3 hours.

So I guess what I need is a code that checks how long internet is active,
and then a code that is able to disable it (preferrably both dialup and cable, just any 'Internet').

Thnx!
Angelo.
0
Comment
Question by:AngeloX
  • 6
  • 5
  • 4
  • +2
17 Comments
 
LVL 11

Expert Comment

by:calinutz
ID: 12593214
You can do it like this....
Check your internet connection type like this:

uses
  WinInet;

const
  MODEM = 1;
  LAN = 2;
  PROXY = 4;
  BUSY = 8;

function GetConnectionKind(var strKind: string): Boolean;
var
  flags: DWORD;
begin
  strKind := '';
  Result := InternetGetConnectedState(@flags, 0);
  if Result then
  begin
    if (flags and MODEM) = MODEM then strKind := 'Modem';
    if (flags and LAN) = LAN then strKind := 'LAN';
    if (flags and PROXY) = PROXY then strKind := 'Proxy';
    if (flags and BUSY) = BUSY then strKind := 'Modem Busy';
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  strKind: string;
begin
  if GetConnectionKind(strKind) then
    ShowMessage(strKind);
end;



And then you can use your connection type to solve your problem for each type.
0
 
LVL 11

Expert Comment

by:calinutz
ID: 12593294
You can place a Timer Component on a form (the same form as above... for example) and place on it 2 Buttons. One that says "Connect" and one that says "Disconnect".
On the Connect button event you start (activate the timer). And set the timer for as long as you want to keep up the connection.
And I recommend for example to disconnect from a LAN connection by removing the gateway settings from the network adapter and place it back when you want to reenable it. It has flaws on Win98 because it will require reboot. On XP it does not require reboot.

To connect / disconnect using dialup you can use this code:

InternetAutoDial(dwFlags, hwndParent) ;

Where dwFlags can be one of the following values:
INTERNET_AUTODIAL_FAILIFSECURITYCHECK - causes InternetAutodial to fail if file and printer sharing is disabled for Windows 95 or later.
INTERNET_AUTODIAL_FORCE_ONLINE - forces an online Internet connection.
INTERNET_AUTODIAL_FORCE_UNATTENDED - forces an unattended Internet dial-up.
and, hwndParent is Handle to the parent window.

Exmaple:

~~~~~~~~~~~~~~~~~~~~~~~~~
uses WinInet;
...
InternetAutoDial(internet_autodial_force_unattended, Handle) ;

{
To disconnect this automatic dial-up connection, use
}

InternetAutodialHangup(0)

{
Another way is to specify the name
of the connection you wish to use
(ConnectionName), from Dialup
networking:
}

if InternetDial
    (0, PChar(ConnectionName), INTERNET_AUTODIAL_FORCE_UNATTENDED, dwConn, 0) <> ERROR_SUCCESS then exit;
//Do something with Internet...
InternetHangup(dwConn, 0) ;
~~~~~~~~~~~~~~~~~~~~~~~~~


0
 
LVL 11

Expert Comment

by:calinutz
ID: 12593326
You can make your software startUp with Windows and check on startup if there is any internet connection. And if there is, you simply start the timer. You can also make the application to stay in the TRAY showing you a different Glyph if connected or not.

Don't ask me to build it for you all the way... You can do this on rent a coder and portals like that one.

I really think it's more that enough code here to help you out build it by yourself
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:calinutz
ID: 12593367
There are more than one tray icon components out there. You can look for one here http://sourceforge.net/projects/tpsystools/ or on torry ... or... lots of places.
0
 

Author Comment

by:AngeloX
ID: 12596254
Hey, I really appreciate your answer, but maybe my question sounds easier than it really is. I probably would have given more points than this if this weren't the maximum. Right now you gave me a code to check the internet connection, thnx a lot for that, but what I was actually looking for is a code to hangup the internet connection LAN (like the question title says), and if that isnt possible, then a code that blocks software from using the internet (firewall).

And then I want do it the right way and not by changing some windows settings that could be edited by a smart windows user. What I want is some code, or just the start of it, to intercept internet connection, like firewalls do, and then block it. I don't think this is impossible since firewalls do exist, I just dont know where to start with blocking programs from accessing the internet.

Thnx!
0
 
LVL 3

Expert Comment

by:gandalf_the_white
ID: 12604591
you know that firewalls have to reboot to inject themselves into the system.
so you need to code a system service that will do something like that

why do you need this?
maybe we find a different workaround for your problem
0
 

Author Comment

by:AngeloX
ID: 12605001
Hey... I need this so that parents are able to set a maximum time of internet usage.
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12608835
Well a simple way would be to open a socket connection (client connecting to a local server socket) on port 80 and the other HTTP ports. This will effectively block the internet. I can post code for this if you like this idea

Regards,

Hypoviax
0
 

Author Comment

by:AngeloX
ID: 12608974
Yes if that will do it please post it and you will get the points. Thnx!
0
 
LVL 5

Accepted Solution

by:
Hypoviax earned 2000 total points
ID: 12612197
This is my program to block ports. You may need to do this if using Delphi7:

Go to Project|Options|Packages then 'Add' then in the window that opens, go to where delphi is installed then to \Bin and find 'dclsockets70.bpl' and add that in.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,ScktComp, StdCtrls, Sockets, JvComponent, JvTrayIcon, ComCtrls,
  Menus, ExtCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    JvTrayIcon1: TJvTrayIcon;
    Edit2: TEdit;
    Edit3: TEdit;
    Button2: TButton;
    ProgressBar1: TProgressBar;
    ListView1: TListView;
    PopupMenu1: TPopupMenu;
    Exit1: TMenuItem;
    Restore1: TMenuItem;
    Timer1: TTimer;
    Label1: TLabel;
    Button4: TButton;
    Timer2: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ListView1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Exit1Click(Sender: TObject);
    procedure Restore1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
  private
    { Private declarations }
  public
socket:array[0..65536] of tclientsocket;
server:array[0..65536] of tserversocket;
m:boolean;
  end;

var
  Form1: TForm1;

implementation


{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var listitem:tlistitem;
begin
server[listview1.Items.Count-1]:=tserversocket.Create(button1);
server[listview1.Items.Count-1].Port:=strtoint(edit1.text);
server[listview1.Items.Count-1].Active:=true;
socket[listview1.Items.Count-1]:=tclientsocket.Create(button1) ;
socket[listview1.Items.Count-1].Port:=strtoint(edit1.text);
socket[listview1.Items.Count-1].Address:='127.0.0.1'  ;
socket[listview1.Items.Count-1].Active:=true;
listitem:=listview1.items.add;
listitem.caption:=edit1.text ;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
X:integer;
listitem:tlistitem;
begin
progressbar1.Position:=0;
progressbar1.Max:=strtoint(edit3.Text);
M:=false;
For X:=strtoint(edit2.Text) to strtoint(edit3.Text) do
begin
try
if m=true then
exit
else
server[listview1.Items.Count-1]:=tserversocket.Create(button1);
server[listview1.Items.Count-1].Port:=x;
server[listview1.Items.Count-1].Active:=true;
socket[listview1.Items.Count-1]:=tclientsocket.Create(button1) ;
socket[listview1.Items.Count-1].Port:=x;
socket[listview1.Items.Count-1].Address:='127.0.0.1'  ;
socket[listview1.Items.Count-1].Active:=true;
listitem:=listview1.items.add;
listitem.caption:=inttostr(x) ;
progressbar1.Position:=x
except
end;
end;
progressbar1.Position:=0;
end;



procedure TForm1.ListView1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if key=vk_delete then
begin
server[listview1.Selected.Index].Close;
//server[listview1.Selected.Index].Free;
listview1.Selected.Delete;
end;
end;

procedure TForm1.Exit1Click(Sender: TObject);
begin
application.Terminate
end;

procedure TForm1.Restore1Click(Sender: TObject);
begin
jvtrayicon1.ShowApplication;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
jvtrayicon1.HideApplication;
timer1.Enabled:=false;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var X:integer;
begin
try
progressbar1.Position:=0;
progressbar1.Max:=listview1.Items.count-1;
For X:=0 to listview1.Items.count-2 do
begin
server[x].Close ;
socket[x].Close;
progressbar1.Position:=x;
end;
progressbar1.Position:=0;
except
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
m:=true;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
m:=false;
end;

procedure TForm1.Button4Click(Sender: TObject);
var X:integer;
begin
try
progressbar1.Position:=0;
progressbar1.Max:=listview1.Items.count-1;
For X:=0 to listview1.Items.count-2 do
begin
server[x].Close ;
socket[x].Close;
progressbar1.Position:=x;
end;
progressbar1.Position:=0;
except
end;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
//m:=true;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
if mouse.CursorPos.X=0 then
M:=true;
end;

end.

Regards,

Hypoviax
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12612209
edit1=port number to block

edit2 = start range to block

edit3= end range to block

Just note this method is not worthwhile to block a large number of ports as it will take a long time. It is fine for a small range

Have a look here for the port numbers or do a google search:

http://www.felgall.com/netint4.htm

Regards,

Hypoviax
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12612225
Most definetly using my method blocking the range from 1024 -> 5000 blocks webbrowseing

Regards,

Hypoviax
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12612236
Block that range aswell as port 80 for it to work

Regards,

Hypoviax
0
 

Author Comment

by:AngeloX
ID: 12613669
Thnx man!
0
 

Author Comment

by:AngeloX
ID: 12618923
Hey,
is there a way to list all open ports so you can close them. Also is it possible to know what internet explorer is using because if i disable 80 and 8080 it is still going to sites. thnx!
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12621535
well you can know which ports are in use by trying to open a server socket on that connection. Technically only one socket is allowed to be open on that port. So if you get an error that the port is in use etc then that port is in effect open. Use try except blocks to handle the error TRY connect to port EXCEPT port is in  use.

Regards blocking 80 and 8080 i have found that just blocking them doesn't work. That is why i specified blocking the range from 1024 to 5000 because i know from testing that definitely blocking these AND port 80 effectively stops webbrowsing.

Any questions please ask :-)

Regards,

Hypoviax
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 12621747
Here's a component that can retrieve all open connections a-la netstat... that way, you can see what the currently open ports are: http://www.delphi32.com/vcl/2230/download,xid,2230.asp
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question