Friday, 26 August 2011

Remote Command Execution


   Remote Command execution  :    

 Find out ip address of system  using
>start>run>type cmd>type ipconfig


// SERVER SIDE

 #include <arpa/inet.h>  /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h>     /* for atoi() and exit() */
#include <string.h>     /* for memset() */


ServAddr.sin_addr.s_addr = htonl(INADDR_ANY); ;  /*this means that server will serve to the specific port which is mentioned regardless of what its ip address is*/

  you need to change  IP Address in client side only


CLIENT SIDE ADD:


 struct sockaddr_in ServAddr; /* server address */



char *servIP;                    /* Server IP address (dotted quad) */
    char *echoString;


 print("enter ip");
scanf("%s",servIP);  // use argv[1]
    memset(&echoServAddr, 0, sizeof(ServAddr));     /* Zero out structure */
    echoServAddr.sin_family      = AF_INET;             /* Internet address family */
    echoServAddr.sin_addr.s_addr = inet_addr(servIP);   /* Server IP address */
    echoServAddr.sin_port        = htons(ServPort); /* Server port */




if(connfd!=0)
    printf("Server accepted client \n");
else
    printf("Server not acce
pted client\n");


recv(connfd,buff,1024,0);
    printf("Serever received msg from client  %s",buff);

 system( buff );
close(sd);
    











//  CLIENT SIDE PROGRAM
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<stddef.h>



char ip_adr[15]="10.121.9.240";   // Here type ur server ipaddress
        struct sockaddr_in serv_addr;



    /*  gets(ip_adr);*/
        serv_addr.sin_addr.s_addr =inet_addr(ip_adr);           /* the obtained ip address which is a dotted string is converted to unsigned long.*/
        serv_addr.sin_family = AF_INET;          /* this information we specify that it belongs to tcp or udp*/
        serv_addr.sin_port = htons(port_no);      /*htons is used because we may need to change it to network byte order which is always big endian*/


Thursday, 18 August 2011

#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<string.h>
#include<sys/ioctl.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<stdio.h>
#include<net/if_arp.h>

File transfer in c program

if(listen(sd,1)!=0)
    printf("Not listened\n");
else
    printf("Listened\n");




while(1)
 { 
    cd=accept(sd,NULL,NULL);

    printf("accept value %d\n",cd);

    recv(cd,buf,sizeof(buf),0);

    int fd=open(buf,O_RDONLY);

    read(fd,buf,1000);

    send(cd,buf,sizeof(buf),0);

    printf("Required File Information:%s\n",buf);

            //execute(buf);
close(cd);

}

if(connect(sockfd,(struct sockaddr *)&servaddr, sizeof(servaddr) )!=0)
{
    printf("client not connected to server\n");
    exit(0);
}
else
{
    printf("client connected to server\n");
}


   printf("\n Enter Required file name:");
scanf("%s",fname);

while(1)
 {

    send(sockfd,fname,sizeof(fname),0);

    recv(sockfd,fname,sizeof(fname),0);
 /*FILE *fp;
fp=fopen("ex3.c","w");
fputs(fname,fp);
 */
printf("SERVER Sending file datas%s\n",fname);

 }


//close client side connection
close(sockfd);







 writing strings into file using c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 50

int main()
{
  FILE *pFile = NULL;
  char *filename = "C:\\myfile.txt";
  char buffer[80] = "asdf";
  int buffer_size = BUFFER_SIZE;

  size_t str_length = 0;

  pFile = fopen(filename, "w");
  if(pFile == NULL)
  {
    printf("Error opening %s for writing. Program terminated.", filename);
    abort();
  }

  str_length = strlen(buffer);
  fwrite(&str_length, sizeof(size_t), 1, pFile);
  fwrite(buffer, str_length, 1, pFile);

  fclose(pFile);
  printf("\nFile write complete\n");
  if(buffer != NULL)
    free(buffer);

}







#include <stdio.h>

int main ()
{
FILE *pFile;
char str[10];
char buffer[10];
gets(str);
strcpy(buffer,str);
pFile = fopen ("myfile.txt" , "w");
fwrite (buffer,1,strlen(buffer),pFile);
fclose (pFile);
return 0;
}




#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
  char str[80];
  FILE *fp;

  if((fp = fopen("TEST", "w"))==NULL) {
    printf("Cannot open file.\n");
    exit(1);
  }

  do {
    printf("Enter a string (CR to quit):\n");
    gets(str);
    strcat(str, "\n");  /* add a newline */
    fputs(str, fp);
  } while(*str!='\n');

  return 0;
}

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>

int main(){

    char input[30];
    char* args[10];
    char argIndex = 0;
   
    printf("Enter command or program name: ");
    fgets(input,30,stdin);
   
    char* str;
    str = strtok(input, " ");
              
    while(str != NULL){
        args[argIndex] = str;
        argIndex++;        
        str = strtok(NULL, " ");
   }
  
   args[argIndex] = (char *) 0;

   execvp(args[0], args);

}




str = strtok(NULL, " \n");

Friday, 12 August 2011

TCP Chat Program

                                             Tcp Chat program is similar to simple tcp client server communication, but it requires loop at both side and include the below function with the tcp server and client program

 //    Include at client side for client chat

Char opt;

printf("Enter ur wish to continue y / n);

scanf("%c",&opt);
while(opt!='y')
{

printf("Type ur client message\n");
    scanf("%s",buff);

// send  msg to server
    send(sockfd,buff,1024,0);


    recv(connfd,buff1,1024,0);

    printf("Serever msg received by client  %s",buff1);      

}


//  Include at server side for server chat


Char opt;

do{

    recv(connfd,buff1,1024,0);

    printf("Serever msg received by client  %s",buff1);   

    printf("Enter ur wish to continue y / n);

scanf("%c",&opt);

   printf("Type ur client message\n");
 scanf("%s",buff);

// send  msg to server
    send(sockfd,buff,1024,0);


} while(opt!='y')







UDP Client server communication Program in c

Connectionless  Program


UDP Client Server Communication      ( Connectionless  Data Transfer )

Sender side Function:
                            socket();
                             bind();
                             recvfrom(); / sendto();
                            close();

Client side Functions:
                             socket();
                             sendto() / recvfrom();

                             close();


                    // UDP SERVER PROGRAM

#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include<netinet/in.h>
#include<sys/types.h>




int main()
{

char buff[2000];
int sd,connfd,len;

struct sockaddr_in servaddr,cliaddr;

sd = socket(AF_INET, SOCK_DGRAM, 0);

if(sd==-1)
{
    printf(" socket not created in server\n");
    exit(0);
}
else
{
    printf("socket created in  server\n");
}

bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(7802);

if ( bind(sd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0 )
    printf("Not binded\n");
else
    printf("Binded\n");

len=sizeof(cliaddr);

recvfrom(sd,buff,1024,0,
                   (struct sockaddr *)&cliaddr, &len);

    printf("UDP Server received msg from UDP client  %s",buff);

close(sd);
  
return(0);
}




                        // UDP CLIENT PROGRAM

#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include<netinet/in.h>
#include<sys/types.h>


int main()
{
char buff[2000];
int sockfd,connfd,len;

struct sockaddr_in servaddr,cliaddr;

// create socket in client side
sockfd = socket(AF_INET, SOCK_DGRAM, 0);

if(sockfd==-1)
{
    printf(" socket not created in client\n");
    exit(0);
}
else
{
    printf("socket created in  client\n");
}


bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY; // ANY address or use specific address
servaddr.sin_port = htons(7802);  // Port address


    printf("Type ur  UDP client message\n");
    scanf("%s",buff);

// send  msg to server

sendto(sockfd, buff, strlen(buff), 0,
              (struct sockaddr *)&servaddr, sizeof(struct sockaddr));

//close client side connection
close(sockfd);

return(0);
}


Steps:
       1) Compile and Run Server Program   cc udpserver.c   ./a.out
        2) Compile and Run Client Program   cc udpclient.c     ./a.out
        3) Enter Message in Client
       

Thursday, 11 August 2011

File Transfer Using TCP Algorithm

File transfer TCP Algorithm 
Server side Filer Transfer TCP Algorithm





STEP 1: Start the program. 
STEP 2: Declare the variables and structure for the socket. 
STEP 3: Create a socket using socket functions 
STEP 4: The socket is binded at the specified port. 
STEP 5: Using the object the port and address are declared. 
STEP 6: After the binding is executed the file is specified. 
STEP 7: Then the file is specified. 
STEP 8: Execute the client program. 


Client File Transfer TCP programming Algorithm 



STEP 1: Start the program. 
STEP 2: Declare the variables and structure. 
STEP 3: Socket is created and connects function is executed. 
STEP 4: If the connection is successful then server sends the message. 
STEP 5: The file name that is to be transferred is specified in the client side. 
STEP 6: The contents of the file is verified from the server side. 
STEP 7: Stop the program 



use File function


fp=open(fname,”w”);
fwrite(op,strlen(op),1,fp); 



fp=fopen(fname,”r”);
fopen(op,1000,1,fp); 





TCP UDP Client Server Communication Diagram




TCP  Client Server Communication



UDP Client Server Communication


UDP Program Steps


                                                       Udp Client Server Communication  (Connection less  service )


Algorithm Steps:

Server:

1. Create a socket using socket() system call.

2. Bind socket to port number using bind() system call.

3. Inside a loop wait for the client to send some message.

4. Receive a message using recvfrom() system call.

5. If received message is ‘bye’, send message ‘bye’ and stop the chat.  If not, send some message using sendto() system call.


Client:

1. Create a socket using socket() system call.

2. Inside a loop, type a message to be send to server.

3. Send the message using sendto() system call.

4. Receive the message from server using recvfrom() system call.

5. If the message received or send is ‘bye’ then stop.

6. Close the socket.

7. Stop.





TCP Socket Creation:

socket(AF_INET,SOCK_STREAM,0);

 UDP Socket creation:

sd = socket(AF_INET, SOCK_DGRAM, 0);


bind(sd,(struct sockaddr *)&servaddr, sizeof(struct sockaddr));


recvfrom(sd,recv_data,1024,0,
                   (struct sockaddr *)&client_addr, &addr_len);

 sendto(sd, send_data, strlen(send_data), 0,
              (struct sockaddr *)&servaddr, sizeof(struct sockaddr));

Wednesday, 10 August 2011

TCP Client server Communication Program

                                                            Connection oriented Program

                    // TCP SERVER PROGRAM

#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include<netinet/in.h>
#include<sys/types.h>




int main()
{

char buff[2000];
int sd,connfd,len;

struct sockaddr_in servaddr,cliaddr;

sd=socket(AF_INET,SOCK_STREAM,0);

if(sd==-1)
{
    printf(" socket not created in server\n");
    exit(0);
}
else
{
    printf("socket created in  server\n");
}

bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(7802);

if ( bind(sd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0 )
    printf("Not binded\n");
else
    printf("Binded\n");

if(listen(sd,1)!=0)
    printf("Not listened\n");
else
    printf("Listened\n");

len=sizeof(cliaddr);

connfd=accept(sd,(struct sockaddr *) &cliaddr, &len);

if(connfd!=0) 
    printf("Server accepted client \n");
else
    printf("Server not accepted client\n");


recv(connfd,buff,1024,0);

    printf("Serever received msg from client  %s",buff);

close(sd);
   
return(0);
}




                        // TCP CLIENT PROGRAM

#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include<netinet/in.h>
#include<sys/types.h>



int main()
{
char buff[2000];
int sockfd,connfd,len;

struct sockaddr_in servaddr,cliaddr;

// create socket in client side
sockfd=socket(AF_INET,SOCK_STREAM,0);

if(sockfd==-1)
{
    printf(" socket not created in client\n");
    exit(0);
}
else
{
    printf("socket created in  client\n");
}


bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY; // ANY address or use specific address
servaddr.sin_port = htons(7802);  // Port address

// client connected to server using server address
if(connect(sockfd,(struct sockaddr *)&servaddr, sizeof(servaddr) )!=0)
{
    printf("client not connected to server\n");
    exit(0);
}
else
{
    printf("client connected to server\n");
}


    printf("Type ur client message\n");
    scanf("%s",buff);

// send  msg to server
    send(sockfd,buff,1024,0);

//close client side connection
close(sockfd);

return(0);
}

Steps:
       1) Compile and Run Server Program   cc tcpserver.c   ./a.out
        2) Compile and Run Client Program   cc tcpclient.c     ./a.out
        3) Enter Message in Client