Porting Unix Socket code to Windows

Windows sockets and UNIX type berkeley sockets provide pretty much similar interface which eases the porting effort. Apart from regular network system calls such as socket(), bind(), listen(), accept(),etc two API calls are important in Windows sockets

WSAStartup()-> should be called before calling any other winsock APIs

WSACleanup()-> should be called when program exit

After a socket connection is established, data can be transferred using the standard read() and write() calls as in UNIX sockets. Sockets must be closed by using the closesocket() function in Windows instead of close() in case of Windows.

Header File:

winsock2.h

Library:

Ws2_32.lib

Alternatively, you can add below line to header file

#pragma comment(lib, “Ws2_32.lib”);

More information on this topic can be found here @ msdn

Advertisements

Network Debugging Utility – netcat

nc or netcat utility is command line tool for debugging server or client network application. It can open TCP connections, send UDP packets, listen on TCP or UDP port,etc.

 Syntax

nc [options] [host] [port]

It is very easy to build basic client server model using this tool.  To transfer file from one host to another,

On the host where you have the file (file.txt) to be transferred, run

$nc localhost 2000 <file.txt

On host where you want to get the file, run

$nc –l 2000 >file.txt

-l option instructs to listen on the specified port.

It is sometimes useful to talk to servers through command prompt.  It helps in troubleshooting, to verify what data a server is sending in response to commands issued by the client.  For example, to retrieve the home page of a web site:

           $ echo -n “GET / HTTP/1.0\r\n\r\n” | nc http://www.yahoo.com 80

It will display the response sent by the web server. If we know the format of the requests required by the server, we can interactively send and get response using this tool.

It can also be used to do port scanning on any hosts. Port scanning is process of connecting to ports and reporting whether the service or server process is running or not.

$nc -z localhost 1-5000

Output

Connection to localhost 22 port [tcp/ssh] succeeded!

Connection to localhost 25 port [tcp/smtp] succeeded!

Connection to localhost 111 port [tcp/sunrpc] succeeded!

Connection to localhost 631 port [tcp/ipp] succeeded!

Connection to localhost 1521 port [tcp/ncube-lm] succeeded!

Connection to localhost 3306 port [tcp/mysql] succeeded!

Another option that may come in handy is the -c option, which tells netcat to execute a command with /bin/sh after it connects — sending the output to the other side of the connection. This can be used on either side of the connection.

To send data from a command to a remote host, you could use

$netcat -c ‘/bin/command’ hostname port

When netcat connects to the service on the remote host, it will attempt to send the output of /bin/command

If you use netcat -l -p 1234 -c ‘/bin/command’, it will send the output of /bin/command to the first client that connects to port 1234, and then close the connection

Little book of semaphores

” The Little book of Semaphores ” by Allen B. Downey is a terrific source for understanding semaphores and learning how to apply them. Allen Downey is an Associate Professor of Computer Science at the Olin College of Engineering in Needham MA, and a Visiting Scientist at Google, Inc. He is the author of How to Think Like a Computer Scientist and several other Free Books.

In most computer science curricula, synchronization is a module in an Operating Systems class. OS textbooks present a standard set of problems with a standard set of solutions, but most students don’t get a good understanding of the material or the ability to solve similar problems.

The approach of this book is to identify patterns that are useful for a variety of synchronization problems and then show how they can be assembled into solutions. The book includes puzzles and examples.  After each problem, the book offers a hint before showing a solution, giving students a better chance of discovering solutions on their own.

Download the book (PDF)

You can also watch an introduction to semaphores which author presented at Northeastern University

Installing Fedora in your windows machine

Insert the Fedora core CD in your CD drive.

Enter BIOS and change the “Booting Sequence” to use CD drive as your first boot device.

Then it will prompt to check for the validity of the fedora CD.  Click “Skip”.

After this, you will get “Anaconda Linux Loading” message and soon after that you will see graphical screen with “fedora” icon. Press Next

Select the language, “English” and press Next

Select the keyboard layout, “U.S.English” and press Next

Then you will get two options,

a)Install fedora

b)Upgrade exisiting installation

Select Install fedora combo box and press Next

After this you need to create partitions in your hard disk to install Linux. Select “create custom layout” option and click next.

On top you will see the summary of the existing partitions in the disk. The space which is already reserved for windows will be shown as ‘sda1’ and remaining will be seen as “Free”. If you do not have any free space, then you cannot proceed installation. You need to reboot in windows and right click “My computer” and select “Manage” menu. You will get “Computer management” window. Select “disk management”, pick the unused windows partition and right click on that and then click “delete partition” from the menu. After partition is deleted, reboot the computer with Linux CD again and proceed.

Every partition in Linux is identified by device number like sda2, sda3 and so on. Partition has mount point which will be used by the user to navigate to that directory.

Click “New” to create a new partition to install Linux operating system.

Enter “/” in the mount point text field.

Select file system type as ‘ext3’

Specify the size as 10000 (10GB)

Press next

For this partition minimum, you should reserve 5 GB for full installation of linux.

It will take you back to the partition window.

Click “New” to create another partition

Select File system type as “swap”

Specify the size as 2000 (2 GB)

It will take you back to the partition window

Click “New” to create another partition

Enter “/home” in the mount point text field

Select File system type as “ext3”

Select the “Fill to maximum allowable size” radio button and press “ok”

Click “Next” from the partition window

This will take you to the GRUB boot loader settings.

Do not change anything and click “Next”

This will take you to network settings.

It you are in network, select “manually” radio button or else select “Next”

Select “Asia/Calcutta” as your timezone.

Then enter the root password and press Next

It will take you to the package selection window.

Make sure that you select “software development” check box and select “customize now” radio button and press next

Click “Desktop Environment” and make sure that both GNOME and KDE check boxes are enabled.

Click “Development” and make sure that “Development libraries”, “Development tools”, “KDE software development” , “Fedora Eclipse”

Click “Base system” and select “System tools”, leave the other options to default.

Click Next from the packing selection window.

You will get the package installation window and click “next”

It will take 40 minutes for the installation to complete.

Once the installation completes, press “Reboot” button.

You will get the Welcome screen. Press “Forward” button.

You will get the License information screen . Press forward button.

You will get the Firewall setting with checkboxes. Check ftp, ssh, telnet and then press forward

You will ge the SELinux setting combo box. select “disabled” and press “Forward”

Select the data and time if it is not correct.

Select “do not send profile” and press “Next”

Enter the username, fullname, password and confirm password and click finish.

System will ask for a reboot, press “yes”.

You system is ready to boot with linux or windows. You will get a menu to select either ‘linux’ or ‘windows’.