This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!

ritesh thakur

Greenhorn
+ Follow
since Jul 08, 2013
ritesh likes ...
Android C++ Java
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
5
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by ritesh thakur

so, because the class I am extending is throwing and exception, I have to declare a constructor ?
Regards
below is a code to implement a simple program of addition using RMI. When i implement the interface HELLOINTERFACE.java in HELLO.java, why do i have to make a constructor hello in hello.java ?



IS it possible to make a chat system for two different laptops using java RMI, or do i have to use Socket programming.?
Also can I make just one server on one laptop and make it accessible to clients on different laptops (server being on one of them only) ?
Regards.

Ulf Dittmer wrote:Oh, it's not the Java process starting the connections? So what you're talking about is a proxy. Why do you want to write your own? There are any number of filtering proxies out there, even open source ones written in Java, if that's what you want to use.



working on a project.
anyways, what i m not getting is, the code i posted previously, just matches the string name, the other code matches and blocks the site, but when i run the program, in menu, i have to use the previous code to block it, while second code alone can do the job.

Ulf Dittmer wrote:Can you describe in words what that code is supposed to be doing? Especially, are you trying to block outgoing connections or incoming connections? "Blocking a web site" only makes sense for outgoing connections (because incoming connections are not tied to web sites, they're tied to IP addresses), but hard to tell from the code without knowing what it does.



i want it to bound all the outgoing connections. if url matches to the one entered, my browser should not open it.

Ulf Dittmer wrote:The code doesn't seem to accept or monitor either incoming or outgoing connections, so I'm not sure how it could be blocking anything...?

What is the underlying problem you're trying to solve?



this is the server side of the quote. can this help me blocking a website.?

public static void internetserver() throws Exception{

Thread thread=new Thread(){public void run(){


try {String host = "127.0.0.1";
int remoteport = 8080;
int localport = 111;
// Print a start-up message
// And start running the server
runServer(host, remoteport, localport); // never returns
} catch (Exception e) {
System.err.println(e);
}}};
thread.start();}


/**
* runs a single-threaded proxy server on
* the specified local port. It never returns.
*/
static int flagchk=0;

public static void runServer(String host, int remoteport, int localport)
throws IOException {
// Create a ServerSocket to listen for connections with
ServerSocket ss = new ServerSocket(localport);

final byte[] request = new byte[1024];
byte[] reply = new byte[4096];

while (true) {
Socket client = null, server = null;
try { flagchk=0;
// Wait for a connection on the local port
client = ss.accept();

final InputStream streamFromClient = client.getInputStream();
final OutputStream streamToClient = client.getOutputStream();

// Make a connection to the real server.
// If we cannot connect to the server, send an error to the
// client, disconnect, and continue waiting for connections.
try {
server = new Socket(host, remoteport);
} catch (IOException e) {
PrintWriter out = new PrintWriter(streamToClient);
out.print("Proxy server cannot connect to " + host + ":"
+ remoteport + ":\n" + e + "\n");
out.flush();
client.close();
continue;
}

// Get server streams.
final InputStream streamFromServer = server.getInputStream();
final OutputStream streamToServer = server.getOutputStream();

// a thread to read the client's requests and pass them
// to the server. A separate thread for asynchronous.
Thread t = new Thread() {
public void run() {
int bytesRead;
try {
while ((bytesRead = streamFromClient.read(request)) != -1) {

String value = new String(request);
int urlindex = value.indexOf("GET");
int urlindex1=urlindex+4;
String urlvalue=value.substring(urlindex1);
int urlindex2=urlvalue.indexOf(" ");
String url=urlvalue.substring(0,urlindex2); //url
String hostsite1="",url1="";
char c='.';
String temp="";
int count = 0;
for (int i = 0 ; i < url.length() ; i++) {
if ( url.charAt(i) == c ) {
count += 1;
}
}
int firstindex=0,secondindex=0;
if(count==1)
{
firstindex=url.indexOf(".");
url1=url.substring(0, firstindex);
url1="www."+url1;
}
else
{
firstindex=url.indexOf(".");
temp=url.substring(firstindex+1);
secondindex=temp.indexOf(".");
if(firstindex<0)
{firstindex=0;}
if(secondindex<0)
{secondindex=0;}
url1=url.substring(0, (secondindex+firstindex+1));

} url=url1;
int thirdindex=url.indexOf("//");
if(thirdindex<0)
{thirdindex=0;}
url1=url.substring(thirdindex+2);



int hostindex = value.indexOf("Host:");
int hostindex1=hostindex+6;
String hostvalue=value.substring(hostindex1);
int hostindex2=hostvalue.indexOf("\n");
String hostsite=hostvalue.substring(0,hostindex2); //host

count = 0;
for (int i = 0 ; i < hostsite.length() ; i++) {
if ( hostsite.charAt(i) == c ) {
count += 1;
}
}
firstindex=0;secondindex=0;
if(count==1)
{
firstindex=hostsite.indexOf(".");
hostsite1=hostsite.substring(0, firstindex);
hostsite1="www."+hostsite1;
}
else
{
firstindex=hostsite.indexOf(".");
temp=hostsite.substring(firstindex+1);
secondindex=temp.indexOf(".");
hostsite1=hostsite.substring(0, (secondindex+firstindex+1));

}
if(firewallstatus==1)
{for(int i=0;i<x;i++)
{
if((hostsite1.equals(searchsite[i]))||(url1.equals(searchsite[i])))
{
flagchk=1;

PrintWriter out = new PrintWriter(streamToClient);
out.print("This website has been blocked by the FIREWALL. ");
out.flush();out.close();break;
}

}
}
if(flagchk==0)
{streamToServer.write(request, 0, bytesRead);
streamToServer.flush();
} }
} catch (IOException e) {}

// the client closed the connection to us, so close our
// connection to the server.
try {
streamToServer.close();
} catch (IOException e) {
}
}
};

// Start the client-to-server request thread running
t.start();

// Read the server's responses
// and pass them back to the client.
int bytesRead;
try {
while ((bytesRead = streamFromServer.read(reply)) != -1) {
streamToClient.write(reply, 0, bytesRead);
streamToClient.flush();
}
} catch (IOException e) {
}

// The server closed its connection to us, so we close our
// connection to our client.
streamToClient.close();
} catch (IOException e) {
System.err.println(e);
} finally {
try {
if (server != null)
server.close();
if (client != null)
client.close();
} catch (IOException e) {
}
}
}
}
this is my code for blocking a website.

System.out.print("\nEnter the URL of the website you want to block:");
System.out.print("\n\n URL: ");
BufferedReader fr=new BufferedReader(new InputStreamReader
(System.in));
try{


blockedsite[x]=fr.readLine();
int checkurlindex=blockedsite[x].indexOf(".");
if(checkurlindex==-1)
{System.out.print("\n\n*********");
System.out.print("\n* ERROR *");
System.out.print("\n*********");
System.out.print("\n\nYou have entered an ");
System.out.print("invalid URL. Please ");
System.out.print("enter again.\n");
continue;}
char c='.';
String temp="";
int count = 0;
for (int i = 0 ; i < blockedsite[x].length() ; i++) {
if ( blockedsite[x].charAt(i) == c ) {
count += 1;
}
}
int firstindex=0,secondindex=0;
if(count==1)
{
firstindex=blockedsite[x].indexOf(".");
searchsite[x]=blockedsite[x].substring(0, firstindex);
searchsite[x]="www."+searchsite[x];
}
else
{
firstindex=blockedsite[x].indexOf(".");
temp=blockedsite[x].substring(firstindex+1);
secondindex=temp.indexOf(".");
searchsite[x]=blockedsite[x].substring(0, (secondindex+firstindex+1));

}
x+=1;
System.out.print("\n\n");
for(int i=1;i<60;i++)
System.out.print("=");
System.out.print("\n\n");
System.out.print("\n\nThe website has been blocked.\n ");

is the code correct.?
thanks a lot henry. you made the concept very clear to me. thanks a lot again. if i could, i wud hit votes for you a 1000 times. thanks henry again.

Ulf Dittmer wrote:That's not what this code does. As Henry explained, it blocks any process from binding to port 80 - thus no process (in particular no HTTP server, since that's what generally uses port 80) can serve anything on port 80 on that machine.

That is entirely unrelated to which ports on other machines any process (like a web browser) can access from that machine. The inbound port is not the same as the outbound port.

Coding something is not the right way to go about this. Either block outgoing HTTP connections from this machine at your network router (or switch or firewall), or use iptables (on *nix) or the firewall built into Windows to block this kind of traffic.



that helped. thanks ulf dittmer. but as you said:
" it blocks any process from binding to port 80 - thus no process (in particular no HTTP server, since that's what generally uses port 80) can serve anything on port 80 on that machine."
so now no HTTP server, can serve on that port. so how can these sites open.? (machine on which 80 is blocked is same machine on which http sites are still opening)

Paul Clapham wrote:I'm guessing that when you say

when i now try to access http:// sites on the same computer where i have blocked port 80



You really mean

when i now try to access http:// sites from the same computer where i have blocked port 80



Or are these http:// sites really hosted on the computer where you blocked port 80?



yes thats what i mean. i blocked port 80 on computer abc, now abc can open http:// sites, when they are supposed to be blocked.

Henry Wong wrote:

ritesh thakur wrote:

I am using this code to block a port.


It says port has been blocked. but for eg. if I block port port 80, http sites are opening, while http:// sites work on port 80.




And technically, this isn't blocking a port. The application is basically binding to a local port, which disallows anyone else from using that same port. Blocking a port is selective, allowing certain clients to use the service, while blocking others from getting to the service. Since the application uses the port, the service can't use it. You can achieve the same affect by just not starting the service.

Henry



so can i achieve, blocking http:// sites using java or it is not possible.?

Paul Clapham wrote:Hi ritesh, welcome to the Ranch!

Your post is a little unclear to me: did you mean to say that you tried to block port 80 on your computer by running that code you posted, and then you found that connecting to HTTP sites on some other computer that you could still do that?



i meant, using this code i block a port, say 80. but when i now try to access http:// sites on the same computer where i have blocked port 80, i can access them. as http:// sites open on port 80. they shul be blocked.

is there a problem with concept or code.?

-3
down vote
favorite
I am using this code to block a port.

try
{

int c=0;
c=b-1;
ServerSocket ss=new ServerSocket(blockedport[c]);

while(true)
{
Thread.sleep(1200);
if(((check[c])==false)||firewallstatus==0)
{
ss.close();
}
Thread.sleep(1200);
if(((check[c])==true)&& (firewallstatus==1))
{
try
{
ss=new ServerSocket (blockedport[c]);
}
}
}
}
Problem I am facing is that when I check whether the port is blocked using another program, which is as follows:

import java.io.*;
import java.net.*;

public class checkport80 {
public static void main(String[] args) throws IOException {
try {
ServerSocket ss = new ServerSocket(80);
System.out.print("The port is not blocked");
} catch (Exception e) {
System.out.print(e);
}
}
}
It says port has been blocked. but for eg. if I block port port 80, http sites are opening, while http:// sites work on port 80.