Skip to content

Instantly share code, notes, and snippets.

@fazo96
Created February 3, 2014 09:45
Show Gist options
  • Save fazo96/8781080 to your computer and use it in GitHub Desktop.
Save fazo96/8781080 to your computer and use it in GitHub Desktop.
Vey basic Golang tcp implementation
package main
import(
"fmt"
"net"
)
func server(){
fmt.Printf("Server started\n")
serv,err := net.Listen("tcp","127.0.0.1:7000") // Start listening on tcp port 7000
if(err != nil){
fmt.Printf("(Server) Error opening port\n")
return
}
for{
fmt.Printf("(Server) Waiting for connection...\n");
_, err := serv.Accept() // Accept first incoming connection
if(err == nil){
fmt.Printf("(Server) connection success\n");
} else {
fmt.Printf("(Server) connection fail\n");
}
}
}
func main(){
go server() // Start server in separate thread
fmt.Printf("Client started\n")
_, err := net.Dial("tcp","localhost:7000") // Try a connection to server
if(err != nil){
fmt.Printf("(Client) Connection failed\n")
} else {
fmt.Printf("(Client) connection success\n")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment