hello, please i make a code that i want it to work as follows:
the user input 3 value and after an inspection(that i am not put it now, i want to try it now so i make the condition, if k%2==0)the input is be either spoof or valed,at the begining the state is alert, then i compute the (th)it is a threshold value, so if it is spoof and the th is less than T1 the o/p is accept, and still that(when the user enter other 3 values) untill the th be larger than T1 and then it is switch to the action state and the o/p is (if it is spoof the o/p is drop and if it is not spoof the o/p is forword)and still that (when the user enter other 3 values) until the th is less than T2


so i do my best and write that code but it's o/p is not like that i want, please can u do any help for me,
thank u very much in advance

import java.io.*;
import java.*;
import java.util.*;
import java.lang.*;

public class IP2HC_Inspect{
//instanceof ttt= new table;
public static void main(String[] args)  {
	int totalpkt=0,k=0,th=0,spoofpkt=0;
	double T1=0.3,T2=0.7;
	boolean done=true,spoof=false,sw1=true,sw2=false;
do{
System.out.println(" \n   Enter your packet that contain a valid IP Add with the field of the TTL and the connection type \n ");
++totalpkt;
Scanner keyboard = new Scanner(System.in);  
System.out.println("ip is");
String IP = keyboard.nextLine();
System.out.println("ttl is");
String TTL = keyboard.nextLine();
System.out.println("conn is");
String conntype = keyboard.nextLine();
if (conntype.equalsIgnoreCase("TCP"))
++k;
//int x = Integer.parseInt( scanner.nextLine() );DIC
int T=Integer.parseInt(TTL);
//long ss = IpConvert.ipToLong(IP);
//int s=Integer.parseInt(IP);
int T0=0,Hc;
if(T<=32)
T0=32;
else if(T>32 && T<=64)
T0=64;
else if(T>64 && T<=128)
T0=128;
else if(T>128 && T<=255)
T0=255;
System.out.println("T0 is  "+T0);

Hc=T0-T;
System.out.println("Hc is  "+Hc);
long s=IpConvert.ipToLong(IP);
System.out.println("ip in long is  "+s);
System.out.println("totalpkt is  "+totalpkt);
System.out.println("k is  "+k);
if(k%2==0)
{spoof=true;
++spoofpkt;
System.out.println("spoof is   "+spoof+"  spoofpkt is  "+spoofpkt);
}else
System.out.println("ligtmate");
th=spoofpkt/totalpkt;
System.out.println("th is   "+th);
if (sw1)
{
	if(th<T1)
	{System.out.println("alert state");
		System.out.println("pkt accepted");}
		if(spoof && th>T1)
		{sw2=true;
		sw1=false;
		}


if (sw2){
System.out.println("action state");
if (spoof && th>T1)
System.out.println("pkt is droped");
else if (!spoof && th>T1)
System.out.println("pkt in action is forword");
else if(!spoof && th<T2)
{System.out.println("back to alert");
sw1=true;sw2=false;
}
}
//if (k==10)
//break;
}
}while(done);

best regards

Recommended Answers

All 28 Replies

th=spoofpkt/totalpkt;

because th is an int, and assuming spoofpkt < totalpkt, th will always be zero (integer division is always rounded down to an integer value). You could make th a float to hold fractional values.

th=spoofpkt/totalpkt;

because th is an int, and assuming spoofpkt < totalpkt, th will always be zero (integer division is always rounded down to an integer value). You could make th a float to hold fractional values.

thanks for that note
although the th is become 1 when i enter some thing else than tcp the o/p is stick in the alert state
if u can try it is't o/p is either stick in alert or start in drop the stick in the alert how can i do that switching write?

thanks in advance
best regards

How about: sw1=true;sw2=false;
Those values only get changed at lines 59/60 if th>T1, so maybe that's never true (see previous post).
ps It's really hard to read complex if logic when the indentation is wrong. Get yourself a code editor that indents for you.

is that code work for u?
when i use the while statement instade of if statment is it more correct?
thanks

th=spoofpkt/totalpkt;

because th is an int, and assuming spoofpkt < totalpkt, th will always be zero (integer division is always rounded down to an integer value). You could make th a float to hold fractional values.

sorry now the th is still 0.0 even when i define it as float, so please why that?
thanks in advance

Add some debug code to print out the values of all the variables used in the expression and post them so we can see what the values going into the expression are.
Copy and paste the console output here.

hello, i cast the one of the tw int as follows

th=(float)spoofpkt/totalpkt;

and the th is now true


now the problem is the o/p is like this

Enter your packet that contain a valid IP Add with the field of the TTL and the connection type

ip is
10.0.0.2
ttl is
23
conn is
tcp
T0 is 32
Hc is 9
ip in long is 167772162
totalpkt is 1
k is 1
ligtmate
spoof is false spoofpkt is 0
th is 0.0
alert state
pkt accepted

Enter your packet that contain a valid IP Add with the field of the TTL and the connection type

ip is
10.0.0.2
ttl is
54
conn is
tcp
T0 is 64
Hc is 10
ip in long is 167772162
totalpkt is 2
k is 2
spoof is true spoofpkt is 1
th is 0.5
action state
pkt is droped

Enter your packet that contain a valid IP Add with the field of the TTL and the connection type

ip is
10.0.0.2
ttl is
43
conn is
tcp
T0 is 64
Hc is 21
ip in long is 167772162
totalpkt is 3
k is 3
ligtmate
spoof is true spoofpkt is 1
th is 0.33333334

Enter your packet that contain a valid IP Add with the field of the TTL and the connection type

ip is
10.0.0.2
ttl is
43
conn is
tcp
T0 is 64
Hc is 21
ip in long is 167772162
totalpkt is 4
k is 4
spoof is true spoofpkt is 2
th is 0.5

it doesn't say any thing in the third time

thank u in advance for any help

Please explain where the third line is and why it is incorrect?
Edit the output and put some comments in it showing where the error is and what the output should be.

thank u for reply
i meam that the o/p in


Enter your packet that contain a valid IP Add with the field of the TTL and the connection type

ip is
10.0.0.2
ttl is
43
conn is
tcp
T0 is 64
Hc is 21
ip in long is 167772162
totalpkt is 4
k is 4
spoof is true spoofpkt is 2
th is 0.5 //here i want the o/p say where are i am now (in which state and what is happen)

that is what i have problem with
is that what u want please, if any thing not clear please tell me
thanks in advance

//here i want the o/p say where are i am now (in which state and what is happen)

What would correct output look like?
Where in your code is that output supposed to be generated?
What variables control the output? What values do they have when the code executes incorrectly?

What would correct output look like?
Where in your code is that output supposed to be generated?
What variables control the output? What values do they have when the code executes incorrectly?

ok, the th value is control the swithing
when it is less than T1 it must switch to second state, that is the code i consern with:

if (sw2){
System.out.println("action state");
if (spoof && th>=T1)
System.out.println("pkt is droped");
else if (!spoof && th>=T1)
System.out.println("pkt in action is forword");
else if(!spoof && th<T2)
{System.out.println("back to alert");
sw1=true;sw2=false;
}

the o/p must be in the action state and the pkt is droped


best regards
thank u in advance

What variables control the output? What values do they have when the code executes incorrectly?

Use printlns to show the values of the variables that control the output.

What about these two cases?
(spoof && th<T1)
(!spoof && !(th>=T1 || th<T2))
in both these cases you do nothing

What about these two cases?
(spoof && th<T1)
(!spoof && !(th>=T1 || th<T2))
in both these cases you do nothing

i adjust the T1 to be 0.5 and T2 to be 0.3
so these values can used to switch from state to another
is that what u want please?

regards

I don't understand what your application is supposed to do or what values different variables should have. That is up to you.

I'm trying to point out ways for YOU to debug your code so it does what you want it to do.
One way to debug code is by using println statements to show the values of variables.
You know what the values of the variables should be at different points in the program.
If you print them out, you will see if the code is computing them correctly or if there is a problem.

I personally don't "want" anything. I was just pointing out that there are two cases where your if tests don't print anything out. Since you were looking for an explanation of why nothing was printed for some data, I thought this could be relevant.

I personally don't "want" anything. I was just pointing out that there are two cases where your if tests don't print anything out. Since you were looking for an explanation of why nothing was printed for some data, I thought this could be relevant.

sorry i misunderstand u
what do u mean by the 2 latest posts?
english is not my mother language

do u mean that i must test those or remove it?

regards

I mean
if (spoof && th<T1) then all your if tests are false and you will do nothing
similarly,
if (!spoof && !(th>=T1 || th<T2)) then all your if tests are false and you will do nothing

Maybe your 3rd line, where nothing was printed, is because one or the other of these two things happened. You can check that by looking at spoof, th, T1 and T2 for that line.
If that is the problem, then you could add these cases to your set of if tests, or you could have a final else clause that prints "none of the above", so you know what's happening.

If I'm using English words you don't know, please ask. I'm English but I live in France, so I know how hard it can be to work in a foreign language.

thank u very much for ur kind and ur reply
so now i understand what u mean and i will try it and reply if any


best regards

hello again
i now arrenge it like this and i think it work right,but i have compilation error

if(th<T1 && sw1=true)
{
	System.out.println("\n alert state");
	System.out.println("\n pkt accepted");
}
if (th>=T1 || sw1=false)
{
	System.out.println("action state");
	sw1=false;
	if(spoof & th>T2)
	System.out.println("pkt droped");
	else if (!spoof)
	{
	
System.out.println("pkt is forword");
if(th<T2)
{
	System.out.println("back to alert");
	sw1=true;
}
}
}

but 2 error occur
that said && and || are unexpected type

so why that please

When you get errors, please copy the FULL text of the error message and paste it here.
Here is a sample:

TestSorts.java:138: cannot find symbol
symbol  : variable var
location: class TestSorts
         var = 2;
         ^

thank u for reply
here is the message

--------------------Configuration: mew tring - JDK version 1.6.0_24 <Default> - <Default>--------------------
C:\Users\Sarah\Documents\JCreator 4\mew tring\IP2HC_Inspect.java:84: unexpected type
required: variable
found : value
if(th<T1 && sw1=true)
^
C:\Users\Sarah\Documents\JCreator 4\mew tring\IP2HC_Inspect.java:90: unexpected type
required: variable
found : value
if (th>=T1 || sw1=false)
^
2 errors

Process completed.

regards

mr.JamesCherrill and mr.NormR1
thaaaaaaaaaank u very much
it must be
if (th<T1 && sw1==true)
and not
if (th<T1 && sw1=true)

my program now work and like i need an happy
so thank u very much for all help and advice and the time that u spend to help me

best regards

it must be if (th<T1 && sw1==true)

Actually, it should be

if (th<T1 && sw1)

IMHO I find that "boolean==true" or "boolean==false" completely assinine. Some say it helps "readability". BS, complete BS. If any programmer cannot read "boolean" or "!boolean" there's a problem, and not with the code. If you don't agree with this, that's your prerogative, but this is just a little pet peeve of mine.

My favourite is this one which crops up about once a month here...

if (booeanExp == true ) {
  myBoolean = true;
} else {
  myBoolean = false;
}

Actually, it should be

if (th<T1 && sw1)

IMHO I find that "boolean==true" or "boolean==false" completely assinine. Some say it helps "readability". BS, complete BS. If any programmer cannot read "boolean" or "!boolean" there's a problem, and not with the code. If you don't agree with this, that's your prerogative, but this is just a little pet peeve of mine.

i am so sorry i could't understand all that, there is some words that does't in the dictionary, and the meaning is not reach to me as u want (i think)
any way thank u very much for ur contribution
regards

i am so sorry i could't understand all that, there is some words that does't in the dictionary, and the meaning is not reach to me as u want (i think)
any way thank u very much for ur contribution
regards

Don't worry - m. was just saying it in a humorous/funny way. His point was:
if you have a boolean variable or expression b then b==true is not good code. It's the same as just b, but longer. Here's the truth table:

b         b==true
-         --------
true      true
false     false

Don't worry - m. was just saying it in a humorous/funny way. His point was:
if you have a boolean variable or expression b then b==true is not good code. It's the same as just b, but longer. Here's the truth table:

b         b==true
-         --------
true      true
false     false

thank u very much for ur attention
so much thanks
best regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.