-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
136 lines (127 loc) · 4.15 KB
/
client.cpp
File metadata and controls
136 lines (127 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <cstdio>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>
#include <sys/wait.h>
#include <pthread.h>
void errorinwrite();
void errorinread();
void errorininput();
bool checkalphanum(char S[]);
void *clientrec(void* ptr);
int sock;
int main(int argc, char *argv[])
{
struct sockaddr_in server;
struct hostent *hp;
char buf[1024];
pthread_t thread;
/* Create socket */
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("opening stream socket");
exit(1);
}
/* Connect socket using name specified by command line. */
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
if (hp == 0) {
fprintf(stderr, "%s: unknown host\n", argv[1]);
exit(2);
}
bcopy(hp->h_addr, &server.sin_addr, hp->h_length);
server.sin_port = htons(atoi(argv[2]));
if (connect(sock,(struct sockaddr *) &server,sizeof(server)) < 0) {
perror("connecting stream socket");
exit(1);
}
write(STDOUT_FILENO,"1.Add command ---> add <list>\n",sizeof("1.Add command ---> add <list>\n"));
write(STDOUT_FILENO,"2.Subtract command --> sub <list>\n",sizeof("2.Subtract command --> sub <list>\n"));
write(STDOUT_FILENO,"3.Multiply command --> mult <list>\n",sizeof("3.Multiply command --> mult <list>\n"));
write(STDOUT_FILENO,"4.Divide command --> div <list>\n",sizeof("4.Divide command --> div <list>\n"));
write(STDOUT_FILENO,"5.Run command --> run <program name>\n",sizeof("5.Run command --> run <program name>\n"));
write(STDOUT_FILENO,"6.Kill command --> kill<name>/<pid>\n",sizeof("6.Kill command --> kill<name>/<pid>\n"));
write(STDOUT_FILENO,"7.List command --> list/ list all\n",sizeof("7.List command --> list/ list all\n"));
write(STDOUT_FILENO,"8.Exit command --> exit\n",sizeof("8.Exit command --> exit\n"));
pthread_attr_t attribute;
pthread_attr_init( &attribute);
pthread_attr_setdetachstate(&attribute,PTHREAD_CREATE_DETACHED);
int t1= pthread_create(&thread,&attribute,&clientrec,NULL);
if(t1 != 0)
perror("Thread error");
int chk2 = write(STDOUT_FILENO,"In client side program, write command\n",sizeof("In client side program, write command\n"));
if(chk2 == -1)
errorinwrite();
while(true){
char reader[300];
int nu = read(STDIN_FILENO,reader,300);
reader[nu-1]=NULL;
if(nu == -1)
errorinread();
int wr = write(sock,reader,300);
if(wr == -1)
perror("Cant send data to server");
}
close(sock);
}
void errorinwrite(){
perror("Error in Write");
}
void errorinread(){
perror("Error in read");
}
void errorininput(){
perror("Alphabet/non-numerical character detected! Write input again!");
}
void* clientrec(void* ptr){
while(true){
char sum[500];
int rd = read(sock,sum,500);
sum[rd]=NULL;
if(strcmp(sum,"Exiting server here")==0){
write(STDOUT_FILENO,"Exiting client",sizeof("Exiting client"));
write(STDOUT_FILENO,"\n",sizeof("\n"));
exit(0);
}
if(rd == -1)
errorinread();
if((strcmp(sum,"Exiting server here")!=0 ) && (rd>0)){
int c=write(STDOUT_FILENO,"Showing result\n",sizeof("Showing result\n"));
int c2=write(STDOUT_FILENO,sum,strlen(sum));
int c3=write(STDOUT_FILENO,"\n",sizeof("\n"));
if(c2 == -1 || c3 == -1 || c==-1)
errorinwrite();
int chk = write(STDOUT_FILENO,"In client side program, write command\n",sizeof("In client side program, write command\n"));
if(chk == -1)
errorinwrite();
}
if(rd == 0){
write(STDOUT_FILENO,"Exiting client\n",sizeof("Exiting client\n"));
exit(EXIT_SUCCESS);
}
}
}
bool checkalphanum(char S[]){
bool flag = false;
for(int i =0;i < strlen(S);i++){
if(!isalpha(S[i])){
flag = true;
}
else{
flag = false;
break;
}
}
return flag;
}