Created
October 15, 2021 16:09
-
-
Save gadelkareem/71790f8fc332c5056c43b6dda7eb5fc7 to your computer and use it in GitHub Desktop.
Fake server in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"net" | |
) | |
func handleConnection(conn net.Conn) { | |
defer conn.Close() | |
scanner := bufio.NewScanner(conn) | |
for scanner.Scan() { | |
message := scanner.Text() | |
fmt.Println("################################Message:", message) | |
// newMessage := strings.ToUpper(message) | |
// conn.Write([]byte(newMessage + "\n")) | |
} | |
if err := scanner.Err(); err != nil { | |
fmt.Println("error:", err) | |
} | |
} | |
func main() { | |
port:= "8080" | |
ln, err := net.Listen("tcp", "0.0.0.0:"+port) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("Accept connection on port " +port) | |
for { | |
conn, err := ln.Accept() | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("Calling handleConnection", conn.RemoteAddr()) | |
go handleConnection(conn) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment