Saturday, November 8, 2008

A bunch of c files for file transfer using tcp/ip sockets

There are 2 files :
1. server.c
2. client.c


server.c :
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>  
#include <arpa/inet.h>
#include <stdio.h>
 
#define PORT 2080
 
main()
{
        int sock1,sock2, clength;
        sock1 =  socket(AF_INET,SOCK_STREAM,0);
        struct sockaddr_in serv,cli;
 
        serv.sin_family = AF_INET;
        serv.sin_port = htons(PORT);
        serv.sin_addr.s_addr = inet_addr("127.0.0.1");
        bind(sock1,(struct sockaddr *)&serv, sizeof(serv));
        listen(sock1,5);
        clength = sizeof(cli);
        int i=0;
        char buf[50];
        sock2 = accept(sock1,(struct sockaddr *)&cli,&clength);
        printf("\n Client Connected\n");
        FILE* fp = fopen("server.txt","r");
        while(!feof(fp)){
                bzero(buf,sizeof(buf));
                fread(buf,sizeof(char),50,fp);
                write(sock2,buf,50);
        }
        write(sock2,"quit1234",50);
        fclose(fp);
        return 0;
}
 
client.c :
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>  
#include <arpa/inet.h>
#include <stdio.h>
 
#define PORT 2080
 
main()
{
        int sock1;
        sock1 =  socket(AF_INET,SOCK_STREAM,0);
        struct sockaddr_in serv;
 
        serv.sin_port = htons(PORT);
        printf("%x %x\n",PORT,htons(PORT));
        serv.sin_family = AF_INET;
        serv.sin_addr.s_addr = inet_addr("127.0.0.1");
        printf("client connecting\n");
        connect(sock1, (struct sockaddr *)&serv,sizeof(serv));
        char buf[50];
        FILE* fp = fopen("client.txt","w");
        while(1){
                bzero(buf,sizeof(buf));
                read(sock1,buf,50);
                if(strcmp(buf,"quit1234")==0)
                {
                        break;  
                }
                fprintf(fp,"%s",buf);
        }
        fclose(fp);
}

Friday, November 7, 2008

Achieving Mathematical Proofs Via Computers

Achieving Mathematical Proofs Via Computers

Friday, October 24, 2008

Ubuntu forgot password

If you don't remember any of your Ubuntu passwords while logging in,
then,
from the boot menu,
select -> Recovery Mode(Ubuntu, not Windows),
then select -> drop root shell
You will get a shell where you would be logged in as root.
There type :
passwd <username>
and change the password.
Reboot.
done.

Thursday, October 23, 2008

Non recursive algorithm for Post Order Traversal

For Pre,Post and In order traversals of a Binary tree,
it is very easy to write recursive programs/algorithms.
But when it comes to a non-recursive solution, it becomes
rather difficult. Though pre-order and in-order traversals
have non-recursive algorithms, which are slightly easier
to implement/understand, post-order is a tough nut to
crack.

Not any more.
Here is an easy to understand non-recursive algorithm
for post-order traversal :
Fact I : Post order traversal(Left-Right-Root) is the exact opposite
of Root-Right-Left traversal(RoRiL)(Prove it!).
Fact II : And a non-recursive algo for Pre-order(RoLRi) is equally good
for RoRiL.

So, just use the non-recursive algo for RoRiL(which is similar to Pre-order)
and traverse it backwards.(that's the post order traversal)

(Pre-order Non Recursive Algorithm)

Blog Archive