Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (C) 2011 - Simple-Chat (Echo Server) 0.1 (Under Construction)
- * developed by Raphael S.Carvalho (a.k.a Utroz)
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details. You should have received a copy of the GNU General Public
- * License along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
- */
- // File: Server.c
- /* A simple server in the internet domain using TCP
- The port number is passed as an argument */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- void dostuff (int , const short);
- void error(const char *);
- int main(int argc, char *argv[])
- {
- static short user_id = 0;
- int sockfd, newsockfd, portno, pid;
- struct sockaddr_in serv_addr, cli_addr;
- socklen_t clilen;
- if (argc < 2) {
- fprintf(stderr,"ERROR, no port provided\n");
- exit(EXIT_FAILURE);
- }
- fprintf(stdout, "%s (v.%.2f) by %s", ":: Simple-Chat", 0.1, "by Utroz ::\n");
- fprintf(stdout, "\nStarting server...");
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd < 0)
- error("ERROR opening socket");
- bzero((char *) &serv_addr, sizeof(serv_addr));
- portno = atoi(argv[1]);
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr = INADDR_ANY;
- serv_addr.sin_port = htons(portno);
- if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
- error("ERROR on binding");
- listen(sockfd,5);
- clilen = sizeof(cli_addr);
- fprintf(stdout, " (Ready).\n- Waiting to connections...\n\n");
- while (1) {
- newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
- if (newsockfd < 0)
- error("ERROR on accept");
- else
- fprintf(stdout, "User id: %1d was connected!\n", ++user_id);
- pid = fork();
- if (pid < 0) {
- close(newsockfd);
- error("ERROR on fork");
- } else if (pid == 0) {
- close(sockfd);
- dostuff(newsockfd, user_id);
- }
- }
- close(newsockfd);
- close(sockfd);
- return EXIT_SUCCESS;
- }
- void dostuff (int sock, const short id)
- {
- int n;
- char buffer[256];
- while (1) {
- bzero(buffer, 256);
- n = read(sock, buffer, 255);
- if (n < 0)
- error("ERROR reading from socket");
- if (strncmp("$kill", buffer, 5) == 0) {
- fprintf(stdout, "User id: %1d was disconnected!\n", id);
- n = write(sock, "log", 4);
- close(sock);
- exit(EXIT_SUCCESS);
- } else if (buffer[0] == 0) {
- close(sock);
- error("ERROR reading from buffer");
- }
- fprintf(stdout, "Message received: %s\n",buffer);
- n = write(sock, buffer, n);
- if (n < 0)
- error("ERROR writing to socket");
- }
- }
- void error(const char *msg)
- {
- perror(msg);
- exit(EXIT_FAILURE);
- }
- /*---------------- */
- // File: Client.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- void error(const char *msg)
- {
- perror(msg);
- exit(0);
- }
- int main(int argc, char *argv[])
- {
- int sockfd, portno, n;
- struct sockaddr_in serv_addr;
- struct hostent *server;
- char buffer[256];
- if (argc < 3) {
- fprintf(stderr,"usage %s hostname port\n", argv[0]);
- exit(0);
- }
- printf("%s (v.%.2f) by %s", ":: Simple-Chat", 0.1, "by Utroz ::\n");
- portno = atoi(argv[2]);
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd < 0)
- error("ERROR opening socket");
- server = gethostbyname(argv[1]);
- if (server == NULL) {
- fprintf(stderr,"ERROR, no such host\n");
- exit(0);
- }
- bzero((char *) &serv_addr, sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
- serv_addr.sin_port = htons(portno);
- if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
- error("ERROR connecting");
- while(1) {
- printf("Please enter the message: ");
- bzero(buffer,256);
- fgets(buffer,255,stdin);
- n = write(sockfd,buffer,strlen(buffer));
- if (n < 0)
- error("ERROR writing to socket");
- //bzero(buffer,256);
- n = read(sockfd, buffer, 255);
- if (strncmp("log", buffer, 3) == 0) {
- printf("Alert: You was disconnected!\n");
- close(sockfd);
- exit(0);
- }
- if (n < 0)
- error("ERROR reading from socket");
- printf("Message received: %s\n",buffer);
- }
- close(sockfd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement