SHARE
TWEET

Untitled

a guest May 22nd, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "net"
  5.     "os"
  6.     "fmt"
  7.     )
  8.  
  9. func gogopowerrangers(commSock net.Conn) {
  10.  
  11.  
  12.   for ;; { // Server loop to interact with the client
  13.       buf := make([]byte, 256) // for buffering // but redecl :-(
  14.       bufLen, err := commSock.Read(buf) // try a read  buffer from the Sock
  15.       if err != nil {
  16.           fmt.Fprintf(os.Stderr, "Error when reading from socket: %s\n", err)
  17.           return
  18.       }
  19.       if bufLen == 0 {
  20.           fmt.Printf("Connection closed by remote host\n")
  21.           return
  22.       }
  23.       hashMap := map[string]string {
  24.         "mdp": "Connexion rĂ©ussie",
  25.       }
  26.       buf = buf[:bufLen-1]  // this is to ignore '\n', counting from 0
  27.       s := string(buf)
  28.       reponse := "Mauvais mot de passe"
  29.  
  30.       if val, ok := hashMap[s]; ok {
  31.         reponse = val
  32.       }
  33.       //fmt.Printf("Length of read buffer = %s\n",s)
  34.       //fmt.Printf("Length of read buffer = %s\n",reponse)
  35.       //fmt.Printf("Client at %s says '%s'\n", commSock.RemoteAddr(), buf)
  36.  
  37.       // prepare the response
  38.       writelen, err := commSock.Write([]byte("Serv ACK! > "+reponse+"\n"))
  39.       if err != nil {
  40.         fmt.Fprintf(os.Stderr, "Could not write to socket: %s\n", err)
  41.       } else {
  42.         fmt.Printf("Wrote %d bytes to socket\n", writelen)
  43.       }
  44.       // send a 'response' to the client via the same connScock
  45.       writtenLen, err := commSock.Write([]byte(reponse))
  46.       if err != nil {
  47.          fmt.Fprintf(os.Stderr, "Could not write to socket: %s\n", err)
  48.        } else {
  49.          fmt.Printf("Wrote %d bytes to socket\n", writtenLen)
  50.        }
  51.        exit()
  52.     }//for
  53. }
  54.  
  55. func exit() {
  56.       os.Exit(0)
  57. }
  58.  
  59.  
  60. func main() {
  61.  
  62.  
  63.  
  64.     sockListener, err := net.Listen("tcp", "127.0.0.1:2056")
  65.     if err != nil {
  66.         fmt.Fprintf(os.Stderr, "Could not listen on socket: %s\n", err)
  67.         return
  68.     }
  69.  
  70.     for {
  71.     commSock, err := sockListener.Accept()
  72.     if err != nil {
  73.         fmt.Fprintf(os.Stderr, "Could not accept connection on socket: %s\n", err)
  74.         return
  75.     }
  76.  
  77.       go gogopowerrangers(commSock)
  78.     }
  79. }
  80.  
  81.  
  82.  
  83. client :
  84.  
  85. package main
  86.  
  87. import "net"
  88. import "fmt"
  89. import "bufio"
  90. import "os"
  91.  
  92. func main() {
  93.  
  94.   // connect to this socket
  95.   conn, _ := net.Dial("tcp", "127.0.0.1:2056")
  96.  
  97.  
  98.   for  {
  99.     // read in input from stdin
  100.     reader := bufio.NewReader(os.Stdin)
  101.     fmt.Print("Password: ")
  102.     text, _ := reader.ReadString('\n')
  103.     // send to socket
  104.     fmt.Fprintf(conn, text)
  105.     // listen for reply
  106.     message, _ := bufio.NewReader(conn).ReadString('\n')
  107.     fmt.Print("Message from server: "+message)
  108.   }
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top