1. Cédric Bonhomme
  2. GoTalk

Commits

Cédric Bonhomme  committed bb91f93

The client uses the same connection to send and receive message.

  • Participants
  • Parent commits b62a6b6
  • Branches default

Comments (0)

Files changed (1)

File client.go

View file
 }
 
 
-func ServerHandler(conn net.Conn) {
-    buffer := make([]byte, 1024)
-    for {
-        bytesRead, error := conn.Read(buffer)
-        if error != nil {
-            Log("connection error: ", error)
-        }
-        message := string(buffer[0:bytesRead])
-        fmt.Print(message)
-    }
+func ServerHandler(connection net.Conn) {
+    // Wait for messages from the server
+
+            for {
+                        buffer := make([]byte, 1024)
+                        bytesRead, error := connection.Read(buffer)
+                        if error != nil {
+                            Log("connection error: ", error)
+                        }
+                        message := string(buffer[0:bytesRead])
+                        fmt.Print(message)
+            }
 }
 
 // Main: connects to a TCP server and ask to the client for inputs
         return // terminate program
     }
 
-    // Wait for messages from the server
-    tcpAddr, error := net.ResolveTCPAddr("tcp", "localhost:1024")
-    netListen, error := net.Listen(tcpAddr.Network(), tcpAddr.String())
-    connection, error := netListen.Accept()
-    go ServerHandler(connection)
+    go ServerHandler(conn)
 
     inputReader := bufio.NewReader(os.Stdin)
     fmt.Println("Please give yourname. Type Q to quit.")