-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbclient (1).c
More file actions
221 lines (196 loc) · 4.84 KB
/
dbclient (1).c
File metadata and controls
221 lines (196 loc) · 4.84 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
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "msg.h"
#define BUF 256
char* interface();
void Usage(char *progname);
int LookupName(char *name,
unsigned short port,
struct sockaddr_storage *ret_addr,
size_t *ret_addrlen);
int Connect(const struct sockaddr_storage *addr,
const size_t addrlen,
int *ret_fd);
int
main(int argc, char **argv) {
if (argc != 3) {
Usage(argv[0]);
}
unsigned short port = 0;
if (sscanf(argv[2], "%hu", &port) != 1) {
Usage(argv[0]);
}
// Get an appropriate sockaddr structure.
struct sockaddr_storage addr;
size_t addrlen;
if (!LookupName(argv[1], port, &addr, &addrlen)) {
Usage(argv[0]);
}
// Connect to the remote host.
int socket_fd;
if (!Connect(&addr, addrlen, &socket_fd)) {
Usage(argv[0]);
}
// Read something from the remote host.
// Will only read BUF-1 characters at most.
char readbuf[BUF];
int res;
while (1) {
char* input = interface();
int a = atoi(input);
//quit input
if(a == 0){
close(socket_fd);
return EXIT_FAILURE;
}
//put input
if(a == 1){
struct record rec;
struct msg mes;
printf("\nEnter the name: ");
char* buf = NULL;
size_t len = 100;
getline(&buf,&len,stdin);
strcpy(rec.name,buf);
printf("\nEnter the id: ");
buf = NULL;
getline(&buf,&len,stdin);
char* tempid = buf;
uint32_t id = atoi(tempid);
rec.id = id;
mes.type = 1;
mes.rd = rec;
send(socket_fd,&mes,sizeof(struct msg),0);
}
//get input
if(a == 2){
struct record rec;
struct msg mes;
printf("\nEnter the id: ");
char* buf = NULL;
size_t len = 100;
getline(&buf,&len,stdin);
char* tempid = buf;
uint32_t id = atoi(tempid);
rec.id = id;
mes.type = 2;
mes.rd = rec;
send(socket_fd,&mes,sizeof(struct msg),0);
}
continue;
res = read(socket_fd, readbuf, BUF-1);
if (res == 0) {
printf("socket closed prematurely \n");
close(socket_fd);
return EXIT_FAILURE;
}
if (res == -1) {
if (errno == EINTR)
continue;
printf("socket read failure \n");
close(socket_fd);
return EXIT_FAILURE;
}
readbuf[res] = '\0';
printf("%s", readbuf);
continue;
}
// Write something to the remote host.
while (1) {
int wres = write(socket_fd, readbuf, res);
if (wres == 0) {
printf("socket closed prematurely \n");
close(socket_fd);
return EXIT_FAILURE;
}
if (wres == -1) {
if (errno == EINTR)
continue;
printf("socket write failure \n");
close(socket_fd);
return EXIT_FAILURE;
}
break;
}
// Clean up.
close(socket_fd);
return EXIT_SUCCESS;
}
void
Usage(char *progname) {
printf("usage: %s hostname port \n", progname);
exit(EXIT_FAILURE);
}
int
LookupName(char *name,
unsigned short port,
struct sockaddr_storage *ret_addr,
size_t *ret_addrlen) {
struct addrinfo hints, *results;
int retval;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
// Do the lookup by invoking getaddrinfo().
if ((retval = getaddrinfo(name, NULL, &hints, &results)) != 0) {
printf( "getaddrinfo failed: %s", gai_strerror(retval));
return 0;
}
// Set the port in the first result.
if (results->ai_family == AF_INET) {
struct sockaddr_in *v4addr =
(struct sockaddr_in *) (results->ai_addr);
v4addr->sin_port = htons(port);
} else if (results->ai_family == AF_INET6) {
struct sockaddr_in6 *v6addr =
(struct sockaddr_in6 *)(results->ai_addr);
v6addr->sin6_port = htons(port);
} else {
printf("getaddrinfo failed to provide an IPv4 or IPv6 address \n");
freeaddrinfo(results);
return 0;
}
// Return the first result.
assert(results != NULL);
memcpy(ret_addr, results->ai_addr, results->ai_addrlen);
*ret_addrlen = results->ai_addrlen;
// Clean up.
freeaddrinfo(results);
return 1;
}
int
Connect(const struct sockaddr_storage *addr,
const size_t addrlen,
int *ret_fd) {
// Create the socket.
int socket_fd = socket(addr->ss_family, SOCK_STREAM, 0);
if (socket_fd == -1) {
printf("socket() failed: %s", strerror(errno));
return 0;
}
// Connect the socket to the remote host.
int res = connect(socket_fd,
(const struct sockaddr *)(addr),
addrlen);
if (res == -1) {
printf("connect() failed: %s", strerror(errno));
return 0;
}
*ret_fd = socket_fd;
return 1;
}
char* interface(){
printf("Enter your choice (1 to put, 2 to get, 0 to quit): ");
char* buf = NULL;
size_t len = 100;
getline(&buf,&len,stdin);
return buf;
}