aboutsummaryrefslogtreecommitdiff
path: root/client.c
diff options
context:
space:
mode:
Diffstat (limited to 'client.c')
-rw-r--r--client.c33
1 files changed, 32 insertions, 1 deletions
diff --git a/client.c b/client.c
index 82c35a6..9e3a421 100644
--- a/client.c
+++ b/client.c
@@ -1,11 +1,19 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <strings.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdbool.h>
+#include <stdio.h>
+
+#include "common.h"
int
main()
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
+ if (sockfd == -1)
+ panic("socket creation error");
struct sockaddr_in serv_addr;
bzero(&serv_addr, sizeof(serv_addr));
@@ -13,5 +21,28 @@ main()
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
serv_addr.sin_port = htons(8888);
- connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
+ if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1)
+ panic("socket connect error");
+
+ while (true) {
+ char buf[1024];
+ bzero(&buf, sizeof(buf));
+ scanf("%s", buf);
+ ssize_t n_write_bytes = write(sockfd, buf, sizeof(buf));
+ if (n_write_bytes == -1) {
+ printf("socket already disconnected, cannot write any more!\n");
+ break;
+ }
+
+ ssize_t n_read_bytes = read(sockfd, buf, sizeof(buf));
+ if (n_read_bytes > 0) {
+ printf("message from server: %s\n", buf);
+ } else if (n_read_bytes == 0) {
+ printf("server socket disconnected!\n");
+ break;
+ } else if (n_read_bytes == -1) {
+ close(sockfd);
+ panic("socket read error");
+ }
+ }
}