Monday, July 2, 2007

What I was doing...

I was a bit busy with my project work. I will add a few things here which I came across while doing the project.

nc (netcat):

This happened when we were developing a simulator. The simulator was developed as different modules which will communicate with each other using internet sockets. It so happened that the one module was ready and we needed to test it before the other module could be coded. We initially decided to have another application that will act as the server.

But, I was too lazy. I knew that the application will send data only at a slow rate. That was when I thought about this application that I used long back. When I checked FC5, it was still there and all I needed was
$nc -l 2000
to start a server at port 2000. We could then write to that port and see the data on the screen. If there is any reply required, it can be typed back on the screen.

PS: On Ubuntu 6.4, this does not work. You need to give -p argument for the port to get recognized

What I did later: I was reading the man page and I saw that you can implement http servers using this. I thought for a while and decided to have a telnet server running ASAP. This is the QDTS (Quick and Dirty Telnet Server :) ) that I came up with.

$ mkfifo telnet
$ cat telnet | nc -l 2000 | bash > telnet

Connect to this server from anywhere using

$ nc 2000

Inserting to a file:
It is with immense pain that I write this... There is no way to insert data to the middle of a file in Linux. I searched... and searched... and searched...

The only way to do it is to read the file, write till the point were you want the data to be inserted into another file. Then write your data you wanted to be added and then write the remaining data from the original file. You may then delete the original and rename the new to original.

However, you can modify any record if the size of the new and old records match. That is, if you want to replace 50 bytes in the middle of a file, you can fseek to that point, then use fprintf to print the new 50 bytes there.

Wget fun:

Wget is a nice utility which you can use for download. Though I have done it a thousand times in past, I am just putting it here for the benefit of those who might have never found it so far. Many sites, prevent download of the music files and only allow streaming them. But, many of them do it just by checking the user agent string (A string sent by the application [for example, a browser] to web server which contains the name of the application).

This is what I did to a site which was allowing real media files to be played only by realplayer.

$ for d in `cat music.txt`; do wget -U "RealPlayer(tm) (win32)" $d; done

What am I doing currently?

I am currently trying to create a simple network library which I can club with any of my simple C programs to net-enable them. I guess, This will be a good learning experience.