tag:blogger.com,1999:blog-69799072016-04-07T06:26:13.731-07:00Nitin's NetTo be paradise for programmersNitin Vermahttp://www.blogger.com/profile/10431708447076575171noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-6979907.post-44028972925577614712008-12-07T07:27:00.000-08:002008-12-07T07:27:28.596-08:00Googolbyte: What is a Googolbyte?<a href="http://googolbyte.blogspot.com/2005/04/what-is-googolbyte.html">Googolbyte: What is a Googolbyte?</a>Nitin Vermahttp://www.blogger.com/profile/10431708447076575171noreply@blogger.com1tag:blogger.com,1999:blog-6979907.post-22750347313592352782007-05-13T04:20:00.001-07:002007-06-03T04:57:43.243-07:00Transcript of me solving - "The Tower of Hanoi".I felt like solving a problem I had never solved before, I took up "The Tower of Hanoi". It is not that I had never heard or seen this problem before but I had never tried to solve it myself or even looked at solution. I was sitting at office with my friend Krishnaprasad Shivdasan, whom we normally refer as KP. He gave me a book written by Ronald L. Graham, Donald E. Knuth & Oren Patashnik, called Concrete Mathematics. The first chapter in the book is on "Recurrent Problems", with first reference to "The Tower of Hanoi".<br /><br />Problem statement as in the book:<br /><br /><code><br />Let's look first at a neat little puzzle called the Tower of Hanoi, invented by the French mathematician Edouard Lucas in 1883. We are given a tower of eight disks, initially stacked in decreasing size on one of three pegs:<br /><br />The objective is to transfer the entire tower to one of the other pegs, moving only one disk at a time and never moving a larger one onto a smaller.<br /></code><br /><br />Just reading this and the next paragraph and I stared with solving the problem, with a pen and a paper. The next paragraph that I read is also interesting.<br /><br /><code><br />Lucas furnished his toy with a romantic legend about a much larger Gold -wow. Tower of Brahma, which supposedly has 64 disks of pure gold resting on three diamond needles. At the beginning of time, he said, God placed these golden disks on the first needle and ordained that a group of priests should transfer them to the third, according to the rules above. The priests reportedly work day and night at their task. When they finish, the Tower will crumble and the world will end.<br /></code> <br /><br />"The Tower of Brahma", Brahma is an Indian God. Now it interests me even more.<br /><br />Trial runs now:<br /><br />To make it easy myself to understand, I stared with small numbers of disks.<br /><br />Let,<br />n be number of disks<br />m be number of moves -- this is what we’ll record.<br /><br />(n=0)<br />Ha ha ha, without doing anything was able to say it would take no moves.<br />Thus, (m=0)<br /><br />(n=1)<br />This just takes one move. Move the disk from first to last. To record my moves I would just name the pegs as A, the starting peg, B the swap peg, C the target peg.<br /><br />1:AC<br /><br /> Thus, (m=1)<br /><br />(n=2)<br />This is also went easy, move smallest disk to peg B, then move other disk to Peg C and then place the smallest disk over the other disk. To make it easy for us to record let us name smallest disk as 1 and other in the increasing sequence. <br /><pre><br />1:AB-C<br />2:A-C-<br /><br />(“-“means no move for this disk)<br /></pre><br />Thus, (m=3)<br /><br />(n=3)<br />This also goes fine.<br /><pre><br />1:AC-B-A-C<br />2:A-B---C-<br />3:A---C---<br /></pre><br />Thus, (m=7)<br /><br />Now, I saw something interesting, there are two patterns in with these disks are moving, ABCA and ACBA. Let us see if this holds well with n=4.<br /><br />(n=4)<br /><pre><br />1:AB-C-A-B-C-A-B-C<br />2:A-C---B---A---C-<br />3:A---B-------C---<br />4:A-------C-------<br /></pre><br />Yes, moving pattern still holds good.<br />(m=15)<br /><br />Getting the above solution took me some good time. I failed to get it in the first run. It was going too astray, so I stared again. Anyway I managed to get the solution in 15 moves eventually. This also has the same pattern going on. Now there is something else is quite evident, biggest disk always moves A to C and it only gets to move once.<br /><br />By now I have one thing clear in my mind, for first time move (n-1)th heap on B or to take it a step ahead move (n-2)th heap on C, and so on.<br /><br />Now, let us try n=5.<br /><br />(n=5)<br /><pre><br />1: AC-B-A-C-B-A-C-B-A-C-B-A-C-B-A-C<br />2: A-B---C---A---B---C---A---B---C-<br />3: A---C-------B-------A-------C---<br />4: A-------B---------------C-------<br />5: A---------------C---------------<br /></pre><br />(m=31)<br /><br />This also follows our pattern ABCA and ACBA. By now I can see another interesting sequence for number of moves per disk.<br />For,<br />(n=5)<br />Number of moves 5th disk are 1<br />4: 2<br />3: 4<br />2: 8<br />1: 16<br /><br />(n=4)<br />4: 1<br />3: 2<br />2: 4<br />1: 8<br /><br />(n=3)<br />3: 1<br />2: 2<br />1: 4<br /><br />(n=2)<br />2: 1<br />1: 2<br /><br />(n=1)<br />1: 1<br /><br />And this should and does sum up to 31, 15, 7, 3, and 1 respectively. Now this is interesting these numbers are from a series 2^0, 2^1, 2^2, 2^3 and 2^4. And most of us know Sum of “X^Y” for Y=0 to n-1 is “(X^n) – 1”.<br /><br />Thus number of moves need to move an “n” disk heap from A to C are “(2^n) -1”. That’s “2^0 - 1 = 0”, “2^1 - 1 = 1”, “2^2 - 1 = 3”, “2^3 - 1 = 7”, “2^4 - 1 = 15”and “2^5 - 1 = 31”.<br /><br />Now I am In search of an algorithm that can be applied to any number of disks.<br />Let us try moving 4 disks, that is:<br /><br />(A: 4321, B: -, C: -)<br />Move 4321 heap on C, from A.<br />Means,<br />Move 321 heap on B, from A<br />Move 4 to C, from A.<br />Move 321 heap on C, from B, over 4.<br />(A: -, B: -, C: 4321)<br /><br />(A: 4321, B: -, C: -)<br />Move 321 heap on B, from A<br />Means,<br />Move 21 heap on C, from A.<br />Move 3 to B, from A.<br />Move 21 heap on B, from C. over 3.<br />(A: 4, B: 321, C: -)<br /><br />(A: -, B: 321, C: 4)<br />Move 321 heap on C, from B, over 4.<br />Means,<br />Move 21 heap on A, from B.<br />Move 3 to C, from B, over 4.<br />Move 21 heap on C, from A, over 34.<br />(A: -, B: -, C: 4321)<br /><br />(A: 4321, B: -, C: -)<br />Move 21 heap on C, from A.<br />Means,<br />Move 1 to B, from A.<br />Move 2 to C, from A.<br />Move 1 to C, over 2, from B.<br />(A: 43, B: -, C: 21)<br /><br />(A: 4, B: 3, C: 21)<br />Move 21 heap on B, from C. over 3.<br />Means,<br />Move 1 to A, from C, over 4.<br />Move 2 to B, from C, over 3, <br />Move 1 to B, from A, over 23<br />(A: 4, B: 321, C: -)<br /><br />(A: -, B: 321, C: 4)<br />Move 21 heap on A, from B.<br />Means,<br />Move 1 to C, from B, over 4.<br />Move 2 to A, from B.<br />Move 1 to A, from C, over 2.<br />(A: 21, B: 3, C: 4)<br /><br />(A: 21, B: -, C: 43)<br />Move 21 heap on C, from A, over 34.<br />Means,<br />Move 1 to B, from A.<br />Move 2 to C, from A, over 34.<br />Move 1 to C, from B, over 234.<br />(A: -, B: -, C: 4321)<br /><br />So, we can see something in this move pattern that is we have 3 classifications of the pegs. Namely, resting peg, swap peg, and target peg let us denote those as R(n), S(n), and T(n) respectively for nth disk.<br /><br /><code><br />Now we can say (more generalized form),<br /><br />Move nth heap on T (n), from R (n)<br />Means,<br />Move (n-1)th heap on S(n), from R(n)<br />Move n to T(n), from R(n).<br />Move (n-1)th heap on T(n), from S(n), over n.<br /><br />Move 0th heap on T (0), from R (0).<br />Means,<br />Move nothing<br /></code><br /><br />Now we’ll just leave this recursive solution as is, and look for some patterns, knowing recursive solutions are slow.<br /><br />Let me try a non-recursive solution also and remember we had a Pattern ABCA, and ACBA there is another pattern of moves. Smallest disk moves every alternate move, that is starting from the first move and first move is always for the smallest.<br /><br /><pre><br />Moves of disk,<br />1: 1-3-5-7-9-11 and so on and so forth.<br />2: -2---6---10---14 and so on and so forth.<br />3: ---4-------12-------18 and so on and so forth.<br />4: -------8---------------24 and so on and so forth.<br /></pre><br />--<br /><br />Logically nth, disk should first move after (n-1)th heap is moved on S(n) and that’s “(2^(n-1)-1)+1”th move. See if “321” formation has to happen on B for 4 to get on C. We know by now it will take 7 moves before 4 moves. Thus it is “2^(n-1)”th move or for 4 its 8th move. So the first move of any disk isn’t a function of number of disks.<br /><br /><code><br />First move of nth disk happens at “2^(n-1)”th move of the disk system.<br /></code><br /><br />Let us see when nth disk will move the second time, let us take the moves of moving 4 disks.<br /><br />(A:4321, B: -, C: -)<br />(A:432, B: 1, C: -)<br />(A:43, B: 1, C: 2)*<br />(A:43, B: -, C: 21)<br />(A:-4, B: 3, C: 21)<br />(A:41, B: 3, C: 2)<br />(A:41, B: 32, C: -)*<br />(A:4, B: 321, C: -)<br />(A:-, B: 321, C: 4)<br />(A:-, B: 32, C: 41)<br />(A:2, B: 3, C: 41)*<br />(A:21, B: 3, C: 4)<br />(A:21, B: -, C: 43)<br />(A:2, B: 1, C: 43)<br />(A: -, B: 1, C: 432)*<br />(A: -, B: -, C: 4321)<br /><br />(“*” marks the move of disk 2)<br /><br /><br /><pre><br /> 1<br /> 2<br />4 3<br />+-------+-------+---<br /><br /><br /> 1<br /> 2<br /> 3 4<br />+-------+-------+---<br /><br /></pre><br />--<br /><br />See the recursive algorithm we made, that says move “n-1”th heap on S(n), and then move nth disk to T(n).<br /><br /><pre><br /> 1<br /> 2<br /> 3<br /> 4<br />+-------+-------+---<br /><br /></pre><br />--<br /><br />And later move “n-1”th heap to T(n).<br /><br /><pre><br /><br />3 1<br />4 2<br />+-------+-------+---<br /><br /> 1<br />4 3 2<br />+-------+-------+---<br />(First time “n-1”th disk moves is after “n-2”th heap was moved to T(n)=C )<br /><br /> 1<br /> 2<br />4 3 <br />+-------+-------+---<br />(Moving “n-2”th heap over “n-1”th disk)<br /><br /> 1<br /> 2<br /> 3 4<br />+-------+-------+---<br />(nth disk moves to T(n) )<br /><br />1<br />2 3 4<br />+-------+-------+---<br />(“n-2”th heap again moves to R (n) to free “n-1”th disk to move to T(n) )<br /><br />1 3<br />2 4<br />+-------+-------+---<br />(second time “n-1”th disk moves)<br /><br /></pre><br />--<br /><br />Meanwhile, first time “n-1”th disk moved was “2^(n-2)” after that it took ( ( 2^(n-2) ) - 1) moves to get “n-2”th heap over it. Then one move for n to move to T(n). Then ( ( 2^(n-2) ) - 1) moves to set “n-1”th disk free to move on nth disk, that is making “n-2”th heap on R(n). Thus moves after first move are “ ( ( 2^(n-2) ) - 1) + 1 + ( ( 2^(n-2) ) - 1) = (2 ^(n-1) -1)”. This is for “n-1”th disk, so for nth disk it should be “2^n - 1”.<br /><br />So, second move for 2 should be after “2^2 - 1=3”, for 3 should be “2^3 - 1=7”, and for 4 should be “2^4 - 1=15”. Now we can say nth disk moves after this many moves, that is 2^n moves after first move or second move is that 2^(n-1)+2^n moves.<br /><br />First two moves<br />1:1,3<br />2:2,6<br />3:4,12<br />4:8,24<br />And so on<br /><br /><code><br />Second move for nth disk is at 2^(n-1)+2^n move of the disk system.<br /></code><br /><br />Now let us look for the mth move of an nth disk. Suppose we again have 4 disks:<br /><br />(A:4321, B: -, C: -)<br />(A:432, B: 1, C: -)<br />(A:43, B: 1, C: 2)*<br />(A:43, B: -, C: 21)<br />(A:-4, B: 3, C: 21)<br />(A:41, B: 3, C: 2)<br />(A:41, B: 32, C: -)*<br />(A:4, B: 321, C: -)<br />(A:-, B: 321, C: 4)<br />(A:-, B: 32, C: 41)<br />(A:2, B: 3, C: 41)*<br />(A:21, B: 3, C: 4)<br />(A:21, B: -, C: 43)<br />(A:2, B: 1, C: 43)<br />(A: -, B: 1, C: 432)*<br />(A: -, B: -, C: 4321)<br /><br /><br />This algorithm is target driven, 4 needs to reach C (Target) for that it needs “123” heap on B (Swap) for this 3 needs “12” heap on C (Swap for 3, as its Target is B, from A). So after “(A:-, B: 321, C: 4)” move 3 needs 2 to go to A, and 2 makes 1 to go to C. So there is one thing clear, pack and unpack is going on. Let us see the movement of disk 1, on the first step 4 needs to go to C, thus instruction flows to 2 to move to C and then 2 wants 1 to move to B. Once 2 reaches C it asks, 1 to come over it. After this way for 3 is clear thus it moves, then 3 needs 2 over it. This would force 1 to move off 2 and then come back.<br /><br />Leaving the first move every other move needs pack and unpack both. <br />Move (n-1)th heap on S(n)<br />Move n to T(n)<br />Move (n-1)th heap on T(n)<br />Move (n+1) to T(n+1)<br />Move (n-1)th heap on S(n)<br />Move n to T(n)<br />Move (n-1)th heap on T(n)<br />Move (n+2) to T(n+2)<br />Move (n-1)th heap on S(n)<br />Move n to T(n)<br />Move (n-1)th heap on T(n)<br />Move (n+1) to T(n+1)<br />Move (n-1)th heap on S(n)<br />Move n to T(n)<br />Move (n-1)th heap on T(n)<br /><br />This shows after every unpack/pack we have movement of some higher number disk and we can quite evidently see between any two moves of nth disk we have “Move (n-1)th heap” twice plus one more move. This is just the same as second move. That is 2^n-1 or we can say every 2^n moves nth disk will move.<br /><br /><code><br />So,<br />mth move of nth disk, M(m,n) = 2^(n-1) + (m-1)2^n<br />or M(m,n) = 2^n( 1/2 + (m-1) )<br />or M(m,n) = 2^n(m-1/2)<br /></code><br />Let us verify for few values of m and n,<br />M(1,1)=1<br />M(3,2)=4(2.5)=10<br />M(2,4)=16(1.5)=24<br /><br />Now let us look at the direction of movement, remember ABCA and ACBA. Let us try to prove that. Before we go ahead let us look at the recursive algorithm we deduced.<br /><br /><code><br />Move nth heap on T (n), from R (n)<br />Means,<br />Move (n-1)th heap on S(n), from R(n)<br />Move n to T(n), from R(n).<br />Move (n-1)th heap on T(n), from S(n), over n.<br /><br />Move 0th heap on T (0), from R (0).<br />Means,<br />Move nothing<br /></code><br /><br />Now if R (n) =A, T (n) =C, so S (n) =B<br />And know T (n-1) = S (n), as we start with all are at R (n), so R (n-1) =A, Thus S (n-1) =C. One nth disk moves to T (n) =C, “n-1”th disk will be at S (n) =B, now new target for n-1 is T (n) =C and S (n-1) = R (n) = A<br /><br />Moves:<br />n:A(B)C<br />n-1:A(C)B(A)C<br /><br />Notation: “n:A(B)C” means A is the resting peg, B is the swap peg and C is the target peg for the nth disk.<br /><br />Moves:<br />n-1:A(C)B<br />n-2:A(B)C(A)B<br /><br />Moves:<br />n-1:B(A)C<br />n-2:B(C)A(B)C<br /><br />Move let us see how many ways any disk can move,<br /><br />A(C)B, B(A)C,C(B)A,A(B)C,C(A)B,B(C)A<br /><br />The above moves are part of the patterns we saw before.<br />ABCA has A(C)B, B(A)C,C(B)A<br />ACBA has A(B)C,C(A)B,B(C)A<br /><br />If we say, ABCA as clockwise and ACBA as anticlockwise, visualize by keeping pegs in a triangle. We will have to prove disk moving in one formation only sticks to the same formation for whole run.<br /><br />Let x can be A, B or C.<br /><br />Moves<br />n: x(x-1)x+1<br />n-1: x(x+1)x-1,x-1(x)x+1<br /><br />n: x(x+1)x-1<br />n-1: x(x-1)x+1,x+1(x)x-1<br /><br />So we can see if nth disk wants to move from x to x+1, then it needs to move “n-1”th disk from x to x-1 to x+1, or to say x to x-1 to x-2. For nth disk’s move from x to x-1, it is x to x+1 to x-1, or to say x to x+1 to x+2.<br /><br />We can see x(x+1)x-1 and x-1(x)x+1 move in the same direct (anticlockwise)and so do x(x-1)x+1 and x+1(x)x-1 (clockwise).<br /><br />So we slow if nth disk moves clockwise, “n-1”th disk moves anticlockwise and visa versa. Now let us look at the bigger picture. Largest Disk always moves from A to C that is anticlockwise. This moves the heap over it from two times, that also in clockwise direction. This would make next heap four times in anticlockwise direction and so on so forth.<br /><br />This even gives the proof for the series we saw before, 2^0, 2^2…, 2^n-1 as number of moves for disk in descending order.<br /><br />So, we can say now<br />If largest disk is even, then all even disks move in anticlockwise direction and all odd disks move on clockwise direction. Now as smallest disk always moves first, the first movement will be clockwise, that is AB.<br /><br />Else if largest disk is odd, then all odd disks move in anticlockwise direction and all even disks move on clockwise direction. Thus the first movement will be anticlockwise, that is AC.<br /><br />Let us make a formula to get the disk direction, if N the largest disk and n is the target disk,0 < n <=N.<br /><br /><code><br />Direction, D(N,n) = (N-n) mod 2<br />0 will mean anticlockwise, that is ACBA<br />1 will mean clockwise, that is ABCA<br /></code><br /><br />From previous discussion we even know that mth move of nth disk happens at “2^n (m-1/2)”. Now we even know how nth disk move in an N disk set.<br /><br />M(m,n) = 2^n(m-1/2)<br />or <br />m=M(m,n)/2^n+1/2<br /><br />This M(m,n) is nothing but kth move of this disk system. Let us write it again with k.<br /><br />m=k/2^n+1/2<br /><br />This would give us a fractional number, and will have to discard the fraction part of it to get the location of nth disk at kth disk system move.<br /><br />m=floor(k/2^n+1/2)<br /><br />As all disks are on A to begin with, we can do something more.<br />Let peg A=0, B=1, C=2<br /><br />Peg location of nth disk on kth move, P(n,k) = floor(k/2^n+1/2) mod 3. So if I make to define a function that return a list, [ACB] or [ABC] depending on the input, D(N,n) will be an input to that.<br /><br />L(a) = [ACB] for a =0, And <br />L(a) = [ABC] for a =1<br /><br />Now, P(n,k) is the index in the returned list for some value of N and n. Now if L(a)[i] means ith index of list L(a).<br /><br /><code><br />Then location of nth disk, in a set of N disks, on the kth system move, <br />W(n,N,k) = L( D(N,n) ) [P(n,k)]<br />or<br />W(n,N,k) = L( (N – n) mod 2 ) [floor( (k/2^n) + (1/2) ) mod 3]<br />where,<br />L(a) = [ACB] for a =0, And <br />L(a) = [ABC] for a =1<br /></code><br /><br />Let us try a few,<br />W(2,4,8)=L(4-2 mod 2)[floor(8/2^2+1/2) mod 3] = L(0)[2] = B<br />W(4,5,24)=L(5-4 mod 2)[floor(24/2^4+1/2) mod 3] = L(1)[2] = C<br /><br />Let us now try “The Tower of Brahma”<br />W(64,64,2^63)=L(0 mod 2)[ floor( 2^63/2^64+1/2 ) mod 3 ] = L(0)[1] = C, the first and the last time the largest disk of Brahma’s tower moves or it is half way to end of the world.<br /><br />Now we have a mathematical representation of the recursive algorithm we had deduced before.<br /><br />Before I sign off, don't you see this disk system recording time like a clock? Assuming moving any disk takes the same amount of time.Nitin Vermahttp://www.blogger.com/profile/10431708447076575171noreply@blogger.com0tag:blogger.com,1999:blog-6979907.post-44271551780767518622007-05-10T16:35:00.000-07:002007-05-14T10:54:32.240-07:00Never stop writing non-production codeAfter we started with jobs (or to say once we are out of school), initial year are about experimentation. As learning is need of the hour. We write more non-production code than production code. This makes us understand and then implement what we are told to. This goes on for few years but after that we gather so much of experience that we really don’t need to write sample codes before we implement something. Still we do get new kind of implementations to do and we do what we did before, write some non-production codes. Introduction of testing tools for test first programming even allow us not to spend our time on sample codes but just write direct production codes. As those are always tested every-time you run the build. So the journey from an ad-hoc code to a mature code that even solves the problem at hand can happen while writing production code only. This conservers your energy and only channelizes in solving the problem at hand. This approach normally reaps great results and most of the time error free codes. I can’t remember an instance were my code failed in a scenario that I had thought of before. Even after reaping so much from test first programming I felt dissatisfied. There was something missing from this coder’s diet -- Unbound-creativity.<br /><br />What author refers to as Unbound-creativity?<br />An unstoppable desire to, create on a limitless canvas.<br /><br />Every time you put a new layer – you add new constraints. Those help do work faster and neater. Even using testing frameworks make you work in their constraints. From creativity standpoint standardization is a necessary evil. Standardization has all the benefits in the world for commercial organizations and large-distributed coding teams but in end of the day, it comes in the way of the creativity of an individual. Coding isn’t an exact science by any means; it’s an Art of instructing a computer. Per say physics also relies on an Art -- Art of reading and understanding nature. Nothing that human does can just be science; it will have an element of art to it.<br />In true sense we all programmers are artists, we create something that a beholder can admire. It can be your code or its output but most of the firms don’t think so they just think programmers as resources, in a simple mathematical model. They have helped commercialization of this art. To sustain and keep resource outputs predictable, they shortlist languages and create a pool of commodity programmers by increasing demand. As they have money and we need some every month, we have to ourselves strike a balance. Don’t ever stop learning; to help this you can do innovative things. Like learn a language every week/month, or select a language else than what you work with. Then try to create all you can think-off with the new language.<br /><br />Whatever you do start-writing some code again for yourself, enjoy writing code without any specifications or guidelines. May be a few layers below you normally code at work!? Write some nonsense, fun-sake, enjoyable, masterpiece, state of the art non-production code, just for yourself regularly.<br /><br />And remember what Donald Ervin Knuth wrote once;<br /><br />“A programmer who subconsciously views himself as an artist will enjoy what he does and will do it better”<br /><br />( <a href="http://nitin.tumblr.com/post/1761267">http://nitin.tumblr.com/post/1761267</a> )Nitin Vermahttp://www.blogger.com/profile/10431708447076575171noreply@blogger.com4tag:blogger.com,1999:blog-6979907.post-1161332968316638542006-10-20T01:28:00.000-07:002006-10-20T01:29:28.326-07:00Happy Deepawali!<a href="http://photos1.blogger.com/blogger/7022/406/640/19-10-06_1624.jpg"><img style="CLEAR: all; FLOAT: left; MARGIN: 0px 10px 10px 0px; CURSOR: hand" alt="" src="http://photos1.blogger.com/blogger/7022/406/320/19-10-06_1624.jpg" border="0" /></a> <a href='http://picasa.google.com/blogger/' target='ext'><img src='http://photos1.blogger.com/pbp.gif' alt='Posted by Picasa' style='border: 0px none ; padding: 0px; background: transparent none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial;' align='middle' border='0' /></a> Nitin Vermahttp://www.blogger.com/profile/10431708447076575171noreply@blogger.com0tag:blogger.com,1999:blog-6979907.post-1148796620894812502006-05-27T23:08:00.000-07:002006-10-20T01:42:58.036-07:00My baby "Mangal Pandey"<a href="http://photos1.blogger.com/blogger/7022/406/640/DSC06149.jpg"><img style="margin: 0px 10px 10px 0px; float: left;" alt="" src="http://photos1.blogger.com/blogger/7022/406/320/DSC06149.jpg" border="0" /></a> <a href="http://picasa.google.com/blogger/" target="ext"><img src="http://photos1.blogger.com/pbp.gif" alt="Posted by Picasa" style="border: 0px none ; padding: 0px; background: transparent none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" align="middle" border="0" /></a>Nitin Vermahttp://www.blogger.com/profile/10431708447076575171noreply@blogger.com0tag:blogger.com,1999:blog-6979907.post-1086605282228717762004-06-07T03:43:00.000-07:002007-05-14T10:07:59.747-07:00AJP13 CPingAJP13 CPing
<br />
<br /><pre>
<br />
<br />package edu.nitin.apache.ajp;
<br />
<br />import java.net.Socket;
<br />import java.net.SocketException;
<br />import java.io.InputStream;
<br />import java.io.OutputStream;
<br />import java.io.IOException;
<br />import java.io.ByteArrayOutputStream;
<br />import java.io.DataInputStream;
<br />import java.io.DataOutputStream;
<br />
<br />/**
<br /> *
<br /> * @author Nitin Verma
<br /> */
<br />public class TCPClient {
<br />
<br /> /** Creates a new instance of TCPClient */
<br /> public TCPClient(String host, int port) {
<br />
<br /> Socket socket = null;
<br /> try {
<br /> if(host == null) {
<br /> host = "127.0.0.1";
<br /> }
<br /> if(port == 0) {
<br /> port = 8080;
<br /> }
<br /> System.out.println("host:port = " + host + ":" + port);
<br /> socket = new Socket(host, port);
<br /> final InputStream in = socket.getInputStream();
<br /> final DataOutputStream out = new DataOutputStream(socket.getOutputStream());
<br /> Thread t1 = new Thread(getRunnableReader(in));
<br /> t1.start();
<br /> byte [] magic = {0x12, 0x34};
<br /> byte [] data = {0x0A};
<br /> ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
<br /> byteArrayOutputStream.write(data);
<br /> int dataLenght = byteArrayOutputStream.size();
<br /> int MAX_LENGTH = 8 * 1024 - 2 /*data length*/ - magic.length;
<br /> if( dataLenght >= MAX_LENGTH ) {
<br /> System.err.println("> max length [" + MAX_LENGTH + "]");
<br /> return;
<br /> }
<br />
<br /> // Magic bytes
<br /> out.write(magic);
<br /> out.flush();
<br /> // Data lenght
<br />
<br /> out.writeShort(dataLenght);
<br /> out.flush();
<br /> // Data
<br /> byteArrayOutputStream.writeTo(out);
<br /> out.flush();
<br /> t1.join();
<br /> }
<br /> catch (IOException ioe) {
<br /> ioe.printStackTrace();
<br /> }
<br /> catch (Throwable th) {
<br /> th.printStackTrace();
<br /> }
<br /> finally {
<br /> System.out.println();
<br /> try {
<br /> socket.close();
<br /> }
<br /> catch (IOException ioe) {
<br /> ioe.printStackTrace();
<br /> }
<br /> }
<br /> }
<br />
<br /> private Runnable getRunnableReader(final InputStream in) {
<br /> Runnable runnable = new Runnable() {
<br /> public void run() {
<br /> try{
<br /> int rByte = 0;
<br /> while((rByte = in.read()) != -1){
<br /> System.out.print(Integer.toHexString(rByte));
<br /> System.out.print(" ");
<br /> }
<br /> }
<br /> catch(SocketException se){
<br /> se.printStackTrace();
<br /> return;
<br /> }
<br /> catch(Throwable th){
<br /> th.printStackTrace();
<br /> return;
<br /> }
<br /> }
<br /> };
<br /> return runnable;
<br /> }
<br />
<br /> /**
<br /> * @param args the command line arguments
<br /> */
<br /> public static void main(String[] args) {
<br /> String host = null;
<br /> int port = 0;
<br /> if(args.length >= 2) {
<br /> host = args[0];
<br /> port = Integer.parseInt(args[1]);
<br /> }
<br /> new TCPClient(host, port);
<br /> }
<br />
<br />
<br />}
<br />
<br /></pre>Nitin Vermahttp://www.blogger.com/profile/10431708447076575171noreply@blogger.com0tag:blogger.com,1999:blog-6979907.post-1084464333971757872004-05-13T08:50:00.000-07:002004-05-13T09:07:24.813-07:00Software PhysicsStudy of nature of software systems
<br />
<br />As we know physics is <i>study of nature</i>. On the same lines I can say Software Physics is <i>study of nature of software systems</i>.
<br />Nitin Vermahttp://www.blogger.com/profile/10431708447076575171noreply@blogger.com0tag:blogger.com,1999:blog-6979907.post-1084461658044889562004-05-13T08:20:00.000-07:002004-06-07T03:58:19.173-07:00Welome to Nitin's NetYet to think what to start with! What about core dump ? ;-)Nitin Vermahttp://www.blogger.com/profile/10431708447076575171noreply@blogger.com0