socket.go 632 B

1234567891011121314151617181920212223242526272829303132
  1. // socket.go
  2. package main
  3. import (
  4. "fmt"
  5. "net"
  6. "io"
  7. )
  8. func main() {
  9. var (
  10. host = "www.apache.org"
  11. port = "80"
  12. remote = host + ":" + port
  13. msg string = "GET / \n"
  14. data = make([]uint8, 4096)
  15. read = true
  16. count = 0
  17. )
  18. // create the socket
  19. con, err := net.Dial("tcp", remote)
  20. // send our message. an HTTP GET request in this case
  21. io.WriteString(con, msg)
  22. // read the response from the webserver
  23. for read {
  24. count, err = con.Read(data)
  25. read = (err == nil)
  26. fmt.Printf(string(data[0:count]))
  27. }
  28. con.Close()
  29. }