-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClient.c
More file actions
134 lines (112 loc) · 3.75 KB
/
ChatClient.c
File metadata and controls
134 lines (112 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
const char *hostname = "127.0.0.1";
const char *portNumber = "8080";
int clientSocket; //socketFD
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
struct addrinfo *results; //const for head
struct addrinfo *record; //temp for traversing
int error;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
error = getaddrinfo(hostname, portNumber, &hints, &results);
//do error checking
if(error >= 0){
printf("client: getaddressinfo() succesful\n");
}
//traversing the list
for(record = results; record != NULL; record = record->ai_next){
clientSocket = socket(record->ai_family, record->ai_socktype, 0);
if(clientSocket == -1){
continue;
}
if(connect(clientSocket,record->ai_addr,record->ai_addrlen)!= -1){
break;
}
close(clientSocket);
}
//socket() and connect() both were sucess
//send()
if(record == NULL){
printf("Error\n");
exit(EXIT_FAILURE);
}
freeaddrinfo(results);
printf("socket status:created and connected\n");
//Collect Username input from user
char username[20];// = "Hello";
printf("Please Enter A Username input\n");
scanf("%19s", username);
printf("Collected Username Input %s\n", username);
if(send(clientSocket, username, strlen(username), 0) == -1){
printf("Error\n");
exit(EXIT_FAILURE);
}
else{
printf("Msg sent successfully\n");
}
sleep(1);
//Collect Password input
//Collect Respone Message from Server
char buffer[1024];
int len;
if ( (len = recv(clientSocket, buffer, sizeof(buffer), 0)) == -1){
printf("len = %d\n", len);
perror("Failed to receive message.");
exit(EXIT_FAILURE);
}
printf("%s\n", buffer);
//Collect Password input
char password[20];// = "Hello";
printf("Please Enter A Password input\n");
scanf("%19s", password);
printf("Collected Password Input %s\n", username);
if(send(clientSocket, password, strlen(password), 0) == -1){
printf("Error\n");
exit(EXIT_FAILURE);
}
else{
printf("Msg sent successfully\n");
}
//Recieve Final Confirmation Message
memset(buffer, 0, sizeof(buffer));
if ( (len = recv(clientSocket, buffer, sizeof(buffer), 0)) == -1){
printf("len = %d\n", len);
perror("Failed to receive message.");
exit(EXIT_FAILURE);
}
printf("Confirmation MSG: %s\n", buffer);
if(strcmp(buffer, "Login Was Succes") == 0){
printf("You have Successfully Logged in, You may begin chatting\n");
}
else{
printf("Closing ClientSocket\n");
close(clientSocket);
exit(EXIT_FAILURE);
}
while(1){
//memset(message, 0, sizeof(message));
char message[20];
printf("\nPlease enter a message\n");
scanf("%19s", message);
//memset(message, 0, sizeof(message));
if(send(clientSocket, message, strlen(message), 0) == -1){
printf("Error\n");
exit(EXIT_FAILURE);
}
else{
printf("Msg sent successfully\n");
}
sleep(1);
memset(message, 0, sizeof(message));
}
close(clientSocket);
return 0;
}