-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalClient.c
More file actions
201 lines (194 loc) · 6.64 KB
/
FinalClient.c
File metadata and controls
201 lines (194 loc) · 6.64 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
//================================================= file = catdogClient.c =====
//= A UDP client to send strings to catdogServer (with retry capability) =
//=============================================================================
//= Notes: =
//= 1) This program conditionally compiles for Winsock and BSD sockets. =
//= Set the initial #define to WIN or BSD as appropriate. =
//= 2) This program assumes that the IP address of the host running =
//= udpServer is defined in "#define IP_ADDR" =
//= 3) The time-out is the #define as MAX_WAIT =
//=---------------------------------------------------------------------------=
//= Example execution: =
//=---------------------------------------------------------------------------=
//= Build: bcc32 catdogClient.c or cl catdogClient.c wsock32.lib for Winsock =
//= gcc catdogClient.c -lsocket -lnsl for BSD =
//=---------------------------------------------------------------------------=
//= Execute: catdogClient =
//=---------------------------------------------------------------------------=
//= Author: Ken Christensen =
//= University of South Florida =
//= WWW: http://www.csee.usf.edu/~christen =
//= Email: christen@csee.usf.edu =
//=---------------------------------------------------------------------------=
//= History: KJC (09/11/15) - Genesis (from udpClient.c) =
//=============================================================================
#define BSD // WIN for Winsock and BSD for BSD sockets
//----- Include files ---------------------------------------------------------
#include <stdio.h> // Needed for printf()
#include <string.h> // Needed for memcpy() and strcpy()
#include <stdlib.h> // Needed for exit()
#include <time.h> // Needed for time()
#ifdef WIN
#include < windows.h > // Needed for all Winsock stuff
#endif
#ifdef BSD
#include <sys/types.h> // Needed for sockets stuff
#include <netinet/in.h> // Needed for sockets stuff
#include <sys/socket.h> // Needed for sockets stuff
#include <arpa/inet.h> // Needed for sockets stuff
#include <fcntl.h> // Needed for sockets stuff
#include <netdb.h> // Needed for sockets stuff
#endif
//----- Defines ---------------------------------------------------------------
#define PORT_NUM 1050 // Port number used
#define IP_ADDR "66.158.200.153" //"127.0.0.1" // IP address of server1 (*** HARDWIRED ***)
#define MAX_WAIT 5 // Maximum wait in seconds
//===== Main program ==========================================================
int clean_stdin()
{
while (getchar()!='\n');
return 1;
}
int main(int argc, char * argv[]) {
#ifdef WIN
WORD wVersionRequested = MAKEWORD(1, 1); // Stuff for WSA functions
WSADATA wsaData; // Stuff for WSA functions
#endif
int client_s; // Client socket descriptor
struct sockaddr_in server_addr; // Server Internet address
unsigned long int noBlock; // Non-blocking flag
int addr_len; // Internet address length
char out_buf[4096]; // Output buffer for data
char in_buf[4096]; // Input buffer for data
int retcode; // Return code
int timeStart; // Time of start for loop
int i; // Loop counter
// Output usage
/* if (argc != 2)
{
printf("Usage: catdogClient <string> where <string> is a maximum 140 \n");
printf(" character string to be cat/dog converted and reflected \n");
exit(1);
} */
#ifdef WIN
// This stuff initializes winsock
WSAStartup(wVersionRequested, & wsaData);
#endif
// Create a socket
client_s = socket(AF_INET, SOCK_DGRAM, 0);
if (client_s < 0) {
printf("*** ERROR - socket() failed \n");
exit(-1);
}
#ifdef WIN
// Make client_s non-blocking
noBlock = 1;
ioctlsocket(client_s, FIONBIO, & noBlock);
#endif
#ifdef BSD
int flags = fcntl(client_s, F_GETFL, 0);
fcntl(client_s, F_SETFL, flags | O_NONBLOCK);
#endif
// Fill-in server1 socket's address information
server_addr.sin_family = AF_INET; // Address family to use
server_addr.sin_port = htons(PORT_NUM); // Port num to use
server_addr.sin_addr.s_addr = inet_addr(IP_ADDR); // IP address to use
// Loop until receive reply message
timeStart = time(NULL);
char username[] = "null";
while (1) {
while(1)
{
int input = -1;
char c;
do
{
printf("Select your operation. \n1. Register Username\n2. Un-register Username\n3. Login\n4. Request Quote(s)\n5. Exit Application\nYour Selection:");
} while (((scanf("%d%c", &input, &c)!=2 || c!='\n') && clean_stdin()) || input<1 || input>6);
switch(input)
{
case 1:
printf("\nPlease enter a username to register with:");
scanf("%s", &username);
// TODO : if username fits criteria send, else loop
sprintf(out_buf, "REG,%s;", username);
goto sendPkt;
break; //should never reach here
case 2:
printf("\nPlease enter a username you want to un-register:");
scanf("%s", &username);
sprintf(out_buf, "UNR,%s;", username);
goto sendPkt;
break;
case 3:
printf("\nPlease enter a username to send to the server:");
scanf("%s", &username);
break;
case 4:
if(0 != strcmp("null",username))
{
char quotes[] = "";
printf("\nPlease enter the quote(s) you want to receive:");
scanf("%s", "es);
// TODO : loop asking for quotes until exit contidion, append to a comma seperated string and store in quotes
sprintf(out_buf, "QUO,%s,%s;", username,quotes);
goto sendPkt;
}
else {
printf("Usename not on file, please register with (1) or login with (3).\n\n");
break;
}
case 5:
goto exit;
}
}
sendPkt:
// Assign a message to buffer out_buf
//strcpy(out_buf, argv[1]);
// Now send the message to server.
retcode = sendto(client_s, out_buf, (strlen(out_buf) + 1), 0, (struct sockaddr * ) & server_addr, sizeof(server_addr));
if (retcode < 0) {
printf("*** ERROR - sendto() failed \n");
exit(-1);
}
//printf("Send to server: %s \n", out_buf);
// Wait to receive a message
retcode = 0;
for (i = 0; i < MAX_WAIT; i++) {
addr_len = sizeof(server_addr);
retcode = recvfrom(client_s, in_buf, sizeof(in_buf), 0, (struct sockaddr * ) & server_addr, & addr_len);
if (retcode > 0) {
printf("Elapsed time = %d seconds \n", time(NULL) - timeStart);
printf("Received from server: %s \n", in_buf);
}
#ifdef WIN
Sleep(1000); // Windows Sleep for 1000 ms (1 second)
#endif
#ifdef BSD
sleep(1); // Unix sleep for 1 second
#endif
}
}
exit:
// Close all open sockets
#ifdef WIN
retcode = closesocket(client_s);
if (retcode < 0) {
printf("*** ERROR - closesocket() failed \n");
exit(-1);
}
#endif
#ifdef BSD
retcode = close(client_s);
if (retcode < 0) {
printf("*** ERROR - close() failed \n");
exit(-1);
}
#endif
#ifdef WIN
// This stuff cleans-up winsock
WSACleanup();
#endif
// Return zero and terminate
return (0);
}