Created
February 3, 2014 09:45
-
-
Save fazo96/8781080 to your computer and use it in GitHub Desktop.
Vey basic Golang tcp implementation
This file contains 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( | |
"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