Browse Source

update 30.05.2020

Ray-works 1 year ago
parent
commit
49436258c6
5 changed files with 88 additions and 80 deletions
  1. 83 17
      client.cpp
  2. 2 2
      client.hpp
  3. 0 2
      main.cpp
  4. 3 59
      server.cpp
  5. BIN
      server.exe

+ 83 - 17
client.cpp

@@ -62,12 +62,12 @@ Client::operator string(){
62 62
     return this->getIP() + " (" + to_string(this->getPort()) + ")";
63 63
 }
64 64
 
65
-void Client::startWorkingThread(Server *server){
65
+void Client::startWorkThread(Server *server){
66 66
     char *clientIpAddress = inet_ntoa(this->getSocketInfo().sin_addr);
67 67
     int clientPort = ntohs(this->getSocketInfo().sin_port); 
68 68
 
69 69
     if(strcmp("0", clientIpAddress) == 0 || clientPort == 0){
70
-        cout << "No valid this ip address and/or port! Disconnecting..." << endl;
70
+        cout << "No valid ip address and/or port! Disconnecting..." << endl;
71 71
         closesocket(this->getSocket());
72 72
         return;
73 73
     }
@@ -77,24 +77,91 @@ void Client::startWorkingThread(Server *server){
77 77
 
78 78
     cout << "[New Connection] " << clientIpAddress << " (" << clientPort << ")" << endl;
79 79
 
80
-    char msgBuffer[MSG_BUFFER_SIZE];
80
+    // do {
81
+    //     char *headerBuffer = new char[5];
82
+    //     int result = recv(this->getSocket(), headerBuffer, 5, MSG_WAITALL);
83
+
84
+    //     if (result > 0) {
85
+    //         unsigned int opcode = (unsigned char) headerBuffer[0]; // value: 0-255 
86
+    //         const unsigned int dataLength = *(unsigned int *)(headerBuffer + 1);
87
+
88
+    //         cout << "[Header] We received " << result << " bytes in total." << endl;
89
+    //         cout << "Opcode is: " << opcode << " and dataLength (which will be send) is: " << dataLength << endl;
90
+
91
+    //         char *dataBuffer = new char[dataLength];
92
+
93
+    //         int result = recv(this->getSocket(), dataBuffer, dataLength, MSG_WAITALL);
94
+    //         if(result > 0){
95
+    //             cout << "[Data] We received " << result << "bytes in total." << endl;
96
+
97
+    //             if(opcode == 55){
98
+    //                 cout << "ohhh opcode 55 received!" << endl;
99
+    //                 cout << dataBuffer << endl;
100
+    //             }
101
+
102
+    //             // for(Client *client : server->getClients()){
103
+    //             //     if(client->getSocket() != this->getSocket()){
104
+    //             //         if(client->getSocket() != SOCKET_ERROR && client->getSocket() != INVALID_SOCKET){
105
+    //             //             int sendResult = send(client->getSocket(), dataBuffer, result, 0);
106
+    //             //             if (sendResult == SOCKET_ERROR) {
107
+    //             //                 cout << "send() Error: " << WSAGetLastError() << endl;
108
+    //             //             }
109
+    //             //         }
110
+    //             //     }
111
+    //             // }
112
+    //         }
113
+    //         delete[] dataBuffer;
114
+    //     } else  {
115
+    //         switch(WSAGetLastError()){
116
+    //             case WSAECONNRESET: {
117
+    //                 cout << "Connection reset by peer." << endl; break;
118
+    //             }
119
+    //             case WSAECONNABORTED: {
120
+    //                 cout << "Software caused connection abort." << endl; break;
121
+    //             }
122
+    //             case WSAESHUTDOWN: {
123
+    //                 cout << "The client socket has been shut down." << endl; break;
124
+    //             }
125
+    //             case WSAETIMEDOUT: {
126
+    //                 cout << "The connection has been dropped because of a network failure or because the peer system failed to respond." << endl; break;
127
+    //             }
128
+    //         }
129
+
130
+    //         closesocket(this->getSocket());
131
+    //         this->setSocket(SOCKET_ERROR);
132
+    //     }
133
+        
134
+    //     delete[] headerBuffer;
135
+    // } while (this->getSocket() != INVALID_SOCKET && this->getSocket() != SOCKET_ERROR);
81 136
 
82
-    int result = 0;
83 137
     do {
84
-        memset(msgBuffer, 0, MSG_BUFFER_SIZE);
85
-
86
-        int result = recv(this->getSocket(), msgBuffer, MSG_BUFFER_SIZE, 0);
138
+        char dataBuffer[MSG_BUFFER_SIZE];
139
+        int result = recv(this->getSocket(), dataBuffer, sizeof(dataBuffer), 0);
87 140
         if (result > 0) {
88
-
89
-            cout << msgBuffer << endl;
90
-
91
-            int sendResult = send(this->getSocket(), msgBuffer, result, 0 );
92
-            if (sendResult == SOCKET_ERROR) {
93
-                cout << "send() Error: " << WSAGetLastError() << endl;
94
-                closesocket(this->getSocket());
95
-                this->setSocket(SOCKET_ERROR);
96
-                break;
141
+            char *token = strtok(dataBuffer, "\x01");
142
+            if(token != NULL){
143
+                if(strcmp(token, "msg") == 0){
144
+                    // do something
145
+                    cout << "cmd: " << token << endl;
146
+                
147
+                    token = strtok(NULL, "\x01");
148
+                    cout << token << endl;
149
+                }                
97 150
             }
151
+
152
+            // unsigned int opcode = (unsigned char) dataBuffer[0]; // value: 0-255 
153
+            // const unsigned int dataLength = *(unsigned int *)(dataBuffer + 1);
154
+
155
+            // for(Client *client : server->getClients()){
156
+            //     if(client->getSocket() != this->getSocket()){
157
+            //         if(client->getSocket() != SOCKET_ERROR && client->getSocket() != INVALID_SOCKET){
158
+            //             int sendResult = send(client->getSocket(), dataBuffer, result, 0);
159
+            //             if (sendResult == SOCKET_ERROR) {
160
+            //                 cout << "send() Error: " << WSAGetLastError() << endl;
161
+            //             }
162
+            //         }
163
+            //     }
164
+            // }
98 165
         } else  {
99 166
             switch(WSAGetLastError()){
100 167
                 case WSAECONNRESET: {
@@ -114,7 +181,6 @@ void Client::startWorkingThread(Server *server){
114 181
             closesocket(this->getSocket());
115 182
             this->setSocket(SOCKET_ERROR);
116 183
         }
117
-
118 184
     } while (this->getSocket() != INVALID_SOCKET && this->getSocket() != SOCKET_ERROR);
119 185
 
120 186
     closesocket(this->getSocket());

+ 2 - 2
client.hpp

@@ -7,7 +7,7 @@
7 7
 #include <Ws2tcpip.h>
8 8
 #include <iostream>
9 9
 #include <thread>
10
-#define MSG_BUFFER_SIZE 1024
10
+#define MSG_BUFFER_SIZE 4096
11 11
 using namespace std;
12 12
 
13 13
 class Server;
@@ -49,7 +49,7 @@ class Client {
49 49
         const Client &operator=(const Client&) = delete;
50 50
         const Client &operator=(Client&&) = delete;
51 51
 
52
-        void startWorkingThread(Server *server);
52
+        void startWorkThread(Server *server);
53 53
 };
54 54
 
55 55
 inline ostream &operator<<(ostream &os, Client &client){

+ 0 - 2
main.cpp

@@ -6,8 +6,6 @@ using namespace std;
6 6
 
7 7
 int main(int argc, char** argv){
8 8
     Server *s = new Server();
9
-
10 9
     s->start();
11
-    
12 10
     return 0;
13 11
 }

+ 3 - 59
server.cpp

@@ -118,7 +118,7 @@ void Server::start(){
118 118
     }
119 119
 
120 120
     cout << "Server started and listen now!" << endl;
121
-    cout << "Waiting for client..." << endl;
121
+    cout << "Waiting for clients..." << endl;
122 122
 
123 123
     for(;;){
124 124
         Client *client = new Client();
@@ -133,69 +133,13 @@ void Server::start(){
133 133
         }
134 134
 
135 135
         cout << "Socket is valid, starting client thread..." << endl;
136
+        this->getClients().push_back(client);
136 137
 
137 138
         client->setSocket(clientSocket);
138
-        client->setWorkThread(new thread(&Client::startWorkingThread, client, this));
139
+        client->setWorkThread(new thread(&Client::startWorkThread, client, this));
139 140
     }
140 141
 }
141 142
 
142
-// void Server::clientThread(Client *client){
143
-//     char *clientIpAddress = inet_ntoa(client->getSocketInfo().sin_addr);
144
-//     int clientPort = ntohs(client->getSocketInfo().sin_port); 
145
-
146
-//     if(strcmp("0", clientIpAddress) == 0 || clientPort == 0){
147
-//         cout << "No valid client ip address and/or port! Disconnecting..." << endl;
148
-//         closesocket(client->getSocket());
149
-//         return;
150
-//     }
151
-
152
-//     client->setIP(clientIpAddress);
153
-//     client->setPort(clientPort);
154
-
155
-//     cout << "[New Connection] " << clientIpAddress << " (" << clientPort << ")" << endl;
156
-
157
-//     char msgBuffer[MSG_BUFFER_SIZE];
158
-
159
-//     int result = 0;
160
-//     do {
161
-//         memset(msgBuffer, 0, MSG_BUFFER_SIZE);
162
-
163
-//         int result = recv(client->getSocket(), msgBuffer, MSG_BUFFER_SIZE, 0);
164
-//         if (result > 0) {
165
-
166
-//             cout << msgBuffer << endl;
167
-
168
-//             int sendResult = send(client->getSocket(), msgBuffer, result, 0 );
169
-//             if (sendResult == SOCKET_ERROR) {
170
-//                 cout << "send() Error: " << WSAGetLastError() << endl;
171
-//                 closesocket(client->getSocket());
172
-//                 client->setSocket(SOCKET_ERROR);
173
-//                 break;
174
-//             }
175
-//         } else  {
176
-//             switch(WSAGetLastError()){
177
-//                 case WSAECONNRESET: {
178
-//                     cout << "Connection reset by peer." << endl; break;
179
-//                 }
180
-//                 case WSAECONNABORTED: {
181
-//                     cout << "Software caused connection abort." << endl; break;
182
-//                 }
183
-//                 case WSAESHUTDOWN: {
184
-//                     cout << "The client socket has been shut down." << endl; break;
185
-//                 }
186
-//                 case WSAETIMEDOUT: {
187
-//                     cout << "The connection has been dropped because of a network failure or because the peer system failed to respond." << endl; break;
188
-//                 }
189
-//             }
190
-
191
-//             closesocket(client->getSocket());
192
-//             client->setSocket(SOCKET_ERROR);
193
-//         }
194
-
195
-//     } while (client->getSocket() != INVALID_SOCKET && client->getSocket() != SOCKET_ERROR);
196
-
197
-//     closesocket(client->getSocket());
198
-// }
199 143
 
200 144
 void Server::stop(){
201 145
     for(auto it = this->getClients().begin(); it != this->getClients().end(); it++){

BIN
server.exe