Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- client
- package main
- import (
- "bufio"
- "fmt"
- "net"
- "os"
- )
- func main() {
- // connect to this socket
- conn, _ := net.Dial("tcp", "127.0.0.1:8080")
- for {
- // read in input from stdin
- reader := bufio.NewReader(os.Stdin)
- fmt.Print("Text to send: ")
- text, _ := reader.ReadString('\n')
- // send to socket
- fmt.Fprintf(conn, text+"\n")
- // listen for reply
- message, _ := bufio.NewReader(conn).ReadString('\n')
- fmt.Print(message)
- }
- }
- server
- package main
- import (
- "bufio"
- "encoding/json"
- "log"
- "net"
- "os"
- "strconv"
- )
- type Configuration struct {
- Port int
- MaxLength int
- MODE string
- SHIFT int
- }
- var mode string
- var shift int
- var indx int
- var maxLength int
- const lowerCaseAlphabet = "abcdefghijklmnopqrstuvwxyz"
- const upperCaseAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- func main() {
- //filename is the path to the json config file
- config := Configuration{}
- file, err := os.Open("config.json")
- if err != nil {
- log.Fatal(err)
- }
- decoder := json.NewDecoder(file)
- err = decoder.Decode(&config)
- if err != nil {
- log.Fatal(err)
- }
- listener, err := net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(config.Port))
- if err != nil {
- log.Fatal("tcp server listener error:", err)
- }
- mode = config.MODE
- shift = config.SHIFT
- maxLength = config.MaxLength
- for {
- conn, err := listener.Accept()
- if err != nil {
- log.Fatal("tcp server accept error", err)
- }
- clientAddr := conn.RemoteAddr().String()
- log.Println("Clientul " + clientAddr + " s-a conectat.")
- go handleConnection(conn)
- }
- }
- func mod(x, d int) int {
- x = x % d
- if x >= 0 {
- return x
- }
- if d < 0 {
- return x - d
- }
- return x + d
- }
- func handleConnection(conn net.Conn) {
- bufferBytes, err := bufio.NewReader(conn).ReadBytes('\n')
- clientAddr := conn.RemoteAddr().String()
- if err != nil {
- log.Println("Clientul " + clientAddr + " a iesit.")
- conn.Close()
- return
- }
- message := string(bufferBytes)
- log.Println("Clientul " + clientAddr + " a trimis: " + message)
- if len(message)-2 > maxLength {
- log.Println("Textul depaseste limita de caractere")
- conn.Write([]byte("Textul depaseste limita de caractere\n"))
- } else {
- //response := fmt.Sprintf(message + " from " + clientAddr + "\n")
- rotatedText := []byte(message)
- if mode == "LEFT" {
- indx = -1
- } else {
- indx = 1
- }
- for index, byteValue := range rotatedText {
- if byteValue >= 'a' && byteValue <= 'z' {
- rotatedText[index] = lowerCaseAlphabet[mod(int(byteValue-'a')+indx*shift, 26)]
- } else if byteValue >= 'A' && byteValue <= 'Z' {
- rotatedText[index] = upperCaseAlphabet[mod(int(byteValue-'A')+indx*shift, 26)]
- }
- }
- log.Println("Textul trimis la " + clientAddr + "este: " + string(rotatedText))
- conn.Write([]byte("Textul codat: " + string(rotatedText)))
- }
- handleConnection(conn)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement