|
@@ -92,9 +92,10 @@ static int init_socket(const char *server_addr) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-static int send_msg(const char *msg) {
|
|
|
|
|
|
|
+static int send_msg(const char *msg, const char *usrname) {
|
|
|
struct vcc_request req;
|
|
struct vcc_request req;
|
|
|
|
|
|
|
|
|
|
+ strcpy(req.usrname, usrname);
|
|
|
strcpy(req.msg, msg);
|
|
strcpy(req.msg, msg);
|
|
|
write(fd, &req, REQ_SIZE);
|
|
write(fd, &req, REQ_SIZE);
|
|
|
|
|
|
|
@@ -102,18 +103,8 @@ static int send_msg(const char *msg) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-static int read_msg(char *buf) {
|
|
|
|
|
- struct vcc_request req;
|
|
|
|
|
- int n;
|
|
|
|
|
-
|
|
|
|
|
- n = read(fd, &req, REQ_SIZE);
|
|
|
|
|
-
|
|
|
|
|
- if (n < 0)
|
|
|
|
|
- return n;
|
|
|
|
|
-
|
|
|
|
|
- memcpy(buf, req.msg, MSG_SIZE);
|
|
|
|
|
-
|
|
|
|
|
- return 0;
|
|
|
|
|
|
|
+static int read_msg(struct vcc_request *buf) {
|
|
|
|
|
+ return read(fd, buf, REQ_SIZE);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -141,7 +132,9 @@ static int setup_alarm(void) {
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, const char **argv) {
|
|
int main(int argc, const char **argv) {
|
|
|
- char buf[REQ_SIZE];
|
|
|
|
|
|
|
+ struct vcc_request req;
|
|
|
|
|
+ char *buf;
|
|
|
|
|
+ char usrname[USRNAME_SIZE];
|
|
|
int pid;
|
|
int pid;
|
|
|
|
|
|
|
|
if (unlikely(argc != 1 && argc != 2))
|
|
if (unlikely(argc != 1 && argc != 2))
|
|
@@ -152,6 +145,8 @@ int main(int argc, const char **argv) {
|
|
|
init_socket(argv[1] ? argv[1] : "124.223.105.230");
|
|
init_socket(argv[1] ? argv[1] : "124.223.105.230");
|
|
|
|
|
|
|
|
printf("Connected to server. \n");
|
|
printf("Connected to server. \n");
|
|
|
|
|
+ fprintf(stderr, "login as: ");
|
|
|
|
|
+ scanf("%s", usrname);
|
|
|
|
|
|
|
|
if ((pid = fork())) {
|
|
if ((pid = fork())) {
|
|
|
/* receive messages */
|
|
/* receive messages */
|
|
@@ -159,26 +154,35 @@ int main(int argc, const char **argv) {
|
|
|
setup_alarm();
|
|
setup_alarm();
|
|
|
|
|
|
|
|
for (;;) {
|
|
for (;;) {
|
|
|
- memset(buf, 0, REQ_SIZE);
|
|
|
|
|
-
|
|
|
|
|
- if (read_msg(buf) == -1) {
|
|
|
|
|
|
|
+ if (read_msg(&req) == -1) {
|
|
|
printf("Server closed\n");
|
|
printf("Server closed\n");
|
|
|
|
|
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ buf = req.msg;
|
|
|
|
|
+
|
|
|
if (!*buf || *(unsigned char *) buf == 0xffu)
|
|
if (!*buf || *(unsigned char *) buf == 0xffu)
|
|
|
continue;
|
|
continue;
|
|
|
|
|
|
|
|
- printf("New message: %s\n", buf);
|
|
|
|
|
|
|
+ printf("\n@%s: %s", req.usrname, buf);
|
|
|
|
|
+ fprintf(stderr, "%s$ ", usrname);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- else
|
|
|
|
|
|
|
+ else {
|
|
|
|
|
+ if (unlikely(!(buf = malloc(MSG_SIZE)))) {
|
|
|
|
|
+ fprintf(stderr, "*** malloc() failed\n");
|
|
|
|
|
+
|
|
|
|
|
+ goto exit;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
for (;;) {
|
|
for (;;) {
|
|
|
- memset(buf, 0, REQ_SIZE);
|
|
|
|
|
|
|
+ memset(buf, 0, MSG_SIZE);
|
|
|
|
|
|
|
|
- if (read(0, buf, REQ_SIZE) <= 0) {
|
|
|
|
|
|
|
+ fprintf(stderr, "%s$ ", usrname);
|
|
|
|
|
+
|
|
|
|
|
+ if (read(0, buf, MSG_SIZE) <= 0) {
|
|
|
fprintf(stderr, "--- read() failed. \n");
|
|
fprintf(stderr, "--- read() failed. \n");
|
|
|
|
|
|
|
|
continue;
|
|
continue;
|
|
@@ -187,12 +191,15 @@ int main(int argc, const char **argv) {
|
|
|
if (!strcmp(buf, "quit"))
|
|
if (!strcmp(buf, "quit"))
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
- send_msg(buf);
|
|
|
|
|
|
|
+ send_msg(buf, usrname);
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- close(fd);
|
|
|
|
|
|
|
+exit: close(fd);
|
|
|
kill(pid, 9);
|
|
kill(pid, 9);
|
|
|
|
|
|
|
|
|
|
+ printf("exited\n");
|
|
|
|
|
+
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|