Friday, June 25, 2010

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

I am using XAMPP's latest version.

fopen( "","rb");

Warning: fopen( [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 : 

Ans : 
I was editing the wrong php.ini.
I should have edited it in : 
rather than

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, 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.

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. resolves as 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         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.


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 You can find out what it really is by going to, 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         80                           32234

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

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

This packet gets back to the router ( 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          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:


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 : 

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

Here are some tutorials/articles on JMS

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:  Can also see this:
 Ans 1 : 

Try this...

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 (  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.

TCP/IP programming socket port doubts

In a simple client server code for TCP/IP: 
server side : 
int sock2 = accept(sock1,&client,&clength);

client side : 
int sock1 = socket(AF_INET,SOCK_STREAM,0);
value of sock1 at client side is 3,
and value of sock2 at server side is 4.
but ain't both the sockets same?
then what do these values denote?
Secondly, on the server side I can print
the port number of the client by cli.sin_port.
From where does that port number come?
Who assigns that port number to the client?
Client machine itself? When does that assignment happen?
I compiled my code like gcc -g filename, but could
not step into the functions like accept,connect,bind etc.\
with gdb.
What do I need to do in order to step inside these function
Do I really need to typecast variables of type "struct sockaddr_in"
to "struct sockaddr"? Since my code compiles simply with a warning
and runs fine, if I don't do this typecasting.
If I include only <arpa/inet.h> and no other header files,like <sys/socket.h>,
then my code doesn't even give a warning, if I don't do this typecasting. 
In fact, if I pass
"serv" in place of "&serv", even then there is no error/warning while
compiling. But the code doesn't work. Why?

Ans : 
A socket has 2 endpoints. Each endpoint has a unique IP address / port combination. So your two socket file units open different entities.
connect(), accept() et al are implemented by operating system. You cannot step into the operating system with userland gdb. You can debug the operating system with its own tools (I have not yet done this - the most I have done is to insert printk calls).
I hope that's a start towards answering your questions - post what you next want to know.

"can't the communication happen between client
and server since they know each other's IP and port

It does happen. It happens over the socket. User applications often don't  concern themselves with both port numbers. A client knows which port / IP it needs to call, it may not car which local port was involved (or even what is the local IP address). The operating system needs this information, so it may interpret and construct IP and TCP headers of the network frames.

Ans  :
The operating system does an implicit bind() of a connecting socket which does not have a local "name" (address / port pair). You could program that bind() yourself before calling connect(), either with a nonzero socket number (which must not be in use to the address (interface) to which you are binding) or leave the port zero and the system will choose an unused one. It is not a good idea to do this, because you bind the socket to a particular IP address (i.e. a particular Network Interface), unless you first consult the routing tables to find out which is the correct interface to which to bind. The operating system will do all that for you.

If you don't typecast to "struct sockaddr", uou can potentially get a warning that you are supplying an incompatible pointer type as an argument. Not all sockets have a port you see (e.g. SOCK_RAW sockets don't), so function prototypes specify the functions take "struct sockaddr" although for regular tcp/ip programming you will be supplying a "struct sockaddr_in". Yes the code will compile with a warning and run just fine - but the value of warnings in general is lost unless you are always aiming for a warning-free compile.

In the absence of a function prototype (as a header file might supply), the C compiler is unable to verify, promote or convert any arguments on the fly - you have to always supply exactly correct argument types and you will get no warning if you don't. Worse still, some "functions" may actually be macros and you will miss out on the macro expansion. This is the case with stat() for instance. To see whether  this applies to your code, do something like:

gcc -E -C myprog.c -o myprog.S

to only pre-process the code. Now take out the header files you took out before, and do:

gcc -E -C myprog.c -o myprog_bad.S

Your code will be the last few lines of each .S file. You should be able to find it easily because -C preserves comments (you do use comments, don't you?). Inspect your code for differences with and without the required headers. 
You  could also make a habit of compiling -Wall -Wmissing-prototypes -Wstrict-prototypes. Then you certainly would get warnings when omitting required headers. I always compile that way.

Mainframe questions

A neighbour came to me with an age old book, which
he needs to mug up in order to clear exams.
But the problem is he wants to understand few things in
it as well.

There are these sentences there in the book : 
"A limitation of mainframe software architectures is that
they do not easily support graphical user interfaces
or access to multiple databases from geographicall
dispersed sites."

I don't know old that description is since I don't know
much about mainframes. I looked up on google for
some time, but could understand these limitations
about graphical user interfaces and why can't a mainframe
access multiple databases?

Ans : 
 agree with giltjr's comments. Perhaps a short history lesson will shed more light on this...

During the 1980's and 1990's the primary display device was the 3720 family of terminals. There devices were commonly attached to the mainframe channel or via SDLC to a 3270 control unit and the terminals themselves were identified as models 3274, 3278, 3279, etc.  There was one graphic-capable terminal that required the GDDM software to be installed to use it. It was expensive and cumbersome, but it did work. The primary users of this hardware were CAD/CAM, CATIA, and SAS software.

It is common to see 3270 emulator software that communicates with the mainframe via TCP/IP today.
The database software was always leading-edge. IBM Invented SQL. Some other original mainframe database system were CICS, IDMS, IMS, Informix, Focus, SAS, and SPSS.

Accessing attributes in XML data passed as ArrayCollection

If I have an XML list like this :

<?xml version="1.0"?>
 <clip desc="desc1">
 <clip desc="desc2">

and I want to use it as a dataprovider to some tilelist, like this :
photoFeed = event.result.catalog.clip as ArrayCollection;

Then, if I want to access the attribute "desc" of clip,(inside
the datarenderer)
how do I do it?

<mx:TextArea text=????>
if I wanted the <title> part,
I could have done text="{data.title}"
but, what for the attribute "desc"?
Why is there no documentation for this?

I can do the following inside some actionscript code :
t.text = data["desc"];

but how do I do it inside mxml?

I also want to know, what actually happens, when
some XML data is passed as ArrayCollection to a
datarenderer? How does the ArrayCollection look like
if the input XML has child nodes(which may again
have attributes) and attributes
converted to the arraycollection.

Ans : 
Is there a reason you aren't using an XMLListCollection?  It would seem like a better fit.  As far as getting to the desc attribute you'd do data.clip.@desc.  Nothing more complicated than that.

MySql phrase search

I read at
that : 
Creating a SQL query for searching an exact phrase in a given field(s) in a table is not much harder than the simple query for searching a substring. However this query could be very slow if you are searching through huge amount of text in the database. So, a good solution to this problem is first to eliminate the rows which do not contain the phrase as a substring (which is fast) and then perform the regular expression (REGEXP) only to the others. Here is what I'm talking about:
SELECT * FROM abc_table WHERE t_text LIKE '%search phrase%' AND t_text REGEXP '[[:<:]]search phrase[[:>:]]'

What I don't understand is, suppose I remove the REGEXP part from the above query,
even then, shouldn't the query be equally fast?
In both the queries, each row is being searched anyway for the given text,
so what's the difference?

Ans : 
Sorry - I'm probably not being too clear

SELECT * FROM abc_table WHERE t_text REGEXP '[[:<:]]search phrase[[:>:]]'
gets the right answer, but is slow

SELECT * FROM abc_table WHERE t_text LIKE '%search phrase%' 
eliminates most but not all of the wrong answers quickly

Their suggestion of

SELECT * FROM abc_table WHERE t_text LIKE '%search phrase%' AND t_text REGEXP '[[:<:]]search phrase[[:>:]]'
SELECT * FROM abc_table WHERE t_text LIKE '%search phrase%' 
to it gets rid of most of the wrong answers quickly, followed by 
 t_text REGEXP '[[:<:]]search phrase[[:>:]]'
to get rid of any remain wrong answers.

LIKE is faster than REGEXP, but doesn't get the whole result. Using LIKE first gets you close quickly, and means the slower REGEXP is use on less cases.


Im running a JMS application using a Glassfish application server.
It runs fine when I run it through netbeans.

But when I run it by clicking directly on the jar file, I get an error : 
cannot instantiate class com.sun.appserv.naming.S1ASCtxFactory (class not found: com.sun.appserv.naming.S1ASCtxFactory)

Ans : 
You need to make sure that appropriate jars are there, check this out

Flash Media Server: Mutiple streams overlay ?

I'm a beginner on Flash Media Server, and am just exploring its features.

I wanted to know whether Flash Media Server Allows :
1. Multiple streams to be overlayed on each other
2. Switching between various streams.

Please help.

Ans : 
You could in a way do overlaying multiple streams by coding this in the client in Flash, but there is no server side support for this (yet).  You can also code to switch between various streams in the Flash client, as there isn't yet server support for this, but the next version of FMS will have this support.

WPF tutorials

I'm a beginner on WPF.
Can someone please post some good links, to learn WPF.

I want to learn about dragging and dropping elements to a canvas.

Ans : 
I'm also a beginner and I've been looking for good sites to learn. I've found these, hope it helps.

Wget : downloading urls matching a regular expression

I want to download urls recursively,
starting from :,
but I want to download only those URLs which
match the this pattern :*

I tried wget -r -D
but it downloads only index.html and stops.

I tried few other options but they didn't work as intended either.

Ans : 
Hm... no linux.... O.K. here is another alternative: w3mir. It's perl based and not restricted to linux. Actually I tried it on windows and it works as expected.

Download the w3mir. Unpack it and read the file INSTALL.w32. Basically it's the following steps to "install" it on windows.

get and install winzip from
get and install ActivePerl (now Build 509) from
get nmake.exe from

After installing the tools above, do this in the unpacked w3mir directory
  nmake install

After that w3mir will be installed in the default path of your perl Installation.

   w3mir -h

Here is a sample file for your problem: w3mir.cfg

# Retrive all of janl's home pages:
Options: recurse
# This is the two argument form of URL:.  It fetches the first into the second
Fetch-RE: m/flash/
cd: d:\mirror

Then run w3mir like this: 

   mkdir d:\mirror
   w3mir -cfgfile w3mir.cfg

Blog Archive