summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2020-11-17 23:59:51 +0300
committerJoursoir <chat@joursoir.net>2020-11-17 23:59:51 +0300
commit83645ac2b31ed10aecb00d660399c0a4d3ce4df2 (patch)
treed53a7360d6d25e6e6db9f69b4d0b681d34c175f3
downloadwant-chat-83645ac2b31ed10aecb00d660399c0a4d3ce4df2.tar.gz
want-chat-83645ac2b31ed10aecb00d660399c0a4d3ce4df2.tar.bz2
want-chat-83645ac2b31ed10aecb00d660399c0a4d3ce4df2.zip
init project
-rw-r--r--.gitignore2
-rw-r--r--Makefile14
-rw-r--r--src/client/client.cpp107
3 files changed, 123 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..aea0610
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+architecture/
+*.o \ No newline at end of file
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..1c55ab6
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,14 @@
+CPP = g++
+CPPFLAGS = -Wall -g
+SOURCES = src/client/client.cpp
+OBJECTS = client.o
+
+.PHONY: all clean client
+
+all: client
+
+client:
+ g++ $(CPPFLAGS) $(SOURCES) -o client
+
+clean:
+ rm -rf src/client/client.o client \ No newline at end of file
diff --git a/src/client/client.cpp b/src/client/client.cpp
new file mode 100644
index 0000000..f09b349
--- /dev/null
+++ b/src/client/client.cpp
@@ -0,0 +1,107 @@
+#include <unistd.h> // for close
+#include <string>
+#include <cstring>
+#include <netinet/in.h> // sockaddr_in
+#include <sys/types.h> // for bind, connect
+#include <sys/socket.h> // for bind, connect
+#include <netinet/in.h> // for iten_aton, iten_ntoa
+#include <arpa/inet.h> // for iten_aton, iten_ntoa
+#include <fcntl.h> // for fcntl
+
+#define SERVER_IP "127.0.0.1"
+static int port = 7777;
+
+int main(int argc, char *argv[])
+{
+ int client = socket(AF_INET, SOCK_STREAM, 0);
+ if(client == -1) {
+ perror("socket");
+ return 1;
+ }
+
+ // remove "port sticking" aka socket in TIME_WAIT
+ int opt = 1;
+ setsockopt(client, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
+
+ /* call bind optional, the system chooses
+ the address automatically */
+
+ struct sockaddr_in server_adress;
+ server_adress.sin_family = AF_INET;
+ server_adress.sin_port = htons(port);
+ if(!inet_aton(SERVER_IP, &(server_adress.sin_addr))) {
+ perror("inet_aton");
+ return 1;
+ }
+
+ int res = connect(client, (struct sockaddr*) &server_adress,
+ sizeof(server_adress));
+ if(res == -1) {
+ perror("connect");
+ return 1;
+ }
+
+ printf("=> Connection to server %s with port number: %d\n",
+ inet_ntoa(server_adress.sin_addr), port);
+
+ // work with server
+ char buffer[1024];
+ memset(buffer, 0, sizeof(buffer)); // clear
+
+ int fd_stdin = STDIN_FILENO;
+ const int flags = fcntl(fd_stdin, F_GETFL, 0);
+ fcntl(fd_stdin, F_SETFL, flags | O_NONBLOCK);
+
+ bool exit = false;
+ do {
+ struct timeval *timeout;
+ timeout->tv_sec = 0;
+ timeout->tv_usec = 5000000;
+
+ fd_set rds, wrs;
+ FD_ZERO(&rds);
+ FD_ZERO(&wrs);
+
+ FD_SET(client, &rds);
+
+ int val = read(fd_stdin, buffer, sizeof(buffer));
+ if(val > 0) FD_SET(fd_stdin, &wrs);
+
+ res = select(client+1, &rds, &wrs, 0, timeout);
+ if(res < 0) {
+ if(errno == EINTR)
+ continue;
+ else {
+ perror("select");
+ break;
+ }
+ }
+
+ if(res > 0) {
+ if(FD_ISSET(fd_stdin, &wrs)) // if client want to send any message
+ {
+ if(buffer[0] == '#') // exit
+ exit = true;
+ else write(client, buffer, strlen(buffer)*sizeof(char));
+
+ memset(buffer, 0, sizeof(buffer)); // clear
+ }
+ if(FD_ISSET(client, &rds)) // if server want to send any message
+ {
+ read(client, buffer, sizeof(buffer));
+
+ printf("%s", buffer);
+ getchar(); /* FIXME: if you remove this thing,
+ then output will not be produced only
+ if you add '\n' to the end of the line */
+
+ memset(buffer, 0, sizeof(buffer)); // clear
+ }
+ }
+ } while (!exit);
+
+ printf("=> GoodBye...\n");
+
+ close(client);
+ return 0;
+} \ No newline at end of file