Saturday, July 10, 2010

PHP data filtering : look at it some day

 <?php $my_string = filter_input(INPUT_GET, 'my_string', FILTER_SANITIZE_STRING); ?>
 Source

Thursday, July 8, 2010

flex sprite startDrag bounds

There is a method in the Sprite class
named startDrag.
One of its parameters is bounds:Rectangle.

Now, when I first tried to use it I thought,
it specified the bounds of the complete sprite,
i.e. the rectangle inside which the sprite will
be forced to stay.

1. But it is actually the bounding rectangle for the 
topLeft point of the sprite.

So, during the dragging, the topLeft point of the sprite
will be forced to stay in this rectangle.

2. Also, the x & y for the bounding rectangle are wrt
 to the parent of sprite.

3. Also, if you have applied some scaling transformations on 
your sprite, you should take that into account while specifying
x,y,width,height for your bounding rectangle.






Friday, June 25, 2010

php problem with fopen("https://..")

I am using XAMPP's latest version.

<?php
fopen( "https://www.google.com/accounts/ClientLogin","rb");
?>

Warning: fopen(https://www.google.com/accounts/ClientLogin) [function.fopen]: failed to open stream: Invalid argument in nn.php on line 2

It works for urls beginning with http://.

In my php.ini, I uncommented the following line : 
extension=php_openssl.dll

Ans : 
Ok,
I was editing the wrong php.ini.
I should have edited it in : 
xampp\apache\bin
rather than
xampp\php\

in phpinfo(), it is shown which php.ini is being loaded.

TCP/IP Network stack packets Home Network

At my home, I use a Broadband connection, behind a wireless router(Netgear).
There is a certain IP address assigned to my Laptop 10.0.0.3, for e.g. on this
wireless network.
Similarly there must be some IP address for the broadband modem I use as well.
I want a tool (for windows) which helps me in clearly visualizing, what happens
when I send a request to a website and how does the data get back to me
(and not to any other laptop connected to this wireless network), i.e.
how are the IPs resolved to make sure that I receive the data which I had
requested and not anyone else.

In case there is no such tool, a clearly written explanation would also suffice.

Ans : 
Wireshark is indeed the best way to see how this works from a packet by packet connection. However you seem to be interested in what happens inside the router, when it is translating from the external IP address to the internal address.


To see this you would need to do a wireshark capture on both sides of the router, on the client side and on the link between the cable modem(or dsl or fios)  and router. You can capture this with a hub ( not a switch, they are getting harder to find now ) that you plug the cable modem, the router and a pc running the capture software into.

Basically the router performs a species of network address translation (NAT) known by several different names (cisco calls it  protocol address translation or PAT. 

I will explain the basics, but here is a wikipedia article.

http://en.wikipedia.org/wiki/Port_address_translation

When your pc connects to a web server , it does the dns and packet connection descried by billbach above.

It then sends a tcp packet to the resolved ip address. www.google.com resolves as 64.233.169.147 to me here...

The packet looks sort of like this. I am skipping a lot of stuff, like mac addresses, flags, data... etc.

Source ip     destination ip      destination tcp port    source tcp port
10.0.0.3      64.233.169.147         80                           1032

That source tcp port will be the key to how pat works.

Your pc keeps track of the above in it's tcp connection table. You can see the tcp connection table in windows by typing netstat -an, or just netstat -a to see the dns names, but it takes longer.

If you have connected to google from your pc you will see this in the netstat -an results.

  TCP    10.0.0.3:1032     64.233.169.147:80      ESTABLISHED

Now when that packet gets to your router, the default gateway, it does a bit of trickiness to convert that packet to the external ip address handed out by your isp. Lets say that is 13.13.13.13. You can find out what it really is by going to www.whatismyip.com, or checking the status inside the router configuration gui.  It changes the source ip to its external interface, and it also modifies the tcp source port to a unique identifyer. How unique is up to the router, but it only needs to be uniqe in terms of destination and tcp port. So it looks like this...


Source ip     destination ip      destination tcp port    source tcp port
13.13.13.13      64.233.169.147         80                           32234


This is how www.google.com sees the connection, and it sends packets back on this tcp connction like this.

Source ip     destination ip      destination tcp port    source tcp port
64.233.169.147   13.13.13.13         32234                         80.

This packet gets back to the router (13.13.13.13) and it then checks it's PAT table and sees that this remote ip/source port/destination corresponds to the original connection and sends it back to the pc as 

Source ip     destination ip      destination tcp port    source tcp port
64.233.169.147   10.0.0.3          1032                        80.

So, in short it keeps track of the ip-tcp source port-tcp destination port combinations in a table for both sides of the connection, and manages its many internal ip addresses using them.

This is TCP, it does the same for UDP, and does something a little different for icmp (ping for instance). Icmp is a little tricky, if a lot of users are getting icmp source quenches from the same external hosts, then a router might have problems getting back to the right pc.  They definitely can have problems with protocols without ports. IPSEC vpn clients can have a lot of problems, especially if there are multiple clients behind the router going to the same external vpn concentrator. You might need to flash your router with new code to make it work. I did : -) .

SO, check out the wikipedia article. It is good.

JAVA error : Cannot find main class (when executing JAR)

There is a J2EE application that i have made on Netbeans 6.1 using glassfish application server. It runs perfectly fine from netbeans.But When i double click the jar file, it says " Cannot find main class" . I made the application again and tested the jar file at each step, so as to understand what part is giving the problem.Initially, it does execute, but when i insert some code related to jms (such as Session, MessageProducer,etc) it gives the error. I have posted the code, which when inserted, gives the error.

 if (destType.equals("queue"))   {    dest = (Destination) queue;  }   else   {    dest = (Destination) topic;  }   Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(dest); 
 
 Ans : 
 If you've added any external libraries to the project it'll be them. For this files to be able to run in netbeans they'll need to be placed in the Java JDK folder.

ex. C:\Program Files (x86)\Java\jdk1.6.0_07\jre\lib\ext

It should be the latest version of the jdk that you have installed. 

In this folder all the libraries netbeans use is placed. 

The files that uses to be here is following:

dsns.jar
localedate.jar
sunjce_provider.jar
sunmscapi.jar
sunpkc11.jar

So if it's any other file that file/s you should add. Or if you know you added some files just include them in your project.

JAVA Message Service: Understanding the concept of message queues

There is a JMS Application, running over the network. There is one Message producer and three message consumers (All different machines). All clients (producers and consumers) have a common Connection Factory and are registered to the same queue. 
I want to understand the running process more clearly. 

1. Does each consumer maintain its own queue ? or Do All share the producer's queue?
2. Does the consumers take the next message only when it has read the current message?
3. Or is it the contrary, The consumers take messages in their queue and then read the messages one by one.
4. Do the consumers PULL messages from the producer or does the producer PUSH messages on to the consumers.

I basically want to know the complete frame work.

Ans : 
 Hi!

1) All share the producer's queue. 
2) The queue is FIFO so you can't read the next message until after reading the current. 
3) No, if they (the clients) are registered to the same queue then they have to access the queue (on the server) 
4) It's a PULL method that is applied. That is the consumer has to acknowledge that it has read the message. 

Here are some JMS examples
http://www.java2s.com/Code/Java/J2EE/Java-Message-Service-JMS.htm

Here are some tutorials/articles on JMS
http://java.sun.com/products/jms/tutorial/index.html
http://www.roseindia.net/software-tutorials/detail/5784

Hope this helps. 

Ans 1 : 

Exception in program: javax.naming.NoInitialContextException

Im running a JMS Application using a queue. 

- It runs Fine.

- BUT when I do a Debug (F7 in netbeans) , It gives an exception in method initJMS()  (code attached).

 javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

 private void initJMS() throws JMSException      {          try         {             jndi = new InitialContext();         }         catch(Exception e)         {             JOptionPane.showMessageDialog(null, e);         }         try         {             ConnectionFactory connectionFactory = (ConnectionFactory)jndi.lookup("jms/ConnectionFactory");             Queue queue = (Queue)jndi.lookup("jms/Queue");             connection = connectionFactory.createConnection();                     session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);             dest=(Destination)queue;             producer = session.createProducer(dest);         }          catch(Exception e)         {              System.out.println(e);             JOptionPane.showMessageDialog(null, e);         }     }
 
 Ans : 
 Thanks for the tidbit.  This is for GlassFish: https://glassfish.dev.java.net/javaee5/docs/DG/beanr.html.  Can also see this: http://weblogs.java.net/blog/kalali/archive/2006/05/step_by_step_to_2.html.
 
 Ans 1 : 
 

Try this...

http://java.sun.com/products/jndi/tutorial/beyond/env/source.html

Apparently, my code is out in the car.  But if you scroll to the bottom, you will see an example of putting value in environment using HashTable.

The approach I used was same as that, but instead of Context.Applet, I was using INITIAL_CONTEXT_FACTORY (http://java.sun.com/j2se/1.4.2/docs/api/javax/naming/Context.html).  In my case I used the context factory for NDS, but you would just put in appropriate value as indicated as a fully qualified classpath to the context factory for your application.


Blog Archive