-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemailclient.c
More file actions
224 lines (188 loc) · 5.11 KB
/
emailclient.c
File metadata and controls
224 lines (188 loc) · 5.11 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#define MAX 80
#define MAX_USER_LENGTH 20
#define PORT 8080
#define SA struct sockaddr
void usercommand(int sockfd){
char user_id[MAX_USER_LENGTH];
// read user_id
scanf("%s",user_id);
write(sockfd,user_id,sizeof(user_id));
char buff[MAX];
bzero(buff,sizeof(buff));
read(sockfd,buff,sizeof(buff)); // initial buffer.
if(strcmp(buff,"username does not exists") == 0){
// user doeas not exists.
printf("%s\n",buff);
}
else{
printf("%s",buff); // user and his mails. sucess
char command[MAX];
char messagebuffer[200];
while(1 < 2){
printf("Sub-Prompt-%s> ",user_id);
scanf("%s",command);
if(strcmp(command,"Done") == 0){
bzero(command,sizeof(command));
strcpy(command,"DONEU");
write(sockfd,command,sizeof(command));
break;
}
else if(strcmp(command,"Read") == 0){
bzero(command,sizeof(command));
strcpy(command,"READM");
write(sockfd,command,sizeof(command));
bzero(messagebuffer,sizeof(messagebuffer));
read(sockfd,messagebuffer,sizeof(messagebuffer));
printf("%s\n",messagebuffer);
}
else if(strcmp(command,"Delete") == 0){
bzero(command,sizeof(command));
strcpy(command,"DELM");
write(sockfd,command,sizeof(command));
bzero(messagebuffer,sizeof(messagebuffer));
char tempm[MAX];
read(sockfd,tempm,sizeof(tempm));
printf("%s\n",tempm);
}
else if(strcmp(command,"Send") == 0){
char rec_id[MAX_USER_LENGTH];
printf("SEND in client\n");
bzero(command,sizeof(command));
strcpy(command,"SEND");
write(sockfd,command,sizeof(command));
printf("Wrote %s\n",command);
scanf("%s",rec_id);
write(sockfd,rec_id,sizeof(rec_id)); // send username.
printf("receiver id is %s\n",rec_id);
bzero(buff,sizeof(buff));
read(sockfd,buff,sizeof(buff));
printf("%s",buff);
if(strcmp(buff,"sucess") == 0){
printf("Entered Sucess\n");
char messagebuffer[200];
char c;
int n=0,flg=0;
c = getchar();
//c = getchar();
while(flg ==0){
messagebuffer[n] = c;
n++;
if(!(n >=3 && messagebuffer[n-1] == '#' && messagebuffer[n-2] == '#' && messagebuffer[n-3] == '#'))
{
c = getchar();
}
else{
flg = 1;
}
}
messagebuffer[n] = '\0'; // read input to messagebuffer.
write(sockfd,messagebuffer,sizeof(messagebuffer));
}
else{
// userid not present.
printf("%s\n",buff);
}
}
else{
char messagebuffer[200];
char c;
int n=0;
c = getchar();
messagebuffer[n]=c;
while(messagebuffer[n]!='\n')
{ c= getchar();
messagebuffer[n]=c;
}
printf("invalid command\n");
continue;
}
}
}
}
void command_processor(int sockfd)
{
char buff[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
printf("Main-Prompt> ");
scanf("%s",buff);
if(strcmp("Adduser",buff) == 0){
// printf("Entered ADDU");
bzero(buff,sizeof(buff));
strcpy(buff,"ADDU");
write(sockfd,buff,sizeof(buff)); // gives addu to server
bzero(buff, sizeof(buff));
scanf("%s",buff); // gives id to server
write(sockfd,buff,sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd,buff,sizeof(buff)); // get reply from execution of following command wheather user created or exists.
printf("%s\n",buff);
bzero(buff, sizeof(buff));
}
else if(strcmp(buff,"Listusers") == 0){
bzero(buff,sizeof(buff));
strcpy(buff,"LSTU");
write(sockfd,buff,sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd,buff,sizeof(buff)); // get reply from execution of following command.
printf("%s\n",buff);
bzero(buff, sizeof(buff));
}
else if(strcmp(buff,"SetUser") == 0){
bzero(buff,sizeof(buff));
strcpy(buff,"USER");
write(sockfd,buff,sizeof(buff));
bzero(buff,sizeof(buff));
usercommand(sockfd);
}
else if(strcmp("Quit",buff) == 0){
bzero(buff,sizeof(buff));
strcpy(buff,"QUIT");
write(sockfd,buff,sizeof(buff)); // gives addu
bzero(buff, sizeof(buff));
break;
}
else{
printf("Invalid command read again\n");
}
}
}
int main(int argc,char* argv[])
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
// socket create and varification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
int V_Port;
sscanf(argv[2],"%d",&V_Port);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(argv[1]);
servaddr.sin_port = htons(V_Port);
// connect the client socket to server socket
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");
// function for chat
command_processor(sockfd);
// close the socket
close(sockfd);
}