Tuesday, 1 October 2013

Embedupload

Embedupload is one of the most useful uploading service which i have came across. It allows person to upload files to many servers in very less time as well as allows us to save our bandwidth. I will recommend it to all my viewers.

Friday, 20 May 2011

Steps involved in creating a client

Following are the major steps involved in creating a client:
  1. Create a socket with the socket() system call
  2. Connect the socket to the address of the server using the connect() system call
  3. Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() system calls.

Steps involved in creating a server

Following are the major steps involved in creating a  server:
  1. Create a socket with the socket() system call
  2. Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
  3. Listen for connections with the listen() system call
  4. Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
  5. Send and receive data

Wednesday, 18 May 2011

Code for finding Ip address of server

#include <stdio.h> /*using for printf*/
#include <netdb.h> /*used for gethostbyname() function*/
#include <sys/types.h>

int main(int argc, char *argv[])
{
  struct hostent *he;/*making a structure to hold all the info related to host like hostname and host ip*/
  if (argc!=2)/*if no arguments are passed*/
  {
  argv[1]="www.google.com";/*then make www.google.com as our default option*/
  }
  he=gethostbyname(argv[1]);/*fills the he structure instance with hostname and ip address*/
  if (he==NULL)/*if he is NULL that means the host is not found*/
  {
  printf("gethostbyname() error\n");
  exit(-1);/*exit the program with error*/
  }
  printf("Hostname : %s\n",he->h_name); /* prints the hostname */
  /*inet_ntoa converts string IP addresses to long*/
  printf("IP Address: %s\n",inet_ntoa(*((struct in_addr *)he->h_addr))); /* prints IP address */
}




Sunday, 15 May 2011

IMPORTANT FUNCTIONS

I will mention some of the important functions used in socket programming and which header files to include in your program:

socket()
 #include <sys/types.h>
 #include <sys/socket.h>

 int socket(int domain,int type,int protocol);


 Let's see the arguments:

  domain -> you can set "AF_INET" (set AF_INET to use ARPA internet protocols)
  or "AF_UNIX" if you want to create sockets for inside comunication.
  Those two are the most used, but don't think that there are just
  those. There are more I just don't mention them.
  type -> here you put the kind of socket you want (Stream or Datagram)
  If you want Stream Socket the type must be SOCK_STREAM
  If you want Datagram Socket the type must be SOCK_DGRAM
  protocol -> you can just set protocol to 0


 socket() gives you a socket descriptor that you can use in later system calls or
 it gives you -1 on error (this is usefull for error checking routines).

bind()


 #include <sys/types.h>
 #include <sys/socket.h>

 int bind(int fd, struct sockaddr *my_addr,int addrlen);


 Let's see the arguments:

  fd -> is the socket file descriptor returned by socket() call
  my_addr -> is a pointer to struct sockaddr
  addrlen -> set it to sizeof(struct sockaddr)

 bind() is used when you care about your local port (usually when you use listen() )
 and its function is to associate a socket with a port (on your machine). It returns
 -1 on error.

 You can put your IP address and your port automatically:

  server.sin_port = 0; /* bind() will choose a random port*/
  server.sin_addr.s_addr = INADDR_ANY; /* puts server's IP automatically */


 An important aspect about ports and bind() is that all ports bellow 1024 are reserved.
 You can set a port above 1024 and bellow 65535 (unless the ones being used by other
 programs).

connect()

 #include <sys/types.h>
 #include <sys/socket.h>

 int connect(int fd, struct sockaddr *serv_addr, int addrlen);

 Let's see the arguments:

  fd -> is the socket file descriptor returned by socket() call
  serv_addr -> is a pointer to struct sockaddr that contains destination IP
  address and port
  addrlen -> set it to sizeof(struct sockaddr)

 connect() is used to connect to an IP address on a defined port. It returns -1 on
 error.


 listen()
 #include <sys/types.h>
 #include <sys/socket.h>

 int listen(int fd,int backlog);

 Let's see the arguments:

  fd -> is the socket file descriptor returned by socket() call
  backlog -> is the number of allowed connections

 listen() is used if you're waiting for incoming connections, this is, if you want
 someone to connect to your machine. Before calling listen(), you must call bind()
 or you'll be listening on a random port =) After calling listen() you must call
 accept() in order to accept incoming connection. Resuming, the sequence of system calls
 is:

  1. socket()
  2. bind()
  3. listen()
  4. accept() /* In the next section I'll explain how to use accept() */

 As all functions above described, listen() returns -1 on error.


accept()

 #include <sys/socket.h>

 int accept(int fd, void *addr, int *addrlen);

 Let's see the arguments:

  fd -> is the socket file descriptor returned by listen() call
  addr -> is a pointer to struct sockaddr_in where you can determine which host
  is calling you from which port
  addrlen -> set it to sizeof(struct sockaddr_in) before its address is passed
  to accept()

 send()

  int send(int fd,const void *msg,int len,int flags);

 Let's see the arguments:

  fd -> is the socket descriptor where you want to send data to
  msg -> is a pointer to the data you want to send
  len -> is the length of the data you want to send (in bytes)
  flags -> set it to 0


 This function is used to send data over stream sockets or CONNECTED datagram sockets.
 If you want to send data over UNCONNECTED datagram sockets you must use sendto().

 send() returns the number of bytes sent out and it will return -1 on error.


recv()

 int recv(int fd, void *buf, int len, unsigned int flags);

 Let's see the arguments:

  fd -> is the socket descriptor to read from
  buf -> is the buffer to read the information into
  len -> is the maximum length of the buffer
  flags -> set it to 0


 As I said above about send(), this function is used to send data over stream sockets or
 CONNECTED datagram sockets. If you want to send data over UNCONNECTED datagram sockets
 you must use recvfrom().

 recv() returns the number of bytes read into the buffer and it'll return -1 on error.


sendto()
 
 int sendto(int fd,const void *msg, int len, unsigned int flags,
  const struct sockaddr *to, int tolen);

 Let's see the arguments:

  fd -> the same as send()
  msg -> the same as send()
  len -> the same as send()
  flags -> the same as send()
  to -> is a pointer to struct sockaddr
  tolen -> set it to sizeof(struct sockaddr)

 As you can see, sendto() is just like send(). It has only two more arguments : "to"
 and "tolen" =)

 sendto() is used for UNCONNECTED datagram sockets and it returns the number of bytes
 sent out and it will return -1 on error.


recvfrom()

 int recvfrom(int fd,void *buf, int len, unsigned int flags
  struct sockaddr *from, int *fromlen);

 Let's see the arguments:

  fd -> the same as recv()
  buf -> the same as recv()
  len -> the same as recv()
  flags -> the same as recv()
  from -> is a pointer to struct sockaddr
  fromlen -> is a pointer to a local int that should be initialised
  to sizeof(struct sockaddr)

 recvfrom() returns the number of bytes received and it'll return -1 on error.


  close()

 close(fd);

 close() is used to close the connection on your socket descriptor. If you call close(),
 it won't be no more writes or reads and if someone tries to read/write will receive an
 error.


shutdown()

 int shutdown(int fd, int how);

 Let's see the arguments:

  fd -> is the socket file descriptor you want to shutdown
  how -> you put one of those numbers:
 
  0 -> receives disallowed
  1 -> sends disallowed
  2 -> sends and receives disallowed

 When how is set to 2, it's the same thing as close().

 shutdown() returns 0 on success and -1 on error.


 gethostname()

 #include <unistd.h>

 int gethostname(char *hostname, size_t size);

 Let's see the arguments:

  hostname -> is a pointer to an array that contains hostname
  size -> length of the hostname array (in bytes)


 gethostname() is used to get the name of the local machine.

Functions Related to IP Adress

In C, there are some functions that will help you manipulating IP addresses.

inet_addr() converts an IP address into an unsigned long.
inet_ntoa() converts string IP addresses to long.

Sockets Conversion Functions

There are two types of byte ordering: most significant byte and least significant byte.
This former is called "Network Byte Order" and some machines store their numbers internally
in Network Byte Order.

There are two types you can convert: short and long.
Imagine you want to convert a long from Host Byte Order to Network Byte Order.

 The following functions are used to convert :

 htons() -> "Host to Network Short"
 htonl() -> "Host to Network Long"
 ntohs() -> "Network to Host Short"
 ntohl() -> "Network to Host Long"