Only valid users can send this request to the server.
When the name already exists in the database, if option -f occurs in the command, the server will replace the phone number for name with phone and string OK will be sent back; otherwise, an error message will be returned.
Only the administrator is allowed to execute this command.
Only the administrator is allowed to execute this command.
Only the administrator is allowed to execute this command.
Only the administrator is allowed to execute this command.
1. Create a connection 2. Look up a phone number 3. Add a new name with a phone number 4. Remove a name from the phone book 5. Add a new user 6. Remove a new user 7. Close the connectionSome comments:
This will try to create a TCP connection to the server.
---------------------------------------------------
| size | request/result strings |
---------------------------------------------------
size: 32 bit integer stored in Network Byte Order, the size of
the request/result string
len = htonl (request_length); write (sock, (void *) &len, 4); write (sock, request, len);
read (sock, &len, 4); /* read the size the of result string */
len = ntohl (len);
size = 0;
while (len > size) {
n = read (sock, result_string + size, len - size);
size += n;
}
/* now result string contains the result from the server */
#include <stdio.h>
#include <gdbm/ndbm.h>
#include <fcntl.h>
void error (const char *msg)
{
fprintf (stderr, "%s\n", msg);
exit (-1);
}
struct {
char *name;
char *phone;
} data [] = {
{"D. C. Lynch", "(316)999-4444"},
{"M. W. Joy", "(316)999-4445"},
{"J. Peterson", "(316)999-4446"},
};
int main (int argc, char **argv)
{
DBM *db;
datum key, value;
int i;
db = dbm_open ("yellowpage", O_RDWR | O_CREAT, 0666);
if (db == NULL)
error ("could not open the database");
/* store data into the database */
for (i = 0; i < sizeof (data) / sizeof (data[0]); i++) {
key.dptr = data[i].name;
key.dsize = strlen (data[i].name) + 1;
value.dptr = data[i].phone;
value.dsize = strlen (data[i].phone) + 1;
if (dbm_store (db, key, value, DBM_REPLACE) != 0)
fprintf (stderr, "could not store (%s, %s)\n",
data[i].name, data[i].phone);
}
/* traverse the whole database */
for(key = dbm_firstkey(db); key.dptr != NULL; key = dbm_nextkey(db)) {
value = dbm_fetch(db, key);
printf ("%s\t%s\n", key.dptr, value.dptr);
}
/* serch the value in the database whose key is specified*/
key.dptr = "J. Peterson";
key.dsize = strlen ("J. Peterson") + 1;
value = dbm_fetch (db, key);
printf ("%s\t%s\n", key.dptr, value.dptr);
/* serch the key in the database */
dbm_delete (db, key);
value = dbm_fetch (db, key);
if (value.dptr == NULL)
printf ("phone number for %s is unknown\n", key.dptr);
dbm_close (db);
}
To compile the code, you can use the following command:
% gcc db.c -o db -lgdbm
http://www.google.com/search?q=c+coding+standard