You are on page 1of 14

&

20906
22

| | 2 | 27-1-2014

Client

#include
#include
#include
#include
#include

<stdio.h>
<stdlib.h>
<unistd.h>
<errno.h>
<string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <signal.h>
#include <fcntl.h>
#define PORT 78
int clntSockfh;
//
int error(const char *msg){
perror(strcat("Error : ", msg));
}
//
void printMenu(){
char choise;
char comm[256];
do{
// .
conversate("create_child");
// EOF
server.
printf("Execute a command remotely : (EOF to end)\n?");
if( fgets(comm, 255, stdin) != NULL)
conversate(comm);
else {conversate("dis"); break;}
// .
conversate("kill_child");

| | 2 | 27-1-2014

}while(1);

// interrupt o client .
void disconnect(){conversate("dis"); exit(0);}
int sConnect(){
signal(SIGINT, disconnect);
//Server address.
struct sockaddr_in servAddr;
//host
struct hostent *server;
//Number of port.
unsigned short portno = PORT;
char host[30] = "127.0.0.1";
clntSockfh = socket(AF_INET, SOCK_STREAM, 0); // IPPROTO_TCP
if(clntSockfh < 0) error("Cannot open socket.");
server = gethostbyname(host);
if(server == NULL){fprintf(stderr, "Error : no such host");
exit(0);}
bzero((char *) &servAddr, sizeof(servAddr));
servAddr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&servAddr.sin_addr.s_addr,
server->h_length);
servAddr.sin_port = htons(portno);
//Connect
if(connect(clntSockfh, (struct sockaddr *) &servAddr,
sizeof(servAddr)) < 0)
error("Could not connect,");
printf("\nConnected to : %s:%d\n", host, portno);
return 0;
}

int conversate( char msg[255]){


char buffer[256];
int check;
//protypo, 9a alla3ei
bzero(buffer, 256);
check = write(clntSockfh, msg, 255);
//check = send(clntSockfh, "I got your message", 18, 0);
if(check < 0) error("Could not write to socket.");

| | 2 | 27-1-2014

check = read(clntSockfh, buffer, 255);


//check = recv(clntSockfh, buffer, sizeof(buffer), 0);
if(check < 0) error("Could not read from socket.");
printf("Client output : %s\n", buffer);
return 0;
}
int main( int argc, const char* argv[] ){
//Socket file handler.
char buffer[256];
sConnect();
printMenu();

close(clntSockfh);
return 0;

Client

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/file.h>

| | 2 | 27-1-2014

#include <netinet/in.h>
#include <netdb.h>
#include <signal.h>
//solaris
#include <fcntl.h>
#define PORT 78
#define READ 0 /* Read end o f pipe */
#define WRITE 1 /* Write end o f pipe */
int stop = 0;
int servSockfh;
int error(const char *msg){
perror(strcat("Error : ", msg));
}
void sig_handler(int signo)
{
if(signo == SIGUSR1)
stop = 1;
if(signo == SIGINT){
printf("\nClosing socket.\n Exiting..\n");
close(servSockfh);
exit(0);
}

}
int main( int argc, const char* argv[] ){
signal(SIGINT, sig_handler);

| | 2 | 27-1-2014

//Socket file handlers.


int clntSockfh;
//Server & Client address.
struct sockaddr_in servAddr, clntAddr;
//Number of port.
unsigned short portno = PORT;
socklen_t clntLen; // unsigned int
int conmsg;
char buffer[256];
char str[21];
int fd[2];//pipe
printf("Server running...\n");
//Socket
bzero((char *) &servAddr, sizeof(servAddr));
servSockfh = socket(AF_INET, SOCK_STREAM, 0);
//IPPROTO_TCP
if( servSockfh < 0) error("Could not open socket.");
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(portno);
//Bind
if( bind(servSockfh,
(struct sockaddr *) &servAddr,
sizeof(servAddr)) < 0) error("Could not
bind.");
//Listen
listen(servSockfh, 1); //1 participant
clntLen = sizeof(clntAddr);

| | 2 | 27-1-2014

while(1){ // interrupted
//Accept
clntSockfh = accept(servSockfh,
(struct sockaddr *)
&clntAddr,
&clntLen);
if( clntSockfh < 0) error("Could not accept.");
inet_ntop(AF_INET, &(clntAddr.sin_addr), str,
INET_ADDRSTRLEN);
printf("Client address : %s\n", str);
//Recv
//protupo apo dw kai katw, 9a alla3ei
printf("Server got connection\n ");
bzero(buffer, 256);
while (conmsg = read(clntSockfh, buffer, 255)){
//conmsg = recv(clntSockfh, buffer, sizeof(buffer), 0);
if(conmsg < 0) error("Could not read from socket.");
printf("Server output : %s\n", buffer);
int checkExec =0;
pid_t pid;
char response[255];
char* command;
char *p = strtok(str, " ");
char message[255]="nessage";
int bytes;
if(strcmp(buffer, "create_child") == 0){
if(pipe(fd)==-1){perror("pipe");}
pid = fork();
if (pid >= 0){
if(pid == 0){ // child process

| | 2 | 27-1-2014

while(stop == 0) {//
,
SIGUSR1
printf("Child says :
Here.\n");
sleep(1);
signal(SIGUSR1, sig_handler);
}//
'
close ( fd [ WRITE ] ) ;

/*close

unused end */
bytes=read ( fd [ READ ] ,
message, sizeof( message ) ) ;
printf( "Read %d bytes : %s\n" ,
bytes , message ) ;
close ( fd [ READ ] ) ;
//
execvp
char *f[100];
char *v;
int i = 1;
v = strtok(message, " \n");
f[0] = v;
while (v = strtok(NULL, " \n")){
f[i] = v;
i++;
}
f[i] = NULL;
int fd =
open("./executed_commands", O_WRONLY | O_CREAT, S_IRUSR
| S_IWUSR | S_IRGRP);
// output fd
executed commands.

| | 2 | 27-1-2014

dup2(fd, 1);
//
execvp(f[0], f);
// exec,
.
perror("FAILED");
}
else{ //Parent process
//write response to server
sprintf(response, "Child created
with pid : %d.\n", pid);
printf(response);
conmsg = write(clntSockfh,
response, 255);
if(conmsg < 0) error("Could not
write to socket.");
conmsg = read(clntSockfh, buffer,
255);
if(conmsg < 0) error("Could not
read from socket.");
printf("%s", buffer);
if(strcmp(buffer, "dis") == 0){
sprintf(response,
"Disconecting from server");
kill(pid, SIGSTOP);
conmsg = write(clntSockfh,
response, 255);
if(conmsg < 0) error("Could
not write to socket.");
close(clntSockfh);
break;
}
kill(pid, SIGUSR1);
close(fd[READ]); /* Close
unused end */

| | 2 | 27-1-2014

write(fd[WRITE], buffer,
strlen(buffer)+1);
close(fd[WRITE]); /* Close
used end */
sprintf(response,
"Child running command : %s", buffer);
printf(response);
conmsg =
write(clntSockfh, response, 255);
if(conmsg < 0)
error("Could not write to socket.");
conmsg = read(clntSockfh, buffer,
255);
if(conmsg < 0) error("Could not
read from socket.");
sprintf(response, "Trying to
stop child : %d.\n", pid);
printf(response);
conmsg = write(clntSockfh,
response, 255);
if(conmsg < 0) error("Could
not write to socket.");
kill(pid, SIGSTOP);
}
}
}else{
sprintf(response, "Could not fork
child.");
printf(response);
conmsg = write(clntSockfh, response,
255);
if(conmsg < 0) error("Could not write
to socket.");
}
}
}

return 0;}

| | 2 | 27-1-2014


1. server.

| | 2 | 27-1-2014

2. o Client. Server ip client. Server


OUTPUT: client.
. client ip
port server,
. EOF.

| | 2 | 27-1-2014

3. O Client ls -l. Server


. Client .

| | 2 | 27-1-2014

executed_commands ls -l.

EOF client server


. client disconnect interrupt signal.

interrupt server socket


0.

| | 2 | 27-1-2014

You might also like