Ping Mechnism

PING

Everybody knows that the ping utility is used to check network connectivity between two hosts, but what happens when a user issues a ping? This article is designed to explain the basics of what happens on a network when a ping is issued. Imagine the following scenario;

You have PC-A in subnet 192.168.1.0/24, PC-B in subnet 192.168.2.0/24 and a router connected to both subnets. You need to check if PC-A can connect to PC-B.


For the purposes of this article we will use the following IP addresses and MAC addresses;

PC-A 
192.168.1.10 MAC Address 00:00:00:00:00:10
Default Gateway 192.168.1.15

Router Interface E0 
192.168.1.15 MAC Address 00:00:00:00:00:15

Router Interface E1 
192.168.2.20 MAC Address 00:00:00:00:00:20

PC-B 
192.168.2.25 MAC Address 00:00:00:00:00:25
Default Gateway 192.168.2.20

A user on PC-A types in “ping 192.168.2.25”

The first thing to happen is that ICMP (Internet Control Management Protocol) creates data. This is just the alphabet. IP (Internet Protocol) on PC-A creates a packet containing the Destination IP Address (192.168.2.25), the Source IP Address (192.168.1.10), the data, and a protocol field. The protocol field informs the receiving host where to pass the data to, in this example the protocol field would be set to 0x1h to indicate ICMP. (0x indicates that the following is an hexadecimal number)

Once the packet has been created ARP (Address Resolution Protocol) is then used to identify the MAC (Media Access Control / Hardware address / Burned Address) address of the destination host. This can happen in a number of ways, the first to happen is that ARP checks it’s cache to see if it has a match to the Destination IP Address. If not then ARP sends out an ARP broadcast to the Ethernet MAC broadcast address (FF:FF:FF:FF:FF:FF)

“Who has 192.168.2.25? Please tell 000000000010”

You will notice that PC-A is asking for replies to be sent to the MAC address. This is because computers communicate only with MAC addresses on LANs (Local Area Networks)

If no response is received by PC-A, then ARP & IP assume that 192.168.2.25 is on a remote subnet and therefore would require routing. At this point the IP address and the MAC address of the default gateway is required. In a Windows machine the registry is consulted in order to get the IP address of the default gateway (192.168.1.15). ARP then consults it’s cache to see if it has match to the IP address of the default gateway, if not then another ARP broadcast is sent

“Who has 192.168.1.15? Please tell 000000000010”

Because this is a broadcast ALL hosts on the 192.168.1.0/24 subnet will receive this frame. The router interface E0 will read the frame and identify itself as the interface with the requested IP address. The router will then reply;

“I have 192.168.1.15. MAC address is 000000000015”

As the request asked for a reply direct to PC-A the frame sent from the router will be directed towards PC-A and not sent as a broadcast. The router will also cache the MAC address of PC-A, which it received via the broadcast sent by ARP to locate the MAC address of the router.

Once IP at PC-A as received the message from the router interface it will pass the packet created earlier and the MAC Destination address down to the Data Link Layer. 

The Data Link Layer creates a frame containing the Destination MAC address, the Source MAC address, A FCS (Frame Check Sequence, used to verify the data has not been corrupted) and an Ether_Type field, in this example the field will be set to 0x8 to indicate IP. This Frame encapsulates the packet passed down from IP at the Network Layer. The MAC address of the router is also cached into the ARP cache on PC-A

Once the frame has been created it is passed down to the Physical Layer where the frame is placed onto the wire one bit at a time. Every host on subnet 192.168.1.0/24 will receive this frame, build it, and check the Destination MAC address, if it is not a match the frame is discarded. At the router interface, E0, the Destination MAC address is a match. The router then checks the Ether_Type field (0x8 = IP) pulls the packet from the frame, discards the frame and passes the packet up to IP at the Network Layer. 

At the Network Layer the Destination IP address is checked to see if it is a match, in this example the Destination IP address is 192.168.2.25, however the IP address of the router interface which received the frame is 192.168.1.15, and is not a match. The router then consults it’s routing table for the destination IP network address (192.168.2.0). If there is no match in the routing table the packet is discarded and a “Destination Network unavailable” message is returned to PC-A

If there is a match in the routing table then the router will switch the packet to the interface configured to send information to the destination IP Network Address, in this example E1.

Interface E1 now needs to know the MAC address of the machine with IP address 192.168.2.25. The first thing it does is check the ARP cache, no match in the cache, E1 then send out an ARP broadcast.

“Who has 192.168.2.25? Please tell 000000000020”

Because PC-B is on the same subnet as E1, PC-B responds

“I have 192.168.2.25. MAC address is 0000000025”

IP at Interface E1, on the router, then passes the packet (created at PC-A) and the Destination MAC address for 192.168.2.25 down to the Data Link Layer. The Data Link Layer then creates a frame containing the Destination MAC address, Source MAC address, FCS and an Ether_Type field (again set to 0x1h to indicate IP), which encapsulates the IP packet. 
(The MAC address of PC-B is placed into the ARP cache on Interface E1, and the MAC address of interface E1 is placed into the cache of PC-B)

The frame is then passed down to the Physical Layer to be placed on the wire one bit at a time. Again all hosts on the 192.168.2.0/24 subnet will receive the frame, build it, check it, discard it with the exception of PC-B which will match the Destination MAC address. PC-B will then check the Ether_Type field, pull the packet from the frame, discard the frame and pass the packet to the protocol indicated in the Ether_Type field, in this example IP. 

IP then checks the Destination IP address in the packet and finds a match. It will then check the Protocol field (0x1h = ICMP) and pass the data to ICMP. ICMP recognises that the data sent is an echo request, and will then create an echo response message.

The echo response is then passed to IP, which will then build a packet, consisting of the Destination IP address (192.168.1.10), the Source IP address (192.168.2.25) the data from ICMP, and the protocol field. Once the packet is built the MAC address of the IP address 192.168.1.10 is required. ARP checks it’s cache, if there is no match an ARP broadcast is sent.

“Who has 192.168.1.10? Please tell 000000000025”

Because 192.168.1.10 is on a remote subnet, and routers do not pass broadcasts there is no response. 

The default gateway is then required for PC-B. The default gateway is configured as 192.168.2.20 and the ARP cache is checked. As PC-B cached the MAC address of interface E1, a match is found and there is no need to send out an ARP broadcast.

Now that the MAC address of the default gateway has been resolved the packet and the Destination MAC address is then passed down to the Data Link Layer.

At the Data Link Layer a frame is built which consists of the Destination MAC address, the Source MAC address, the FCS and the Ether_Type field (again set to 0x8 to indicate IP). The frame encapsulates the packet passed down from IP. The complete frame is then passed down to the Physical Layer to be put onto the wire one bit at a time.

At Interface E1 of the router, the frame is received, the Destination MAC address is then checked and found to be a match. The Ether_Type field is then checked, the packed is pulled from the frame, the frame is discarded and the packed passed to IP, as indicated in the Ether_Type field.

IP on E1 checks the IP destination address and finds it is not a match. It then consults the routing table for the IP Network Address (192.168.1.0/24), if a match is found the packet is switched to the Interface configured for the 192.168.1.0/24 network, in this example E0.

If no match is found then the packet is discarded. PC-A will receive a time-out error in this case, as the time set to receive replies has been exceeded. A destination network unavailable message is NOT sent to PC-A. If the message could be sent to PC-A then the router would obviously have a route to PC-A’s network and then would not need to generate the message!!

On Interface E0, the interface configured for 192.168.1.0/24, IP and ARP will then locate the MAC address for the IP address 192.168.1.10. ARP checks the cache, because the MAC address for PC-A was cached on the outgoing trip, there is a match and the packet and frame are then passed down to the Data Link Layer. 

The Data Link Layer will then build a frame, consisting of the Destination MAC address, the Source MAC address, Ether_Type field and the FCS. This frame encapsulates the packet passed down from IP and then passes the frame down to the Physical Layer to be placed onto the wire, one bit at a time.

PC-A receives the frame sent from interface E0 on the router, checks the MAC address, finds a match, reads the Ether_Type field, pulls the packet from the frame, discards the frame and passes the packet to IP as indicated in the Ether_Type field. IP checks the Destination IP address and finds a match. IP will then read the Protocol field (0x1h = ICMP) and passes the data to ICMP.

ICMP recognises the data as an echo response, ICMP acknowledges receipt by sending information to the user interface, (“!” with Cisco routers, “Reply from 192.168.2.25…….” and additional information in Windows), and then builds another echo request and the whole process begins again.

The above is designed to give an overview of what happens on the network when data is sent from one machine to another. This is by no way to be considered complete as there are additional parameters which can be configured and created both within the IP packet and the Data-Link Frame. The above assumes the use of Ethernet_II frames on the network. No matter how big the network or how many routers the data passes through the process is identical to the above.

“variable_node”

One More Logic “Reverse String”

Hey Guys i am back with some coding problem i have develop a solution

question is
given a String like
i am arun kumar gupta and how are you guys

we want the reverse of String
like
syug uoy era woh dna atpug ramuk nura ma i

so i am giving code of the Algo

import java.util.*;
import java.lang.*;
public class ReverseString
{
public static void main (String [] args )
{
Scanner sc  = new Scanner(System.in);
String input = sc.nextLine();
int len = input.length();
//System.out.println(len);
//char inputc[] = new char[len];
char[] cArray = new char[len+1];
 cArray = input.toCharArray();
/*for(int i = 0 ; i
{
System.out.print(cArray[i]);
}*/
int start = 0, end = 0 ;
char temp = cArray[0];
reverse(cArray , len-1 ,start);
System.out.println(“\n”);
/*for(int i = 0 ; i
{
System.out.print(cArray[i]);
}*/
System.out.println(“\n”);
cArray[len-1] = ‘ ‘;

while(true)
{
if(cArray[end] != ‘ ‘)
{
++end;
}

if((cArray[end] == ‘ ‘ ) /*|| (end == len+1)*/)
{
//System.out.println(end);
reverse(cArray , end-1 ,start);
start = end+1;
end = end+1;
//System.out.println(end);
if(end  == len)
break;
}

}
cArray[len-1] = temp;
for(int i = 0 ; i
{
System.out.print(cArray[i]);
}
//System.out.print(temp);
System.out.println(“\n”);


}
static void reverse(char cArray[] , int end , int start)
{
//int end = len;
//int start = start ;
char temp = ‘a’;
while((end > start)||(end == start))
{
temp = cArray[start];
cArray[start] = cArray[end];
cArray[end] = temp;
–end;
++start;

}

}
}

Solution of Taddy and Tracy Competition

http://arungupta1.blogspot.in/2012/07/tracy-and-taddy-competition-algo.html

I have Written this code by myself so please go through this ……

import java.util.*;
import java.lang.*;
import java.io.*;
public class AStringGame {




public static void main (String [] args)
{
HashMap hm = new HashMap(); 
String turn = new String(“Taddy”);
Scanner sc  = new Scanner(System.in);
int test_case = sc.nextInt();
Set set = hm.entrySet(); 
//bool last = true;
Iterator ik = set.iterator(); 
int who = 0; // 0 teddy  1  1 tracy
int left = 0;
for(int i = 0 ; i
{
//String arr = new String(“array”+i+”string”);
//System.out.println(“\n”+arr);
//int ” arr”[] = new int[20];
String input = sc.next();
int len =input.length();

char[] iArray = input.toCharArray();
int inputs = sc.nextInt();
for(int j = 0 ; j< inputs ; ++j)
{
String arr = new String(“array”+i+”:”+j+”string”);
//System.out.println(“\n”+arr);
hm.put(arr,new String( sc.next())); 
//System.out.println(hm.get(arr));
String in =  (String) hm.get(arr);
int lenn = in.length();
//System.out.println(“:::::::”+lenn);
who = 0 ;
int t = 0;
int ine = 0;
char[] aArray = in.toCharArray();
int start = 0 , end = 0;
for(int k = 0 ; k
{
if(iArray[k] == aArray[0])
{

int counter = k;
start = k;


while(t < lenn)
{
if(iArray[counter] == aArray[t])
{

//System.out.println(“startinput:”+counter+”\t:check string “+t   );
who = 1;
end = counter;
if((lenn -1) ==(t) )
{
//System.out.println(“Hey i am also here in if  “);
//System.out.println(“\nSome 1 Won”);
t =9898;
left = inputs -1;
//System.out.println(“Left *********************:”+left);
if(ine == 0)
{
turn = “Tracy”;
ine =  1;
}else
{
turn = “Taddy”;
ine = 0;
}
for(int gg = start ; gg <= end ;++gg)
{
iArray[gg] = ‘\0’;
}

break; 

//System.out.println(“Hey i am also here in if  “); 
}
//System.out.printf(“\nInside\n”);
++counter;
++t;
}
else if(t< lenn)
{
//System.out.println(“\nI am also here \n”);
//System.out.println(“\n Some one is going to looseclea”);
t =0;
counter = 0;
who = 0;
//left = left +1;
break;

if(t == (len-1) )
{   break;   }
}

}
if(t == (len-1) )
{   break;   }


}
if(who == 0)
{
int oo = 0;
//System.out.println(“Success Success”);

}


}
/*while(ik.hasNext()) { 
//Map.Entry me = (Map.Entry)ik.next(); 
System.out.print(hm.getKey() + “: “); 
System.out.println(hm.getValue()); 

} */




/*if ((left%2) == 0)
{
System.out.println(“Tracy”);
}
else 
System.out.println(“Taddy”);
*/
System.out.println(“\n”+turn);

}
}
}

Tracy and Taddy Competition (Algo Problem)

Teddy and Tracy like to play a game based on strings. The game is as follows. Initially, Tracy writes a long random string on a whiteboard. Then, each player starting with Teddy makes turn alternately. Each turn, the player must erase a contiguous substring that exists in the dictionary. The dictionary consists of N words.
Of course, the player that can’t erase any substring in his turn loses the game, and the other player is declared the winner.
Note that after a substring R is erased, the remaining substring becomes separated, i.e. they cannot erase a word that occurs partially to the left of R and partially to the right of R.
Determine the winner of the game, assuming that both players play optimally.

Input

The first line contains a single integer T, the number of test cases. T test cases follow. The first line of each testcase contains a string S, the string Tracy writes on the whiteboard. The next line contains a single integer NN lines follow. The i-th line contains a single string wi, the i-th word in the dictionary.

Output

For each test case, output a single line containing the name of the winner of the game.

Example

Input:
3
codechef
2
code
chef
foo
1
bar
mississippi
4
ssissi
mippi
mi
ppi

Output:
Tracy
Tracy
Teddy

Constraints

  • 1 <= T <= 5
  • 1 <= N <= 30
  • 1 <= |S| <= 30
  • 1 <= |wi| <= 30
  • S and wi contain only characters ‘a’-‘z’

Algorithm Problem

Gift to his Child understand the code if you guys have any problem please let me know .

Our Chef is very happy that his son was selected for training in one of the finest culinary schools of the world. So he and his wife decide to buy a gift for the kid as a token of appreciation. Unfortunately, the Chef hasn’t been doing good business lately, and is in no mood on splurging money. On the other hand, the boy’s mother wants to buy something big and expensive. To settle the matter like reasonable parents, they play a game.
They spend the whole day thinking of various gifts and write them down in a huge matrix. Each cell of the matrix contains the gift’s cost. Then they decide that the mother will choose a row number r while the father will choose a column number c, the item from the corresponding cell will be gifted to the kid in a couple of days.
The boy observes all of this secretly. He is smart enough to understand that his parents will ultimately choose a gift whose cost is smallest in its row, but largest in its column. If no such gift exists, then our little chef has no option but to keep guessing. As the matrix is huge, he turns to you for help.
He knows that sometimes the gift is not determined uniquely even if a gift exists whose cost is smallest in its row, but largest in its column. However, since the boy is so smart, he realizes that the gift’s cost is determined uniquely. Your task is to tell him the gift’s cost which is smallest in its row, but largest in its column, or to tell him no such gift exists.

Input

First line contains two integers R and C, the number of rows and columns in the matrix respectively. Then follow R lines, each containing C space separated integers – the costs of different gifts.

Output

Print a single integer – a value in the matrix that is smallest in its row but highest in its column. If no such value exists, then print “GUESS” (without quotes of course)

Constraints

1 <= R, C <= 100
All gift costs are positive and less than 100000000 (10^8)

Example 1

Input:
2 3
9 8 8
2 6 11

Output:
8

Example 2

Input:
3 3
9 8 11
2 6 34
5 9 11

Output:
GUESS

Example 3

Input:
2 2
10 10
10 10

Output:
10

Hi Friends there is a problem …..

a mouse is at one end of the table say (0,0) and table is divided in to the grids ,some blocks are blocked
so it does not know . when he is mowing then it came to know that way is blocked …..
find  a algo and write the code to send the mouse to (n,n) position means at last.

below is the code i made by me only ‘

Arun Kumar Gupta     cc

import java.util.*;

public class Mouse
{
public static void main (String [] args)
{
//public int backtrack(int r , int t) ;
Scanner sc = new Scanner(System.in);
//System.out.println(“Please Enter the Value for Square Array  :”);
//int table = sc.nextInt();
//System.out.println(“Array is of” +table+”*”+table);
int table = 5;
int row = 7 , colum = 7;
int table1 [] []  = new int[table+2][table+2];
int i =0 , j =0;
for( i = 0 ; i< (row) ; ++i)
{

for( j = 0 ; j< (colum) ; ++j)
{
//System.out.print(“i am here”);
if((i == 0) || (i == (row -1)   ))
{
//System.out.print(“i am here”);
table1[i][j] = 0;
}
else if((j == 0) || (j == (colum -1)   ))
{
table1[i][j] = 0;
}

else {
table1[i][j] = 1;
}

}

}
for( i = 0 ; i< (row) ; ++i)
{

for( j = 0 ; j< (colum) ; ++j)
{
System.out.print(“\t”+table1[i][j]);
}
System.out.println(“\n”);

}
System.out.println(“\n”);
System.out.println(“\n”);
table1[1][2] = 0;
table1[2][2] = 0;
table1[4][0] = 0;
table1[4][1] = 0;
table1[3][4] = 0;
table1[4][4] = 0;
table1[5][4] = 0;
for( i = 0 ; i< (row) ; ++i)
{

for( j = 0 ; j< (colum) ; ++j)
{
System.out.print(“\t”+table1[i][j]);
}
System.out.println(“\n”);

}
i = 1;
j = 1;
int dx = 1;
int dy = 1;
int pdx = 1 ,pdy = 1;
for(i =1 ; i <= (row *colum) ; ++i)
{
if((dx < row)&& (dy < row))
{
if((dx == (row -2)) && (dy == (colum -2)))
{
System.out.print(“\n\n\n\n\nFinally Mouse At the End\n\n\n “);
for( i = 0 ; i< (row) ; ++i)
{

for( j = 0 ; j< (colum) ; ++j)
{
System.out.print(“\t”+table1[i][j]);
}
System.out.println(“\n”);

}
System.exit(0);
}
if( table1[dx][dy+1] == 0)
{
System.out.print(“i am here ” +dx+ “:” +dy+ “\t0\n”);
table1[dx][dy] = 9;
dx = dx +1;
if(dx == (row-1))
{
dx = dx-1;

}

}
if((dx == (row -2)) && (dy == (colum -2)))
{
System.out.print(“\n\n\n\n\nFinally Mouse At the End\n\n\n “);
for( i = 0 ; i< (row) ; ++i)
{

for( j = 0 ; j< (colum) ; ++j)
{
System.out.print(“\t”+table1[i][j]);
}
System.out.println(“\n”);

}
System.exit(0);
}
if (table1[dx][dy+1] == 1)
{
System.out.print(“i am here ” +dx+ “:” +dy+ “\t1\n” );
table1[dx][dy] = 9;
dy = dy +1;
continue ;
}
else if ((table1[dx][dy+1] == 0 ) && (table1[dx+1][dy] == 0))
{
System.out.print(“i am here goint to call Back track with “+dx+ “:”+dy+”:”);
int roww = backtrack(dx ,dy , table1 , row , colum);
dx = roww;
continue;

}

}
}

}
public static int backtrack(int r , int t , int[][] arr , int row , int colum )
{
System.out.print(“I am at back tecrk \n”);
for( int u = 0 ; u<(row *colum) ; ++u)
{
if(arr[r-1][t +1] == 0)
{
arr[r][t] = 1;
r =r-1;
System.out.print(“i am here Back track ” +r+ “:” +t+ “\t1\n” );
continue;

}
else if(arr[r-1][t +1] == 1)
{
System.out.print(“i am here Back track ” +r+ “:” +t+ “\t1\n” );
r = r -1;

break ;
}
}
return r;
}

}

Parenthesis Checking Code

Hi Friends …. happy to see again
i made a code of  Parenthesis Checking it’s order is of the order is only O(n)
see this code and enjoy really …….     cc Arun Kumar Gupta Only.

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

public class Stack
 {
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Entere Combination”);
String Input = sc.next();

System.out.println(“Entered Values \t” + Input);
int len =Input.length();
char[] iArray = Input.toCharArray();
String cArray[] = new String[len];
for(int j = 0 ; j < len ; j++)
{

cArray[j] =Character.toString(iArray[j]) ;
}
int l =0, m=0 ,u =0;
int ru = 0, rm= 0, rl = 0 ;
String upper1 = “[” ;
String middle1 = “{” ;
String lower1 = “(” ;
String upper2 = “]” ;
String middle2 = “}” ;
String lower2 = “)” ;
for(int j = 0 ; j < len ; j++)
{
System.out.print(“\t”+cArray[j]);
}
System.out.print(“\n”);
for(int j = 0 ; j < len ; j++)
{
//System.out.print(“\t”+cArray[j]);
if(cArray[j].equals(upper1))
{
//System.out.print(“i am here [“);
++u;
ru = 1;
rm= 0;
rl =0;
}
if(cArray[j].equals(middle1))
{
++m;
rm =1;
ru = 0;
rl = 0;

}
if(cArray[j].equals(lower1))
{
++l;
rl =1;
ru = 0;
rm = 0;
}
if(cArray[j].equals(upper2))
{
u = u-1;
if((rm == 0) && (rl == 0))
{
ru= 0;
}
}
if(cArray[j].equals(middle2))
{
m = m-1;
if((ru == 0) && (rl == 0))
{
rl = 0;
}
}
if(cArray[j].equals(lower2))
{
l = l-1;
if((rm == 0) && (ru == 0))
{
rl = 0;
}
}



}
System.out.println(” Count Upper   :” +u+”\n” );
System.out.println(” Count Middle   :” +m+ “\n”);
System.out.println(” Count Lower   :” +l+ “\n”);
if((u == 0)&&(m ==0)&&(l ==0))
{
System.out.println(“Everything is ok !!!! No error”);
}
else
System.out.println(“Some Error is there “);

}

}