Browse Source

intial commit

Ray-works 1 year ago
commit
7c0d8d7fcd
5 changed files with 119 additions and 0 deletions
  1. 5 0
      .vscode/settings.json
  2. 69 0
      client.cpp
  3. BIN
      client.exe
  4. 27 0
      client.hpp
  5. 18 0
      main.cpp

+ 5 - 0
.vscode/settings.json

@@ -0,0 +1,5 @@
1
+{
2
+    "files.associations": {
3
+        "iostream": "cpp"
4
+    }
5
+}

+ 69 - 0
client.cpp

@@ -0,0 +1,69 @@
1
+#include "client.hpp"
2
+
3
+
4
+SOCKET &Client::getConnectSocket(){
5
+    return this->connectSocket;
6
+}
7
+
8
+void Client::setConnectSocket(SOCKET const &connectSocket){
9
+    this->connectSocket = connectSocket;
10
+}
11
+
12
+WSADATA &Client::getWSAData(){
13
+    return wsaData;
14
+}
15
+
16
+Client::Client(){
17
+    
18
+}
19
+
20
+Client::~Client(){
21
+    if (!WSACleanup() == 0) {
22
+        cout << "WSACleanup failed!" << endl;
23
+    }
24
+}
25
+
26
+// Server::operator string(){
27
+//     return this->getName() + ", v" + to_string(this->getVersion());
28
+// }
29
+
30
+void Client::connectToServer(){
31
+    if(WSAStartup(MAKEWORD(2,2),(LPWSADATA)&(this->getWSAData())) != 0){
32
+        cout << "WSAStartup() Error: " << WSAGetLastError() << endl;
33
+        exit(1);
34
+    }
35
+
36
+    memset(&this->getConnectSocket(),0,sizeof(SOCKADDR_IN));
37
+    this->setConnectSocket(socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
38
+    if (this->getConnectSocket() == INVALID_SOCKET) {
39
+        cout << "socket() Error: " << WSAGetLastError() << endl;
40
+        WSACleanup();
41
+        return;
42
+    }
43
+    SOCKADDR_IN server;
44
+    server.sin_family = AF_INET;
45
+    server.sin_port = htons(7070);
46
+    server.sin_addr.s_addr = inet_addr("127.0.0.1");
47
+
48
+    int result = connect(this->getConnectSocket(), (SOCKADDR*)&server, sizeof(SOCKADDR_IN));
49
+    if (result == SOCKET_ERROR) {
50
+        closesocket(this->getConnectSocket());
51
+        this->setConnectSocket(INVALID_SOCKET);
52
+    }
53
+}
54
+
55
+void Client::sendMessage(string const &message){
56
+    int result = send(this->getConnectSocket(), message.c_str(), (int)strlen(message.c_str()), 0);
57
+    cout << "Send bytes: " << result << endl;
58
+    
59
+    if (result == SOCKET_ERROR) {
60
+        cout << "send() Error: " << WSAGetLastError() << endl;
61
+        closesocket(this->getConnectSocket());
62
+        WSACleanup();
63
+        return;
64
+    }
65
+}
66
+
67
+void Client::disconnectFromServer(){
68
+    WSACleanup();
69
+}

BIN
client.exe


+ 27 - 0
client.hpp

@@ -0,0 +1,27 @@
1
+
2
+#pragma once
3
+#include <winsock2.h>
4
+#include <string>
5
+#include <ostream>
6
+#include <Ws2tcpip.h>
7
+#include <iostream>
8
+using namespace std;
9
+
10
+class Client {
11
+    private:
12
+        SOCKET connectSocket;
13
+        WSADATA wsaData;
14
+
15
+    public:
16
+        SOCKET &getConnectSocket();
17
+        void setConnectSocket(SOCKET const &connectSocket);
18
+
19
+        WSADATA &getWSAData();
20
+
21
+        Client();
22
+        ~Client();
23
+
24
+        void connectToServer();
25
+        void sendMessage(string const &message);
26
+        void disconnectFromServer();
27
+};

+ 18 - 0
main.cpp

@@ -0,0 +1,18 @@
1
+#include <iostream>
2
+#include <string>
3
+#include "client.hpp"
4
+
5
+using namespace std;
6
+
7
+int main(int argc, char** argv){ 
8
+    Client *c = new Client();
9
+    c->connectToServer();
10
+    for(;;){
11
+        string msg;
12
+        getline(cin, msg);
13
+        c->sendMessage(msg);
14
+    }
15
+    getchar();
16
+    c->disconnectFromServer();
17
+    return 0;
18
+}