Wednesday, 23 September 2015

client server program in c !

In previous tutorial "Introduction to Network Programming using TCP/IP ! " : http://programmingethicalhackerway.blogspot.in/2015/07/introduction-to-network-programming.html, We had learned about socket functions. In this tutorial, we will look at how these functions are used by server-client application. Before we proceeding, let's first we understand what exactly is the server-client ? If i say in simple words, a server is one who serve services and client is one who receive services from server. Consider a case, you are writing a program which hack the victim's computer. The idea of the program may be you are a server and your victim is client. You wrote a malicious client program which spawn shell and send it to the attacker's IP address. Now you send this client program to your victim and whenever your victim run this program, game will over. So, Your victim send shell (cmd.exe or bin/bash) to you and you receive the shell  on your server.

I hope now you get some basic idea of client-server concept. Let us first write server code , then client code. Don't get panic after looking code. I will describe each and every line in detail later.

server.c
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <arpa/inet.h>
  8. #define port 8080
  9. int main()
  10. {
  11.      int fd, newfd, serlength, clienlength, num;
  12.      int yes =1;
  13.     
  14.      struct sockaddr_in server;            
  15.      struct sockaddr_in clienlen;       
  16.  
  17.      char buffer[1024];
  18.      char *msg = "Recived your message. Good Bye\n";
  19.     
  20.     
  21.      fd=socket(AF_INET, SOCK_STREAM, 0);    
  22.      if (fd < 0)
  23.         perror("Error in Opening socket");
  24.      if (setsockopt(fd,SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)       
  25.         perror("Error in reusing address\n");
  26.      server.sin_family=AF_INET;                   
  27.      server.sin_addr.s_addr=INADDR_ANY;           
  28.      server.sin_port=htons(port);                 
  29.      serlength=sizeof(server);
  30.      if (bind(fd, (struct sockaddr *)&server, serlength) < 0)
  31.           perror("Error in binding socket");
  32.      clienlength=sizeof(clienlen);                       
  33.      if (listen(fd,10) < 0)                                
  34.           perror("Error in listening");
  35.      while (1) {
  36.          newfd =accept(fd, (struct sockaddr *)&clienlen, &clienlength);      
  37.          if (newfd < 0)
  38.             perror("Error in Accepting");
  39.         printf("A connection has been accepted from %s\n", inet_ntoa((struct in_addr)clienlen.sin_addr));
  40.          num = recv(newfd,buffer,1023,0);                   
  41.          printf("Recieve %d bytes from client\n",num);
  42.          if (num == -1) {
  43.             perror("Error in Reading");
  44.          }
  45.          else {
  46.            buffer[num]='\0';
  47.            printf("Message from client : %s\n",buffer);   
  48.            serlength = strlen(msg);
  49.            num = send(newfd,msg,serlength,0);               
  50.            if (num < serlength)
  51.                 perror("Error in writing");
  52.            if (close(newfd) == -1)
  53.                 perror("Error in closing");
  54.                 
  55.      }
  56.      }
  57.     
  58. }

Now i will explain step by step.
The header file #include <sys/types.h> contains definitions of a number of data types used in system calls.
The header file #include <netinet/in.h> contains constants and structures needed for internet domain addresses.
The header file #include <sys/socket.h> includes a number of definitions of structures needed for sockets.
The Two variables fd and newfd are file descriptors and store the values returned by the socket system call and the accept system call. clienlen stores the size of the address of the client. num is the return value for the read() and write() calls.

A sockaddr_in is a structure containing an internet address.We have already discussed about this  structure in our previous tutorial.

The socket() system call creates a new socket. It takes three arguments as we already discussed previously. If the socket call fails, it returns -1.

The setsockopt() function allow us to reuse a given address for binding. Without this option set, when the program tries to bind to a given port, it will fail if that port is already in use.

The variable server is a structure of type struct sockaddr_in. This structure has four fields and set accordingly our requirement.

Now The bind() call passes the socket file descriptor, the address structure,  and the length of the address structure.

The listen() call tells the socket to listen for incoming connections. It takes two arguments. The first argument is the socket file descriptor, and the second is the the number of connections that can be waiting.

The accept() call actually accepts an incoming connection.The accept() system call causes the process to block until a client connects to the server.It takes three arguments. The accept() function returns a new socket file descriptor for the accepted connection. This way, the original socket file descriptor can continue to be used for accepting new connections, while the new socket file descriptor
is used for communicating with the connected client.

Once a connection has been established, everything written by the client will be read by the server, and everything written by the server will be read by the client. So when our connection has been established, we will receive a message from our client and then our server send a message "Received your message. Good Bye!" to the client.


                                                  
    

if  this code is still not clear to you, then i would recommend you:
1 . C  programming for hackers.
2. Introduction to Network Programming using TCP/IP !
In the next post, we will discuss about client program.

If you like this post or have any question, please feel free to comment!
Facebook Twitter Google+

 
Back To Top