diff options
author | LAN-TW <lantw44@gmail.com> | 2013-11-10 22:40:32 +0800 |
---|---|---|
committer | LAN-TW <lantw44@gmail.com> | 2013-11-10 22:40:32 +0800 |
commit | 731a3ed2ab7f6197dba5a2515b31ff698c4ead97 (patch) | |
tree | 0e9f824752fd2a3bf34d5e364085e0dfb5217de2 | |
parent | dd3aacf95750461bf041abda556ad405db980361 (diff) | |
download | cn2013-731a3ed2ab7f6197dba5a2515b31ff698c4ead97.tar.gz cn2013-731a3ed2ab7f6197dba5a2515b31ff698c4ead97.tar.zst cn2013-731a3ed2ab7f6197dba5a2515b31ff698c4ead97.zip |
HW1: RasClient 現在已經可以登入,但選擇使用者的功能尚未實作
-rw-r--r-- | hw1/client-main.c | 21 | ||||
-rw-r--r-- | hw1/socktool.c | 4 | ||||
-rw-r--r-- | hw1/socktool.h | 14 |
3 files changed, 26 insertions, 13 deletions
diff --git a/hw1/client-main.c b/hw1/client-main.c index 5e301d9..c1c8dc4 100644 --- a/hw1/client-main.c +++ b/hw1/client-main.c @@ -10,6 +10,7 @@ #include <inttypes.h> #include <locale.h> #include <netdb.h> +#include <netinet/in.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> @@ -22,11 +23,15 @@ #include <time.h> #include <unistd.h> +static int client_rw_cb (int fd[2], RasBuffer buf[2]) { + return true; +} + int main (int argc, char* argv[]) { setlocale (LC_ALL, ""); tzset (); - const char* logincmd = isatty (STDIN_FILENO) ? "LOGINTTY" : "LOGIN"; + const char* logincmd = isatty (STDIN_FILENO) ? "LOGINTTY\n" : "LOGIN\n"; const char* conn[2] = { NULL, NULL }; bool allowv4 = true; bool allowv6 = true; @@ -36,10 +41,10 @@ int main (int argc, char* argv[]) { for (int j = 1; argv[i][j] != '\0'; j++) { switch (argv[i][j]) { case 't': - logincmd = "LOGINTTY"; + logincmd = "LOGINTTY\n"; break; case 'T': - logincmd = "LOGIN"; + logincmd = "LOGIN\n"; break; case 'I': allowv4 = true; @@ -115,7 +120,7 @@ int main (int argc, char* argv[]) { hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; - hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG; + hints.ai_flags = AI_ADDRCONFIG; int gaierr = getaddrinfo (conn[0], conn[1], &hints, &result); if (gaierr != 0) { @@ -204,5 +209,13 @@ int main (int argc, char* argv[]) { return 3; } + ras_socktool_write_string (sockfd, logincmd, 0); + + int fd[2] = { STDIN_FILENO, sockfd }; + ras_socktool_exchange_data (fd, client_rw_cb); + + shutdown (sockfd, SHUT_RDWR); + close (sockfd); + return 0; } diff --git a/hw1/socktool.c b/hw1/socktool.c index 6c313ad..3f2eb85 100644 --- a/hw1/socktool.c +++ b/hw1/socktool.c @@ -142,11 +142,11 @@ char* ras_socktool_getline (int sockfd, RasBuffer* buf, int delim, int* len) { } int ras_socktool_write_string (int sockfd, const char* str, size_t size) { - size_t wtn = 0; - size_t rem = size; + ssize_t wtn = 0; if (size <= 0) { size = strlen (str); } + size_t rem = size; while (rem > 0) { wtn = write (sockfd, str, rem); if (wtn < 0) { diff --git a/hw1/socktool.h b/hw1/socktool.h index ebcc2d5..b3ccd0c 100644 --- a/hw1/socktool.h +++ b/hw1/socktool.h @@ -16,13 +16,13 @@ char* ras_socktool_get_peername (int sockfd); #define RAS_BUFFER_SIZE 8192 typedef struct { - char buf[RAS_BUFFER_SIZE]; - off_t buf_start; - off_t buf_len; - char* buf_line; - size_t buf_line_len; - int buf_error : 1; - int buf_eof : 1; + char buf[RAS_BUFFER_SIZE]; + off_t buf_start; + off_t buf_len; + char* buf_line; + ssize_t buf_line_len; + int buf_error : 1; + int buf_eof : 1; } RasBuffer; void ras_socktool_buffer_clear (RasBuffer* buf, bool initial); |