Advertisement
bebesurf

2 servers many clients with data

May 20th, 2019
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.55 KB | None | 0 0
  1. package main
  2. // This is a parot server
  3. // January 2018, JCA, IUT, University of Nnantes
  4. // VERSION QUI RENVOIE UN NOMBRE EN FONCTION DE CE QU'IL Y A DANS LA MAP
  5.  
  6. import (
  7.     "net"  // for net/socket resources
  8.     "os"   // for OS resources
  9.     "fmt"  // general formatting
  10.     //"strconv" // Convert str to int or int to
  11.     // "bufio" // intput output package
  12.     )
  13.  
  14. //--------------------------------------------
  15. func interactWithClient(commSock net.Conn) {
  16.     myMap := map[string]string{
  17.         "1":"Salut",
  18.         "2":"Comment va ?",
  19.         "3":"Bien et toi ?",
  20.         "4":"Travail terminé !",
  21.         "42":"BIEN JOUE !",
  22.     }
  23.     // send a Agreement msg to the client
  24.     writelen, err := commSock.Write([]byte("Serv ACK! > Ready to receive\n"))
  25.     if err != nil {
  26.         fmt.Fprintf(os.Stderr, "Could not write to socket: %s\n", err)
  27.     } else {
  28.         fmt.Printf("Wrote %d bytes to socket\n", writelen)
  29.     }
  30.  
  31.     for ;; { // Server loop to interact with the client
  32.         buf := make([]byte, 256) // for buffering // but redecl :-(
  33.         bufLen, err := commSock.Read(buf) // try a read  buffer from the Sock
  34.         if err != nil {
  35.             fmt.Fprintf(os.Stderr, "Error when reading from socket: %s\n", err)
  36.             return
  37.         }
  38.         if bufLen == 0 {
  39.             fmt.Printf("Connection closed by remote host\n")
  40.             return
  41.         }
  42.         fmt.Printf("Length of read buffer = %d\n",bufLen)
  43.         buf = buf[:bufLen-2]  // this is to ignore the 2 '\n', counting from 0
  44.         fmt.Printf("Client at %s says %s \n", commSock.RemoteAddr(), buf)
  45.         // prepare the response
  46.         resp := ""
  47.         num := string(buf)      // Cast the buff to string
  48.         if myMap[num] == "" {
  49.             resp = "ANS> No value matches"+"\n"
  50.         } else {
  51.             resp = "ANS>"+myMap[num]+"\n"              
  52.         }
  53.         // send a 'response' to the client via the same connScock
  54.         writtenLen, err := commSock.Write([]byte(resp))
  55.         if err != nil {
  56.            fmt.Fprintf(os.Stderr, "Could not write to socket: %s\n", err)
  57.            } else {
  58.            fmt.Printf("Wrote %d bytes to socket\n", writtenLen)
  59.        }
  60.    }//for
  61. }
  62.  
  63. func listenForClients(ip string, port string) {
  64.     sockListener, err := net.Listen("tcp", ip + ":" + port)
  65.     if err != nil {
  66.         fmt.Fprintf(os.Stderr, "Could not listen on socket: %s\n", err)
  67.         return
  68.     }
  69.     for ;;{     // Allows to accept many clients
  70.         // try to accept clients on the connection sockect => communic socket
  71.         commSock, err := sockListener.Accept()  // wait until a Dial from a client
  72.         if err != nil {
  73.             fmt.Fprintf(os.Stderr, "Could not accept connection on socket: %s\n", err)
  74.             return
  75.         }
  76.         resp := "BONJOUR, Vous êtes sur le serveur " + ip + ":" + port + "\n"
  77.         _, _ = commSock.Write([]byte(resp))        
  78.         if port == "2056"{
  79.             interactWithClient(commSock) // interact wih a client in case of a 'Dial'. Launch and continue the for loop
  80.         } else {
  81.             go interactWithClient(commSock) // interact wih a client in case of a 'Dial'. Launch and continue the for loop
  82.         }
  83.     }
  84. }
  85.  
  86. //------------------------------
  87. func main() {
  88.     // attempt to create a connection sock
  89.     // listening at a port of a machine : localhost:port
  90.    
  91.  
  92.     go listenForClients("172.28.11.42","2056")
  93.     listenForClients("172.28.11.42","2057")
  94.    
  95.    
  96. }
  97.  
  98. //-----------------------------
  99. //  to run the server after compilation, from an xterm  : ./sockServer_1
  100. //
  101. //  to interact from client side : telnet localhost 2056
  102. //----------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement