Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

client/server application to simulate the basic functionality of secure shell using socket

Assignment 5 – minissh with socket
Use socket to write a client/server application to simulate the basic functionality of secure shell.
Once the client is connected to the server, it can keep sending shell commands to the server. For
each command, the server will return the result of executing the shell command.
• The server and client programs can be run on different machines.
• The server program is started first, waiting for client to connect.
• When the server program is terminated with CTRL-C, the client program is also
terminated.
• When the client program is terminated with CTRL-C, the server program will wait for the
next connection from client.
Simplification:
• The shell commands from the client are simple ones without arguments.
• The commands from the client can be executed on the server side by different shell
processes.
Sample run:

client/server application to simulate the basic functionality of secure shell using socket

client program – executed on a local machine with wifi connection: cs.uwindsor.ca

client/server application to simulate the basic functionality of secure shell using socket2

Answer:

client.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <netinet/in.h>
#include <netdb.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>  
/*
Title: Assignment #5 

Objective: This program is Client side.
*/

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

	/*Variables*/
	int sock;
	struct sockaddr_in server;
	struct hostent *hp;
	char buff[1024],buff2[1024],buff3[1024];
	sock = socket(AF_INET,SOCK_STREAM,0);
	
	if(sock<0)
	{
		printf("failed to create socket");
		exit(1);
	}
	
	server.sin_family=AF_INET;
	hp=gethostbyname(argv[1]);
	if(hp==0)
	{
		printf("gethostbyname failed");
		close(sock);
		exit(1);
	}
	
	memcpy(&server.sin_addr,hp->h_addr,hp->h_length);
	server.sin_addr.s_addr = inet_addr(argv[1]);
	server.sin_port=htons(5000);
	
	if(connect(sock, (struct sockaddr *) &server,sizeof(server)) <0)
	{
		printf("connect error");
		close(sock);
		exit(1);
	}
	
	while (1)
	{
		printf("enter shell command: ");
		scanf("%s", buff);
		send(sock,buff,sizeof(buff),0);
		
		recv(sock,buff2,sizeof(buff2),0);
		printf("%s\n",buff2);

	}
	signal(SIGINT, SIG_DFL);
	close(sock);
	
}

server.c

#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
/*
Title: Assignment #5 

Objective: This program is Server side.
*/

void myhandler(int signum)
{
    alarm(5);
	/* when (Ctrl C) is inputed, both server and client should stop*/
    signal(SIGINT, SIG_DFL);    
}

void main(int argc,char *argv[])
{
	/*Variables*/ 
	int sock,n;
	int pid;
	struct sockaddr_in server;
	int mysock;
	char buff[1024],buff2[1024];
	printf("Waiting for connection...\n");
	
	/*Create socket*/
	sock = socket(AF_INET,SOCK_STREAM,0);
	
	if(sock<0)
	{
		printf("failed to create socket");
		exit(1);
	}

	server.sin_family=AF_INET;
	server.sin_addr.s_addr = INADDR_ANY;	
	server.sin_port = htons(5000);
	
	/*Call bind*/
	if(bind(sock,(struct sockaddr *)&server,sizeof(server)))
	{
		printf("bind failed");
		exit(1);
	}
	
	/*Listen*/
	listen(sock,5);
	
	/*Accept*/	
	mysock=accept(sock,(struct sockaddr*) 0,0);
	
	printf("connected\n");
	do{
		signal(SIGALRM,myhandler);
		alarm(2);
		
		recv(mysock,buff,sizeof(buff),0);
		 
		printf("received command: %s\n",buff);

		pid=fork();
		if (pid == 0){
			dup2(mysock, STDOUT_FILENO);
			execlp(buff,buff, (char*)NULL);						
			printf ("Unable to execute...\n");					
		}
		 else{
			wait(&n);
			send(mysock,buff2,sizeof(buff2),0);					
		}
					
	}while(1);
	
	signal(SIGINT, SIG_DFL);	 
	 close(mysock);
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!