Disable Internet After Some Time.

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.
AngeloXAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

calinutzCommented:
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
calinutzCommented:
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
calinutzCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

calinutzCommented:
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
AngeloXAuthor Commented:
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
gandalf_the_whiteCommented:
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
AngeloXAuthor Commented:
Hey... I need this so that parents are able to set a maximum time of internet usage.
0
HypoviaxCommented:
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
AngeloXAuthor Commented:
Yes if that will do it please post it and you will get the points. Thnx!
0
HypoviaxCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HypoviaxCommented:
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
HypoviaxCommented:
Most definetly using my method blocking the range from 1024 -> 5000 blocks webbrowseing

Regards,

Hypoviax
0
HypoviaxCommented:
Block that range aswell as port 80 for it to work

Regards,

Hypoviax
0
AngeloXAuthor Commented:
Thnx man!
0
AngeloXAuthor Commented:
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
HypoviaxCommented:
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
DragonSlayerCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.