Skip to content

Commit

Permalink
+ formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
gesslerpd committed Jul 31, 2016
1 parent a63ed80 commit 24902df
Show file tree
Hide file tree
Showing 5 changed files with 377 additions and 382 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
client
server
*~

6 changes: 6 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
cc=gcc
flags=-Wall
flagsClient=-lpthread
flagsFormat=-sob -bad -bap -br -ce -cdw -cli4 -npcs -cs -nsaw -nsai -nsaf -nbc -di1 -cdb -sc -brs -brf -i4 -lp -ppi 4 -l100 --ignore-newlines -nbbo -nut
source=./src/
files= ./src/server.c ./src/client.c ./src/chat.h

all: compile

Expand All @@ -15,3 +17,7 @@ server: $(source)server.c

clean:
rm client server

format:
indent $(files) $(flagsFormat)

8 changes: 2 additions & 6 deletions src/chat.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@

#define USERNAMExMESSAGE ": "


//General
// General
#define TRUE 1
#define FALSE 0


#define SYSERR -1
#define OK 0


//color
// Colors
#define NCOLORS 12
#define RED "\x1B[31m"
#define GREEN "\x1B[32m"
Expand All @@ -36,4 +33,3 @@
#define LCYAN "\x1B[96m"
#define WHITE "\x1B[37m"
#define RESET "\x1B[0m"

252 changes: 125 additions & 127 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,146 +13,144 @@

#include "chat.h"

int done = FALSE; //boolean that tells threads when client program is finished
int done = FALSE; //boolean that tells threads when client program is finished

int sockfd; //socket file descriptor for client
int sockfd; //socket file descriptor for client

pthread_mutex_t mutexsum = PTHREAD_MUTEX_INITIALIZER; //mutual exclusion for our threads
pthread_mutex_t mutexsum = PTHREAD_MUTEX_INITIALIZER; //mutual exclusion for our threads

void *sender(); // thread function that will take user input and send out messages to server
void *sender(); // thread function that will take user input and send out messages to server

void *receiver(); // thread function that will listen for received messages coming from the server
void *receiver(); // thread function that will listen for received messages coming from the server

char sendBuffer[BUF_SIZE];
char receiveBuffer[BUF_SIZE+USERNAME_LEN+2];

int main (int argc, char *argv[]){
bzero(sendBuffer,BUF_SIZE); //zero out both buffers
bzero(receiveBuffer,BUF_SIZE+USERNAME_LEN+2);
int portnum;
char username[USERNAME_LEN];
if (argc != 4)
{
fprintf(stderr, "Usage: %s [server] [portnum] [username]\n", argv[0]);
exit(EXIT_FAILURE);
}
portnum = atoi(argv[2]);
strncpy(username,argv[3],USERNAME_LEN);
printf("server: %s\n",argv[1]);
printf("port: %d\n",portnum);
printf("username: %s\n",username);
//allow server to resolve hostnames or use ip's
struct hostent *server_host;
if ((server_host=gethostbyname(argv[1])) == NULL) { // get the host info
fprintf (stderr, "Failed to resolve server host information\n");
exit (EXIT_FAILURE);
}
printf("Host: %s\n", server_host->h_name);
printf("IP Address of host: %s\n", inet_ntoa((struct in_addr)*((struct in_addr *)server_host->h_addr)));
struct sockaddr_in server_addr; // server's internet address used for all sends and receives

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd == -1){ //socket() returns -1 on error
close (sockfd);
fprintf (stderr, "Failed to get socket file descriptor\n");
exit (EXIT_FAILURE);
}

server_addr.sin_family = AF_INET; // host byte order
server_addr.sin_port = htons(portnum); // short, network byte order
server_addr.sin_addr = *((struct in_addr *)server_host->h_addr);
memset(&(server_addr.sin_zero), '\0', 8); // zero the rest of the struct
//Make connection to server socket so we can use send() and recv() to read and write the server
if(connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == SYSERR){
close(sockfd);
fprintf(stderr, "Failed to connect to remote server!\n");
exit(EXIT_FAILURE);
}
// Create and send out open message to the server so it knows our username and we are identified as a connected client
strcpy(sendBuffer,username);
if (send(sockfd,sendBuffer,strlen(sendBuffer), 0) == SYSERR){
perror("send");
close(sockfd);
exit(EXIT_FAILURE);
}
//create threads
//Thread 1: takes in user input and sends out messages
//Thread 2: listens for messages that are comming in from the server and prints them to screen
// Set up threads
pthread_t threads[2];
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

// Run the sender and receiver threads
pthread_create(&threads[0], &attr, sender, NULL);
pthread_create(&threads[1], &attr, receiver, NULL);

// Wait until done is TRUE then exit program
while(!done);
close(sockfd);
return OK;
char receiveBuffer[BUF_SIZE + USERNAME_LEN + 2];

int
main(int argc, char *argv[]) {
bzero(sendBuffer, BUF_SIZE); //zero out both buffers
bzero(receiveBuffer, BUF_SIZE + USERNAME_LEN + 2);

int portnum;
char username[USERNAME_LEN];

if(argc != 4) {
fprintf(stderr, "Usage: %s [server] [portnum] [username]\n", argv[0]);
exit(EXIT_FAILURE);
}

portnum = atoi(argv[2]);
strncpy(username, argv[3], USERNAME_LEN);

printf("server: %s\n", argv[1]);
printf("port: %d\n", portnum);
printf("username: %s\n", username);

//allow server to resolve hostnames or use ip's
struct hostent *server_host;

if((server_host = gethostbyname(argv[1])) == NULL) { // get the host info
fprintf(stderr, "Failed to resolve server host information\n");
exit(EXIT_FAILURE);
}

printf("Host: %s\n", server_host->h_name);
printf("IP Address of host: %s\n", inet_ntoa((struct in_addr)
*((struct in_addr *)
server_host->h_addr)));

struct sockaddr_in server_addr; // server's internet address used for all sends and receives

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd == -1) { //socket() returns -1 on error
close(sockfd);
fprintf(stderr, "Failed to get socket file descriptor\n");
exit(EXIT_FAILURE);
}

server_addr.sin_family = AF_INET; // host byte order
server_addr.sin_port = htons(portnum); // short, network byte order
server_addr.sin_addr = *((struct in_addr *) server_host->h_addr);
memset(&(server_addr.sin_zero), '\0', 8); // zero the rest of the struct

//Make connection to server socket so we can use send() and recv() to read and write the server
if(connect(sockfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr)) == SYSERR) {
close(sockfd);
fprintf(stderr, "Failed to connect to remote server!\n");
exit(EXIT_FAILURE);
}
// Create and send out open message to the server so it knows our username and we are identified as a connected client

strcpy(sendBuffer, username);
if(send(sockfd, sendBuffer, strlen(sendBuffer), 0) == SYSERR) {
perror("send");
close(sockfd);
exit(EXIT_FAILURE);
}
//create threads
//Thread 1: takes in user input and sends out messages
//Thread 2: listens for messages that are comming in from the server and prints them to screen
// Set up threads
pthread_t threads[2];
pthread_attr_t attr;

pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

// Run the sender and receiver threads
pthread_create(&threads[0], &attr, sender, NULL);
pthread_create(&threads[1], &attr, receiver, NULL);

// Wait until done is TRUE then exit program
while(!done);

close(sockfd);
return OK;
}

void *
sender() {

void *sender(){
while(1) {
bzero(sendBuffer, BUF_SIZE);

while(1){
bzero(sendBuffer,BUF_SIZE);
fgets(sendBuffer, BUF_SIZE, stdin);

fgets(sendBuffer, BUF_SIZE, stdin);
//send message to server
if(send(sockfd, sendBuffer, strlen(sendBuffer), 0) == SYSERR) {
perror("send");
done = TRUE;
pthread_mutex_destroy(&mutexsum);
pthread_exit(NULL);
}
// Check for quiting
if(strcmp(sendBuffer, CLOSE) == 0 || strcmp(sendBuffer, EXIT) == 0) {

//send message to server
if (send(sockfd,sendBuffer,strlen(sendBuffer), 0) == SYSERR){
perror("send");
done = TRUE;
pthread_mutex_destroy(&mutexsum);
pthread_exit(NULL);
}
// Check for quiting
if(strcmp(sendBuffer,CLOSE)==0 || strcmp(sendBuffer,EXIT) == 0){
done = TRUE;
pthread_mutex_destroy(&mutexsum);
pthread_exit(NULL);

done = TRUE;
pthread_mutex_destroy(&mutexsum);
pthread_exit(NULL);

}
pthread_mutex_unlock (&mutexsum);
}
}
pthread_mutex_unlock(&mutexsum);
}
}


void *receiver(){
int nbytes;
while(1){
bzero(receiveBuffer,BUF_SIZE);

//Receive messages from server
if ((nbytes = recv(sockfd, receiveBuffer, BUF_SIZE-1, 0)) == SYSERR) {
perror("recv");
done = TRUE;
pthread_mutex_destroy(&mutexsum);
pthread_exit(NULL);
}
receiveBuffer[nbytes] = '\0';
printf("%s",receiveBuffer);
pthread_mutex_unlock (&mutexsum);
}
void *
receiver() {
int nbytes;

while(1) {
bzero(receiveBuffer, BUF_SIZE);

//Receive messages from server
if((nbytes = recv(sockfd, receiveBuffer, BUF_SIZE - 1, 0)) == SYSERR) {
perror("recv");
done = TRUE;
pthread_mutex_destroy(&mutexsum);
pthread_exit(NULL);
}
receiveBuffer[nbytes] = '\0';
printf("%s", receiveBuffer);
pthread_mutex_unlock(&mutexsum);
}
}



Loading

0 comments on commit 24902df

Please sign in to comment.