Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.73 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <netdb.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <pthread.h>
  10. #include <error.h>
  11. #include <errno.h>
  12. #include <signal.h>
  13.  
  14.  
  15.  
  16. void signal_handler(int signo) {
  17. if(signo == SIGINT) {
  18. write(STDOUT_FILENO, "Interrupt signal received.", 27);
  19. exit(0);
  20. }
  21.  
  22. if(signo == SIGTERM) {
  23. write(STDOUT_FILENO, "Terminate signal received.", 26);
  24. exit(0);
  25. }
  26.  
  27. }
  28. int sock;
  29.  
  30. int main() {
  31. signal(SIGINT, signal_handler);
  32. signal(SIGTERM, signal_handler);
  33.  
  34. struct sockaddr_in server;
  35. struct hostent *hp;
  36. char buf[1024];
  37.  
  38. //Thread func
  39.  
  40.  
  41. /* Create socket */
  42. sock = socket(AF_INET, SOCK_STREAM, 0);
  43. if (sock < 0) {
  44. perror("opening stream socket");
  45. exit(1);
  46. }
  47. while(1) {
  48. write(STDOUT_FILENO, "Client Running!nEnter the IP address to connect to: ", 52);
  49. char ip[20];
  50. int rip = read(STDIN_FILENO, ip, 20);
  51. ip[rip - 1] = '';
  52.  
  53. write(STDOUT_FILENO, "nEnter the port to connect to: ", 31);
  54. char port[10];
  55. int rpo = read(STDIN_FILENO, ip, 10);
  56. port[rpo - 1] = '';
  57. //printf("%s %s", ip, port);
  58.  
  59. /* Connect socket using name specified by command line. */
  60. server.sin_family = AF_INET;
  61. hp = gethostbyname(ip);
  62. if (hp == 0) {
  63. perror("");
  64. fprintf(stderr, "%s: unknown hostn", ip);
  65. exit(2);
  66. }
  67. bcopy(hp->h_addr, &server.sin_addr, hp->h_length);
  68. server.sin_port = htons(atoi(port));
  69.  
  70. if (connect(sock,(struct sockaddr *) &server,sizeof(server)) < 0) {
  71. perror("connecting stream socket");
  72. exit(1);
  73. }
  74.  
  75. while(1) {
  76. char buff[50];
  77. int rc = read(STDIN_FILENO, buff, 50);
  78.  
  79. if(rc == 0) {
  80. write(STDOUT_FILENO, "Invalid Command.n", 16);
  81. }
  82.  
  83. buff[rc - 1] = '';
  84. if((strcasecmp(buff, "exit") == 0)) {
  85. write(sock, "Client Disconnected.n", 22);
  86. close(sock);
  87. exit(0);
  88. }
  89. else {
  90. if (write(sock, buff, (rc - 1))< 0) {
  91. perror("writing on stream socket");
  92. }
  93.  
  94. }
  95. }
  96.  
  97. close(sock);
  98. }
  99. }
  100.  
  101. #include <sys/types.h>
  102. #include <sys/socket.h>
  103. #include <netinet/in.h>
  104. #include <errno.h>
  105. #include <netdb.h>
  106. #include <stdio.h>
  107. #include <sys/wait.h>
  108. #include <wait.h>
  109. #include <string.h>
  110. #include <unistd.h>
  111. #include <signal.h>
  112. #include <stdlib.h>
  113. #include <stdio.h>
  114.  
  115. struct Process {
  116. char name[50];
  117. int pid;
  118. char status[50];
  119. time_t stime;
  120. time_t etime;
  121.  
  122. };
  123. struct Process *pro;
  124. int pc = 0;
  125. int msgsock;
  126. char *array;
  127.  
  128. void signal_handler(int signo) {
  129. if(signo == SIGINT) {
  130. write(STDOUT_FILENO, "Interrupt signal received.", 27);
  131. exit(0);
  132. }
  133.  
  134. if(signo == SIGTERM) {
  135. write(STDOUT_FILENO, "Terminate signal received.", 26);
  136. exit(0);
  137. }
  138.  
  139. }
  140.  
  141. void add() {
  142.  
  143. int sum=0;
  144. while (array!=NULL) {
  145. int a = atoi(array);
  146. sum= sum+a;
  147. array= strtok(NULL, " n");
  148. }
  149. char b[50];
  150. int count=sprintf(b, "Sum: %dn", sum);
  151. write(msgsock, b, count);
  152.  
  153. }
  154.  
  155. void subt() {
  156.  
  157. int subt=atoi(array);
  158. array = strtok(NULL, " n");
  159.  
  160. while (array!=NULL) {
  161. int a = atoi(array);
  162. subt= subt-a;
  163. array= strtok(NULL, " n");
  164. }
  165.  
  166. char b[50];
  167. int count=sprintf(b, "Subtract Result: %dn", subt);
  168. write(msgsock, b, count);
  169. }
  170.  
  171. void mult() {
  172.  
  173. int sum=1;
  174. while (array!=NULL) {
  175. int a = atoi(array);
  176. sum= sum*a;
  177. array= strtok(NULL, " n");
  178. }
  179. char b[50];
  180. int count=sprintf(b, "Product: %dn", sum);
  181. write(msgsock, b, count);
  182. }
  183.  
  184. void run() {
  185. int status;
  186. char path[50];
  187. strcpy(path,"/usr/bin/");
  188.  
  189. int cpid = fork();
  190.  
  191. if(cpid > 0) {
  192. //Parent
  193. pid_t wid = waitpid(cpid, &status, WNOHANG);
  194. if (wid == 0) {
  195. write(STDOUT_FILENO, "Exec successfuln", 16);
  196. pro[pc].pid = cpid;
  197.  
  198. strcpy(pro[pc].name, array);
  199. strcpy(pro[pc].status, "active");
  200.  
  201. struct timeval tv1;
  202. gettimeofday(&tv1, NULL);
  203. pro[pc].stime = tv1.tv_sec;
  204. pro[pc].etime = NULL;
  205.  
  206. }
  207. else {
  208. write(STDOUT_FILENO, "Exec Unsuccessfuln", 18);
  209. }
  210. }
  211.  
  212. if(cpid == 0) {
  213. //Child
  214. strcat(path, array);
  215. int new = execl(path,"",NULL);
  216. if(new == -1) {
  217. write(msgsock, "No such file.n", 14);
  218. perror("exec failed");
  219. exit(EXIT_FAILURE);
  220. }
  221.  
  222. }
  223.  
  224. }
  225.  
  226. void list() {
  227.  
  228. write(msgsock, "PNAME PID STATUS START_TIME END_TIMEn", sizeof("PNAME PID STATUS START_TIME END_TIMEn"));
  229.  
  230.  
  231. for(int j = 0; j < pc; j++) {
  232.  
  233. char temp[500];
  234. char list= sprintf(temp, "n%s %d %s %ld %ldn", pro[j].name, pro[j].pid, pro[j].status, pro[j].stime, pro[j].etime);
  235.  
  236. write(msgsock, temp, list);
  237.  
  238. }
  239.  
  240. }
  241.  
  242. void terminate() {
  243. if(array != NULL) {
  244. int count = 0;
  245. int freshKill = 0;
  246. for(int j = 0; j < pc; j++) {
  247. if((strcasecmp(pro[j].name, array) == 0) && (strcasecmp(pro[j].status, "active") == 0)) {
  248. freshKill = kill(pro[j].pid, SIGTERM);
  249. if(freshKill == 0) {
  250. strcpy(pro[j].status, "inactive");
  251. struct timeval tv2;
  252. gettimeofday(&tv2, NULL);
  253. pro[j].etime = tv2.tv_sec;
  254. write(msgsock, "Program terminated.n", 20);
  255. }
  256. else if (freshKill == -1) {
  257. perror("kill");
  258. }
  259. count++;
  260. }
  261. }
  262.  
  263.  
  264.  
  265. if(count == 0) {
  266. write(msgsock, "Unable to terminate. Program name or id mismatch.n", 50);
  267. }
  268. }
  269. }
  270.  
  271.  
  272.  
  273.  
  274.  
  275. int main() {
  276.  
  277. int sock, length;
  278.  
  279. struct sockaddr_in server, clientinfo;
  280. char buf[1024];
  281. //char buf2[1024];
  282. int rval;
  283. int rval2;
  284. int i;
  285.  
  286.  
  287. sock = socket(AF_INET, SOCK_STREAM, 0);
  288. if (sock < 0) {
  289. perror("opening stream socket");
  290. exit(1);
  291. }
  292. /* Name socket using wildcards */
  293. server.sin_family = AF_INET;
  294. server.sin_addr.s_addr = INADDR_ANY;
  295. server.sin_port = 0;
  296. if (bind(sock, (struct sockaddr *) &server, sizeof(server))) {
  297. perror("binding stream socket");
  298. exit(1);
  299. }
  300. /* Find out assigned port number and print it out */
  301. length = sizeof(server);
  302. if (getsockname(sock, (struct sockaddr *) &server, &length)) {
  303. perror("getting socket name");
  304. exit(1);
  305. }
  306. printf("Socket has port #%dn", ntohs(server.sin_port));
  307. fflush(stdout);
  308.  
  309. /* Start accepting connections */
  310. listen(sock, 5);
  311.  
  312. do {
  313. //char buffer[1024];
  314. msgsock = accept(sock, 0, 0);
  315. if(msgsock == -1) {
  316. perror("accept");
  317. }
  318. else do {
  319. bzero(buf, sizeof(buf));
  320. if ((rval = read(msgsock, buf, 1024)) < 0)
  321. perror("reading stream message");
  322.  
  323. if (rval == 0)
  324. printf("Ending connectionn");
  325.  
  326.  
  327. array = strtok(buf, " n");
  328.  
  329. if(array == NULL)
  330. continue;
  331.  
  332. if(strcasecmp(array, "run")==0) {
  333. //write(msgsock, "Message received at server."n, 27);
  334. array =strtok(NULL," n");
  335. run();
  336. }
  337.  
  338. else if(strcasecmp(array, "add")==0) {
  339. array = strtok(NULL, " n");
  340. add();
  341. }
  342.  
  343. else if(strcasecmp(array, "subt")==0) {
  344. array = strtok(NULL, " n");
  345. subt();
  346. }
  347.  
  348. else if (strcasecmp(array, "mult")==0) {
  349. array =strtok(NULL, " ");
  350. mult();
  351. }
  352.  
  353. else if(strcasecmp(array, "list")==0) {
  354. array = strtok(NULL, " n");
  355. list();
  356. }
  357.  
  358. else if(strcasecmp(array, "terminate")==0) {
  359. array = strtok(NULL, " n");
  360. terminate();
  361. }
  362.  
  363. else {
  364. write(msgsock, "Invalid commandn", 16);
  365. }
  366.  
  367.  
  368. } while(rval != 0);
  369. close(msgsock);
  370.  
  371. } while(1);
  372.  
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement