Created
October 31, 2015 23:24
-
-
Save skeltont/6a257f6759d92ff5ee18 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
# | |
#******************************************************************************************* | |
# Ty Skelton | |
# [email protected] | |
# CS372 | |
# | |
# References: | |
# help with sockets and server stuff: | |
# http://www.binarytides.com/server-client-example-c-sockets-linux/ | |
#******************************************************************************************* | |
# | |
*/ | |
#include "header_client.h" | |
void *send_message(int sock, char *username) { | |
char message[1000], payload[1000]; | |
printf("%s%s",username,PROMPT); | |
gets(message); | |
sprintf(payload,"%s%s%s\n", username, PROMPT, message); | |
// if(strlen(message) < 1) { | |
// printf("You need to enter in a command, plz. \n"); | |
// continue; | |
// } | |
if(strcmp(message, CMD_EXIT) == 0) { | |
printf("Exiting Chat \n"); | |
} | |
if(send(sock, payload, strlen(payload), 0) < 0) { | |
puts("Sending message failed"); | |
} | |
} | |
void *receive_message(int sock, char *server_reply) { | |
if(recv(sock, server_reply, 5000 ,0) < 0) { | |
puts("recv failed"); | |
} else { | |
printf("%s", server_reply); | |
} | |
} | |
int main(int argc , char *argv[]) { | |
int i, sock, o, gfd, pfd, flags, mode, num_read, cpid, rm, sm; | |
struct sockaddr_in server; | |
long t; | |
pthread_t threads[NUM_THREADS]; | |
char username[10], server_reply[1000]; | |
// command line argument variables | |
char* p_value = NULL; | |
char* i_value = NULL; | |
FILE *fp; | |
// GET OPTIONS | |
while ((o = getopt (argc, argv, "p:i:")) != -1) { | |
switch (o) { | |
case 'p': | |
p_value = optarg; | |
break; | |
case 'i': | |
i_value = optarg; | |
break; | |
case '?': | |
return 1; | |
default: | |
abort(); | |
} | |
} | |
// Make sure they specify a port # and an ip address. | |
if ((p_value == NULL) && (i_value == NULL)) { | |
printf("\n Usage: \n"); | |
printf("\t my_socket_client -p <port #> -i <ip address>\n\n"); | |
exit(1); | |
} | |
// Attempt to create the socket | |
sock = socket(AF_INET, SOCK_STREAM, 0); | |
if (sock == -1) { | |
printf("Could not create socket"); | |
} | |
printf("Socket created\n"); | |
server.sin_addr.s_addr = inet_addr(i_value); | |
server.sin_family = AF_INET; | |
server.sin_port = htons(atoi(p_value)); | |
//Connect to remote server | |
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) { | |
perror("connect failed. Error"); | |
return 1; | |
} | |
printf("Connected\n"); | |
printf("Enter username (up to 10 characters): "); | |
gets(username); | |
for(;;) { | |
sm = pthread_create(&threads[0], NULL, send_message(sock, username), (void *) 0); | |
rm = pthread_create(&threads[1], NULL, receive_message(sock, server_reply), (void *) 1); | |
} | |
close(sock); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment