Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. client
  2.  
  3. package main
  4.  
  5. import (
  6. "bufio"
  7. "fmt"
  8. "net"
  9. "os"
  10. )
  11.  
  12. func main() {
  13.  
  14. // connect to this socket
  15. conn, _ := net.Dial("tcp", "127.0.0.1:8080")
  16. for {
  17. // read in input from stdin
  18. reader := bufio.NewReader(os.Stdin)
  19. fmt.Print("Text to send: ")
  20. text, _ := reader.ReadString('\n')
  21. // send to socket
  22. fmt.Fprintf(conn, text+"\n")
  23. // listen for reply
  24. message, _ := bufio.NewReader(conn).ReadString('\n')
  25. fmt.Print(message)
  26. }
  27.  
  28. }
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. server
  36.  
  37. package main
  38.  
  39. import (
  40. "bufio"
  41. "encoding/json"
  42. "log"
  43. "net"
  44. "os"
  45. "strconv"
  46. )
  47.  
  48. type Configuration struct {
  49. Port int
  50. MaxLength int
  51. MODE string
  52. SHIFT int
  53. }
  54.  
  55. var mode string
  56. var shift int
  57. var indx int
  58. var maxLength int
  59.  
  60. const lowerCaseAlphabet = "abcdefghijklmnopqrstuvwxyz"
  61. const upperCaseAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  62.  
  63. func main() {
  64.  
  65. //filename is the path to the json config file
  66. config := Configuration{}
  67. file, err := os.Open("config.json")
  68. if err != nil {
  69. log.Fatal(err)
  70. }
  71. decoder := json.NewDecoder(file)
  72. err = decoder.Decode(&config)
  73. if err != nil {
  74. log.Fatal(err)
  75. }
  76.  
  77. listener, err := net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(config.Port))
  78. if err != nil {
  79. log.Fatal("tcp server listener error:", err)
  80. }
  81.  
  82. mode = config.MODE
  83. shift = config.SHIFT
  84. maxLength = config.MaxLength
  85.  
  86. for {
  87. conn, err := listener.Accept()
  88. if err != nil {
  89. log.Fatal("tcp server accept error", err)
  90. }
  91. clientAddr := conn.RemoteAddr().String()
  92. log.Println("Clientul " + clientAddr + " s-a conectat.")
  93. go handleConnection(conn)
  94. }
  95. }
  96. func mod(x, d int) int {
  97. x = x % d
  98. if x >= 0 {
  99. return x
  100. }
  101. if d < 0 {
  102. return x - d
  103. }
  104. return x + d
  105. }
  106. func handleConnection(conn net.Conn) {
  107. bufferBytes, err := bufio.NewReader(conn).ReadBytes('\n')
  108. clientAddr := conn.RemoteAddr().String()
  109. if err != nil {
  110. log.Println("Clientul " + clientAddr + " a iesit.")
  111. conn.Close()
  112. return
  113. }
  114. message := string(bufferBytes)
  115. log.Println("Clientul " + clientAddr + " a trimis: " + message)
  116. if len(message)-2 > maxLength {
  117. log.Println("Textul depaseste limita de caractere")
  118. conn.Write([]byte("Textul depaseste limita de caractere\n"))
  119. } else {
  120. //response := fmt.Sprintf(message + " from " + clientAddr + "\n")
  121. rotatedText := []byte(message)
  122. if mode == "LEFT" {
  123. indx = -1
  124. } else {
  125. indx = 1
  126. }
  127. for index, byteValue := range rotatedText {
  128. if byteValue >= 'a' && byteValue <= 'z' {
  129. rotatedText[index] = lowerCaseAlphabet[mod(int(byteValue-'a')+indx*shift, 26)]
  130. } else if byteValue >= 'A' && byteValue <= 'Z' {
  131. rotatedText[index] = upperCaseAlphabet[mod(int(byteValue-'A')+indx*shift, 26)]
  132. }
  133. }
  134.  
  135. log.Println("Textul trimis la " + clientAddr + "este: " + string(rotatedText))
  136.  
  137. conn.Write([]byte("Textul codat: " + string(rotatedText)))
  138.  
  139. }
  140.  
  141. handleConnection(conn)
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement