Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts

Wednesday, June 27, 2012


erver.sin_port = htons(PORT);   /* Remember htons() from "Conversions" section? =) */

  server.sin_addr.s_addr = INADDR_ANY;  /* INADDR_ANY puts your IP address automatically */  

  bzero(&(server.sin_zero),8); /* zero the rest of the structure */



 

  if(bind(fd,(struct sockaddr*)&server,sizeof(struct sockaddr))==-1){ /* calls bind() */

      printf("bind() error\n");

      exit(-1);

  }    



  if(listen(fd,BACKLOG) == -1){  /* calls listen() */

      printf("listen() error\n");

      exit(-1);

  }



while(1){

  sin_size=sizeof(struct sockaddr_in);

  if ((fd2 = accept(fd,(struct sockaddr *)&client,&sin_size))==-1){ /* calls accept() */

    printf("accept() error\n");

    exit(-1);

  }

 

  printf("You got a connection from %s\n",inet_ntoa(client.sin_addr) ); /* prints client's IP */

 

  send(fd2,"Welcome to my server.\n",22,0); /* send to the client welcome message */

 

  close(fd2); /*  close fd2 */

}

}



/* <---- SOURCE CODE ENDS HERE ----> */







9. A STREAM CLIENT EXAMPLE

=======================================



/* <---- SOURCE CODE STARTS HERE ----> */



#include <stdio.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>        /* netbd.h is needed for struct hostent =) */



#define PORT 3550  /* Open Port on Remote Host */

#define MAXDATASIZE 100   /* Max number of bytes of data */



int main(int argc, char *argv[])

{

  int fd, numbytes;  /* files descriptors */

  char buf[MAXDATASIZE];  /* buf will store received text */

 

  struct hostent *he;         /* structure that will get information about remote host */

  struct sockaddr_in server;  /* server's address information */



  if (argc !=2) {      /* this is used because our program will need one argument (IP) */

    printf("Usage: %s <IP Address>\n",argv[0]);

    exit(-1);

  }



  if ((he=gethostbyname(argv[1]))==NULL){ /* calls gethostbyname() */

    printf("gethostbyname() error\n");

    exit(-1);

  }



  if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1){  /* calls socket() */

    printf("socket() error\n");

    exit(-1);

  }



  server.sin_family = AF_INET;

  server.sin_port = htons(PORT); /* htons() is needed again */

  server.sin_addr = *((struct in_addr *)he->h_addr);  /*he->h_addr passes "*he"'s info to "h_addr" */

  bzero(&(server.sin_zero),8);



  if(connect(fd, (struct sockaddr *)&server,sizeof(struct sockaddr))==-1){ /* calls connect() */

    printf("connect() error\n");

    exit(-1);

  }



  if ((numbytes=recv(fd,buf,MAXDATASIZE,0)) == -1){  /* calls recv() */

    printf("recv() error\n");

    exit(-1);

  }



      buf[numbytes]='\0';



      printf("Server Message: %s\n",buf); /* it prints server's welcome message =) */



      close(fd);   /* close fd =) */

}



/* <---- SOURCE CODE ENDS HERE ----> */

Base Number Systems


Well Base Number Systems are the whole entire system that our computers run on, Binary is a Base System. In this case Base 2.

Before the days of languages, such as C++, C, Visual Basic and so on, there was actually Intelligent people slaving away at computers typing up endless pages of machine code. If you want to know more about the history of Machine code and etc. check out some my other tutorials for Blacksun. Well the point im trying to make is that the average computer programmer, back then had done Degrees in mathematics. 80% of the time the programmers had degrees anyway, This degree in mathematics helped them understand the way a computer works, eg. the electronic pulses caused by the computers output of 1's and 0's. This being Binary.

The Language of Numbers.

At bottom, Computers understand only one language -- the binary code of ones and zero's that represent on-off electronic pulses. Because this code is so difficult for humans, programmers have built more concise ways of expressing the binary numbers that constitute, for example the contents of a computer's memory or the address in memory of each peice of data. T wo numbering systems that can serve as convenient short hand for the binary (base 2) are octal (base 8) and Hexidecimal (base 16). Hexidecimal is sometimes known as "Hex" by programmers. Becuase 8 is raised 2 to the third power ( 8 = 2 * 2 * 2) , one octal digit is the equivilant of three binary digits, similarly, one hexidecimal digit represents 4 binary digits ( 16 is raised to the forth power). The tables below list the decimal dumbers 0 through 16 and there binary, octal, hexidecimal equivalents. In each system, the value of a digit is determined by the value of its place column. The letters A through F in hexadecimal represents the 11th through 16th digits in that system.


The process of Conversion


Decimal to Binary


Subtract the highest possible power of 2 from the decimal number - here, 4 from 5 - and continue subtracting the highest possible power from the remainder, marking a 1 in each binary place column where subtraction occurs and a 0 where it doesn't . Here. one 4, no 2 and one 1 gives binary 101.

Binary to Decimal


Add the values of all the binary places occupied by 1s. Here, to convert the 12- digit binary number 100101101001, add the place values of 2048,256,64,32,8 and 1. The result is the decimal number 2409.

Binary to Octal


Starting with the righmost digit, group the binary digits in threes, treating each three as a seperate binary number with the place values of 4,2 and 1. The sum of each of trio's place values equals one octal digit. Here, the sums of the values of each of the four groups ar 4,5, 5 and 1, making octal 4551.

Binary to Hexidecimal


Again from the right, group the binary digits in fours, treating each four as 1 binary number with the place values 8, 4, 2 and 1. The sum of each group's place values equals one hexidecimal digit. Here, the sums of the three groups are 9, 6 and 9, making hexidecimal 969.

The Principles Of Addition


Addition in Binary


Using the same rules as in decimal addition, start by adding the figures in the rightmost, or 1s column: 1 + 1. The result - 2 - is expressed in binary as 10 (one - zero). Write down the 0 and carry the 1. In the 2s column, 1 + 1 again equals 2, or binary 10;write down the 0 and carry the 1 into the 4s column. The result is 100, the binary equivalent of decimal 4.

Addition in Octal


Adding the figuress in the 1s column - 7 + 1 - gives 8, expressed in the octal system as 10 (one - zero). As in binary addition, write down the 0 and carry the 1. Next, add the figures in the 8s column, the sum of 6 and 1 is 7. The result is Octal 70 - the equivalant of binary 111000, or decimal 56.

Addition in Hexidecimal


Adding the figures in the 1s column - 7 + 9 - Gives 16, the base of the hexidecimal system, expressed as 10. Write down the 0 and carry the 1. In the 16s coloumn, add 1 to the D (13 in decimal). D plus 1 is E (14 in decimal). The result is E0 (E-Zero), Hexidecimal shorthand for binary 11100000, or decimal 224