Advertisement
utroz

Simple-Chat (Echo Server)

Dec 27th, 2011
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.51 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2011 - Simple-Chat (Echo Server) 0.1 (Under Construction)
  3.  * developed by Raphael S.Carvalho (a.k.a Utroz)
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify it
  6.  * under the terms of the GNU General Public License as published by the Free
  7.  * Software Foundation; either version 2 of the License, or (at your option)
  8.  * any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful, but WITHOUT
  11.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  13.  * more details.  You should have received a copy of the GNU General Public
  14.  * License along with this program; if not, write to the Free Software
  15.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  16.  */
  17.  
  18. // File: Server.c
  19. /* A simple server in the internet domain using TCP
  20.    The port number is passed as an argument */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25.  
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29.  
  30. void dostuff (int , const short);
  31. void error(const char *);
  32.  
  33. int main(int argc, char *argv[])
  34. {
  35.         static short user_id = 0;
  36.         int sockfd, newsockfd, portno, pid;    
  37.         struct sockaddr_in serv_addr, cli_addr;
  38.         socklen_t clilen;
  39.      
  40.         if (argc < 2) {
  41.         fprintf(stderr,"ERROR, no port provided\n");
  42.         exit(EXIT_FAILURE);
  43.         }
  44.  
  45.         fprintf(stdout, "%s (v.%.2f) by %s", ":: Simple-Chat", 0.1, "by Utroz ::\n");
  46.         fprintf(stdout, "\nStarting server...");
  47.      
  48.         sockfd = socket(AF_INET, SOCK_STREAM, 0);
  49.         if (sockfd < 0)
  50.         error("ERROR opening socket");
  51.      
  52.         bzero((char *) &serv_addr, sizeof(serv_addr));
  53.         portno = atoi(argv[1]);
  54.      
  55.         serv_addr.sin_family = AF_INET;
  56.         serv_addr.sin_addr.s_addr = INADDR_ANY;
  57.         serv_addr.sin_port = htons(portno);
  58.      
  59.         if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  60.         error("ERROR on binding");
  61.      
  62.         listen(sockfd,5);
  63.         clilen = sizeof(cli_addr);
  64.        
  65.         fprintf(stdout, " (Ready).\n- Waiting to connections...\n\n");
  66.  
  67.         while (1) {
  68.         newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  69.         if (newsockfd < 0)
  70.                 error("ERROR on accept");
  71.         else
  72.                 fprintf(stdout, "User id: %1d was connected!\n", ++user_id);       
  73.  
  74.         pid = fork();
  75.         if (pid < 0) {
  76.                 close(newsockfd);
  77.                 error("ERROR on fork");
  78.         } else if (pid == 0) {
  79.                 close(sockfd);
  80.                 dostuff(newsockfd, user_id);
  81.         }  
  82.         }
  83.      
  84.         close(newsockfd);
  85.         close(sockfd);
  86.         return EXIT_SUCCESS;
  87. }  
  88.  
  89. void dostuff (int sock, const short id)
  90. {
  91.         int n;
  92.         char buffer[256];
  93.      
  94.         while (1) {
  95.         bzero(buffer, 256);    
  96.    
  97.         n = read(sock, buffer, 255);
  98.         if (n < 0)
  99.                 error("ERROR reading from socket");
  100.    
  101.         if (strncmp("$kill", buffer, 5) == 0) {
  102.                 fprintf(stdout, "User id: %1d was disconnected!\n", id);
  103.                 n = write(sock, "log", 4);
  104.                 close(sock);
  105.                 exit(EXIT_SUCCESS);        
  106.         } else if (buffer[0] == 0)  {  
  107.                 close(sock);
  108.                 error("ERROR reading from buffer");
  109.         }
  110.    
  111.         fprintf(stdout, "Message received: %s\n",buffer);  
  112.         n = write(sock, buffer, n);    
  113.         if (n < 0)
  114.                 error("ERROR writing to socket");
  115.         }
  116. }
  117.  
  118. void error(const char *msg)
  119. {
  120.         perror(msg);
  121.         exit(EXIT_FAILURE);
  122. }
  123.  
  124. /*---------------- */
  125. // File: Client.c
  126.  
  127. #include <stdio.h>
  128. #include <stdlib.h>
  129. #include <unistd.h>
  130. #include <string.h>
  131.  
  132. #include <sys/types.h>
  133. #include <sys/socket.h>
  134. #include <netinet/in.h>
  135. #include <netdb.h>
  136.  
  137. void error(const char *msg)
  138. {
  139.         perror(msg);
  140.         exit(0);
  141. }
  142.  
  143. int main(int argc, char *argv[])
  144. {
  145.         int sockfd, portno, n;
  146.         struct sockaddr_in serv_addr;
  147.         struct hostent *server;
  148.         char buffer[256];
  149.    
  150.         if (argc < 3) {
  151.         fprintf(stderr,"usage %s hostname port\n", argv[0]);
  152.         exit(0);
  153.         }
  154.        
  155.         printf("%s (v.%.2f) by %s", ":: Simple-Chat", 0.1, "by Utroz ::\n");
  156.    
  157.         portno = atoi(argv[2]);
  158.    
  159.         sockfd = socket(AF_INET, SOCK_STREAM, 0);
  160.         if (sockfd < 0)
  161.         error("ERROR opening socket");
  162.    
  163.         server = gethostbyname(argv[1]);    
  164.         if (server == NULL) {
  165.         fprintf(stderr,"ERROR, no such host\n");
  166.         exit(0);
  167.         }
  168.    
  169.         bzero((char *) &serv_addr, sizeof(serv_addr));
  170.    
  171.         serv_addr.sin_family = AF_INET;
  172.    
  173.         bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);    
  174.         serv_addr.sin_port = htons(portno);
  175.    
  176.         if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
  177.         error("ERROR connecting");
  178.    
  179.         while(1) {            
  180.         printf("Please enter the message: ");
  181.         bzero(buffer,256);
  182.         fgets(buffer,255,stdin);
  183.    
  184.         n = write(sockfd,buffer,strlen(buffer));
  185.         if (n < 0)
  186.                 error("ERROR writing to socket");    
  187.         //bzero(buffer,256);
  188.        
  189.         n = read(sockfd, buffer, 255);
  190.         if (strncmp("log", buffer, 3) == 0) {
  191.                 printf("Alert: You was disconnected!\n");
  192.                 close(sockfd);
  193.                 exit(0);
  194.         }
  195.         if (n < 0)
  196.                 error("ERROR reading from socket");
  197.        
  198.         printf("Message received: %s\n",buffer);
  199.         }
  200.    
  201.         close(sockfd);
  202.         return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement