Index: server/server.cpp
===================================================================
--- server/server.cpp	(revision b53c6b345affaea1c625ed8caa8faf8df17f5992)
+++ server/server.cpp	(revision 3b1efcc97deda61383fe1470d63eb56b0e172045)
@@ -4,4 +4,5 @@
 #include <string>
 #include <iostream>
+#include <sstream>
 #include <vector>
 #include <algorithm>
@@ -21,4 +22,5 @@
 #include "../common/Compiler.h"
 #include "../common/Message.h"
+#include "../common/Common.h"
 
 #include "Player.h"
@@ -27,5 +29,5 @@
 using namespace std;
 
-void processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, int &num, NETWORK_MSG &serverMsg);
+bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg);
 
 // this should probably go somewhere in the common folder
@@ -63,24 +65,11 @@
 }
 
-void set_nonblock(int sock)
-{
-    int flags;
-    flags = fcntl(sock, F_GETFL,0);
-    assert(flags != -1);
-    fcntl(sock, F_SETFL, flags | O_NONBLOCK);
-}
-
 int main(int argc, char *argv[])
 {
    int sock, length, n;
    struct sockaddr_in server;
-   struct sockaddr_in from; // holds the info about the connected client
+   struct sockaddr_in from; // info of client sending the message
    NETWORK_MSG clientMsg, serverMsg;
    vector<Player> vctPlayers;
-
-   srand(time(NULL));
-   int num = (rand() % 1000) + 1;
-
-   cout << "num: " << num << endl;
 
    SSL_load_error_strings();
@@ -105,4 +94,5 @@
    set_nonblock(sock);
 
+   bool broadcastMessage;
    while (true) {
 
@@ -114,11 +104,23 @@
          cout << "Got a message" << endl;
 
-         processMessage(clientMsg, from, vctPlayers, num, serverMsg);
+         broadcastMessage = processMessage(clientMsg, from, vctPlayers, serverMsg);
 
          cout << "msg: " << serverMsg.buffer << endl;
 
-         n = sendMessage(&serverMsg, sock, &from);
-         if (n  < 0)
-            error("sendMessage");
+         if (broadcastMessage)
+         {
+            vector<Player>::iterator it;
+
+            for (it = vctPlayers.begin(); it != vctPlayers.end(); it++)
+            {
+               if ( sendMessage(&serverMsg, sock, &(it->addr)) < 0 )
+                  error("sendMessage");
+            }
+         }
+         else
+         {
+            if ( sendMessage(&serverMsg, sock, &from) < 0 )
+               error("sendMessage");
+         }
       }
 
@@ -128,5 +130,5 @@
 }
 
-void processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, int &num, NETWORK_MSG &serverMsg)
+bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, NETWORK_MSG &serverMsg)
 {
    DataAccess da;
@@ -136,4 +138,6 @@
    cout << "MSG: type: " << clientMsg.type << endl;
    cout << "MSG contents: " << clientMsg.buffer << endl;
+
+   bool broadcastResponse = false;
 
    // Check that if an invalid message is sent, the client will correctly
@@ -149,7 +153,10 @@
          cout << "password: " << password << endl;
 
-         da.insertPlayer(username, password);
-
-         strcpy(serverMsg.buffer, "Registration successful");
+         int error = da.insertPlayer(username, password);
+
+         if (!error)
+            strcpy(serverMsg.buffer, "Registration successful.");
+         else
+            strcpy(serverMsg.buffer, "Registration failed. Please try again.");
 
          serverMsg.type = MSG_TYPE_REGISTER;
@@ -179,5 +186,5 @@
 
             vctPlayers.push_back(newP);
-            strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is.");
+            strcpy(serverMsg.buffer, "Login successful. Enjoy chatting with outher players.");
          }
 
@@ -222,18 +229,10 @@
          else
          {
-            int guess = atoi(clientMsg.buffer);
-
-            cout << "guess: " << guess << endl;
-
-            if (guess < 1 || guess > 1000) {
-               strcpy(serverMsg.buffer, "You must guess a number between 1 and 1000");
-            }else if(guess > num)
-               strcpy(serverMsg.buffer, "The number I'm thinking of is less than that.");
-            else if(guess < num)
-               strcpy(serverMsg.buffer, "The number I'm thinking of is greater than that.");
-            else if(guess == num) {
-               strcpy(serverMsg.buffer, "Congratulations! I will now think of a new number.");
-               num = (rand() % 1000) + 1;
-            }
+            broadcastResponse = true;
+
+            stringstream ss;
+            ss << p->name << ": " << clientMsg.buffer << endl;
+
+            strcpy(serverMsg.buffer, ss.str().c_str());
          }	
 
@@ -250,4 +249,6 @@
          break;
       }
-   }
-}
+
+      return broadcastResponse;
+   }
+}
