Sfoglia il codice sorgente

fixbug: windows no random

ithewei 5 anni fa
parent
commit
ee138bd9c9
1 ha cambiato i file con 4 aggiunte e 3 eliminazioni
  1. 4 3
      examples/tcp_chat_server.c

+ 4 - 3
examples/tcp_chat_server.c

@@ -45,7 +45,7 @@ void join(chatroom_t* room, connection_t* conn) {
 
     struct list_node* node;
     connection_t* cur;
-    msglen = snprintf(msg, sizeof(msg), "room[%d] clients:\r\n", room->roomid);
+    msglen = snprintf(msg, sizeof(msg), "room[%06d] clients:\r\n", room->roomid);
     hio_write(conn->connio, msg, msglen);
     list_for_each (node, &room->conns) {
         cur = list_entry(node, connection_t, node);
@@ -54,7 +54,7 @@ void join(chatroom_t* room, connection_t* conn) {
     }
     hio_write(conn->connio, "\r\n", 2);
 
-    msglen = snprintf(msg, sizeof(msg), "client[%s] join room[%d]\r\n", conn->addr, room->roomid);
+    msglen = snprintf(msg, sizeof(msg), "client[%s] join room[%06d]\r\n", conn->addr, room->roomid);
     broadcast(room, msg, msglen);
 }
 
@@ -142,7 +142,8 @@ int main(int argc, char** argv) {
 
     s_chatroom.loop = loop;
     s_chatroom.listenio = listenio;
-    s_chatroom.roomid = random() % 1000000;
+    srand(time(NULL));
+    s_chatroom.roomid = rand() % 1000000;
     list_init(&s_chatroom.conns);
 
     hloop_run(loop);