Browse Source

communication between client and server fixed. Nickname is now required for broadcasting messages.

Ray-works 1 year ago
parent
commit
ddeaed5ec0
3 changed files with 27 additions and 15 deletions
  1. 25 12
      client.cpp
  2. 2 3
      data.cpp
  3. BIN
      server.exe

+ 25 - 12
client.cpp

@@ -47,7 +47,7 @@ Client::Client(): Client(0){}
47 47
 
48 48
 Client::Client(SOCKET const &socket){
49 49
     this->setSocket(socket);
50
-    this->setName("Unknown");
50
+    this->setName("");
51 51
     this->setIP("0.0.0.0");
52 52
     this->setPort(0);
53 53
 }
@@ -89,22 +89,35 @@ void Client::startWorkThread(Server *server){
89 89
                 this->setName(name);
90 90
             }
91 91
 
92
-            if(strcmp(data->getCmd(), "msg") == 0){
93
-                for(Client *client : server->getClients()){
94
-                    if(client->getSocket() != this->getSocket()){
95
-                        if(client->getSocket() != SOCKET_ERROR && client->getSocket() != INVALID_SOCKET){
96
-                            string msg = this->getName() + ": " + data->getContext();
97
-                            cout << msg << endl;
98
-                            int sendResult = send(client->getSocket(), msg.c_str(), msg.size(), 0);
99
-                            if(sendResult == SOCKET_ERROR){
100
-                                cout << "send() Error: " << WSAGetLastError() << endl;
92
+            if(strcmp(this->getName().c_str(), "") != 0){
93
+                if(strcmp(data->getContext(), "") != 0){
94
+                    if(strcmp(data->getCmd(), "msg") == 0){
95
+                        for(Client *client : server->getClients()){
96
+                            if(client->getSocket() != this->getSocket()){
97
+                                if(client->getSocket() != SOCKET_ERROR && client->getSocket() != INVALID_SOCKET){
98
+                                    string msg = this->getName() + ": " + data->getContext();
99
+                                    int sendResult = send(client->getSocket(), msg.c_str(), msg.size(), 0);
100
+                                    if(sendResult == SOCKET_ERROR){
101
+                                        cout << "send() Error: " << WSAGetLastError() << endl;
102
+                                    }
103
+                                }
101 104
                             }
102 105
                         }
103 106
                     }
107
+                } else {
108
+                    string msg = "err#Empty message.";
109
+                    int sendResult = send(this->getSocket(), msg.c_str(), msg.size(), 0);
110
+                    if(sendResult == SOCKET_ERROR){
111
+                        cout << "send() Error: " << WSAGetLastError() << endl;
112
+                    }
113
+                }
114
+            } else {
115
+                string msg = "err#Set a nickname first before sending any messages.";
116
+                int sendResult = send(this->getSocket(), msg.c_str(), msg.size(), 0);
117
+                if(sendResult == SOCKET_ERROR){
118
+                    cout << "send() Error: " << WSAGetLastError() << endl;
104 119
                 }
105 120
             }
106
-
107
-            delete data;
108 121
         } else  {
109 122
             switch(WSAGetLastError()){
110 123
                 case WSAECONNRESET: {

+ 2 - 3
data.cpp

@@ -44,12 +44,11 @@ void Data::parse(){
44 44
         strcpy(cmd, token);
45 45
         this->setCmd(token);
46 46
 
47
-        token = strtok(NULL, "\x01");
48
-        if(token != NULL){
47
+        if((token = strtok(NULL, "\x01")) != NULL){
49 48
             char *context = new char[strlen(token)];
50 49
             strcpy(context, token);
51 50
             this->setContext(context);
52 51
         }
53 52
     }
54
-    delete token;
53
+    //delete token;
55 54
 }

BIN
server.exe