Created
December 2, 2015 21:44
-
-
Save frrist/56e08a8d5fd87d9ec850 to your computer and use it in GitHub Desktop.
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 "os" | |
func main() { | |
// connect to this socket | |
conn, _ := net.Dial("tcp", "127.0.0.1:8081") | |
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 from server: "+message) | |
} | |
} |
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