Pages

Wednesday, October 21, 2015

Printing binary numbers In C

Because I am following K&R page by page, example by example I decided to document my effort here.  Sometimes I will be posting the answer to the question and sometimes like today I will be posting some code that solves a problem.

This is my code for printing numbers in binary format in C:


 #include <stdio.h>  
 int main() {  
   unsigned long long int x,mask,temp;  
   int leadingzero=1;int i;  
   mask=1;  
   printf("Enter a decimal number: ");  
   if (scanf("%llu",&x)) {  
     printf("Binary representation: ");  
     i=sizeof(x)*8;  
     mask = mask << i-1;  
     while (i) {  
       if ((mask & x ) == mask) {  
         printf("1");  
         leadingzero=0;}  
       else {  
         if (!leadingzero) { printf("0"); }  
       }  
       x=x<<1; i--;  
     }  
     printf("\n");  
   }//if scanf was ok  
 }  


The concept is the following:
  1. Create a mask that has a 1 in the MSBit of a variable of the same size of the decimal to convert. Both, the mask and the input variable,x, are unsigned int. In my machine, a x86_64, that means 4 bytes of length. So, in my case mask has a 1 in the bit number 31 and 0 in the rest of the bits.
  2. Start a while loop of 32 iterations. In every iteration execute a bitwise AND (&) between the mask and x, this one shifthed to the right one digit per iteration.  When the the MSB of the shifted version of x is one, the AND result in the same value of mask, so a 1 is printed.
  3. leadingzero is just a nice addition, in my opinion to avoid printing leading zeroes.

And that is for this exercise.

Wednesday, January 14, 2015

Avoiding timeout in SQLDeveloper or JDeveloper connected to remote Oracle Database

I am a remote DBA for my University. Because of that I have configured a VPN Tunnel from my home to the office that works pretty well much of the time. I might say that it works great much of the time. The only complaining I've had in years is when using sqldeveloper to connect to one of the Oracle databases at the school. As long as I kept working, doing queries, inserting, updating, etc, the connection was solid and fast. But if I directed my attention to something else. Like browsing, email reading, or any other activity, after a few minutes, around 10 minutes of idle time, the connection died.  Next query I tried, sqldeveloper greeted me with a connection failure error.

I asked some of the non-remote coworkers if they were having that issue but they did not. So I focus my interest in the Firewall (both end points). I modified the services (port 1521, or application layer oracle) to have much longer timeouts that the default the firewall was using. It did not work.

The solution to this problem is so simple. Just created a sqlnet.ora file on the $TNS_ADMIN directory with the statement:

sqlnet.expire_time = 1

And restart the listener.

Every one minute Oracle  will send a ACK package to my PC, and my PC will answer back with  another ACK.
This is a small sample on how it looks in tcpdump  (I am not using the standard 1521 port for the listener).

19:13:46.036181 IP bannerdb64.server.edu.rds2 > 192.168.40.162.49726: P 4221:4231(10) ack 162 win 295
19:13:46.298970 IP 192.168.40.162.49726 > bannerdb64.server.edu.rds2: . ack 4231 win 16570
19:14:46.049832 IP bannerdb64.server.edu.rds2 > 192.168.40.162.49726: P 4231:4241(10) ack 162 win 295
19:14:46.312593 IP 192.168.40.162.49726 > bannerdb64.server.edu.rds2: . ack 4241 win 16567
19:15:46.063600 IP bannerdb64.server.edu.rds2 > 192.168.40.162.49726: P 4241:4251(10) ack 162 win 295
19:15:46.326957 IP 192.168.40.162.49726 > bannerdb64.server.edu.rds2: . ack 4251 win 16565



Note: I also create an entry on my windows 7 registry for KeepAlive every 5 minutes. But it did not work until I configure the oracle listener as shown previously. Next thing I need to know is remove that entry and see if it still working.

I'll update this post with the result.