Created
May 25, 2021 01:19
-
-
Save Yapcheekian/81853f2d39327c86c8bc24c1d05c2490 to your computer and use it in GitHub Desktop.
example server in go
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 "net" | |
import "fmt" | |
import "bufio" | |
import "strings" // only needed below for sample processing | |
func main() { | |
fmt.Println("Launching server...") | |
// listen on all interfaces | |
ln, _ := net.Listen("tcp", ":8081") | |
// accept connection on port | |
conn, _ := ln.Accept() | |
// run loop forever (or until ctrl-c) | |
for { | |
// will listen for message to process ending in newline (\n) | |
message, _ := bufio.NewReader(conn).ReadString('\n') | |
// output message received | |
fmt.Print("Message Received:", string(message)) | |
// sample process for string received | |
newmessage := strings.ToUpper(message) | |
// send new string back to client | |
conn.Write([]byte(newmessage + "\n")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment