Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 212c9a7
Showing
202 changed files
with
117,647 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* DaytimeServer | ||
*/ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
"time" | ||
) | ||
|
||
func main() { | ||
|
||
service := ":1200" | ||
tcpAddr, err := net.ResolveTCPAddr("tcp", service) | ||
checkError(err) | ||
|
||
listener, err := net.ListenTCP("tcp", tcpAddr) | ||
checkError(err) | ||
|
||
for { | ||
conn, err := listener.Accept() | ||
if err != nil { | ||
continue | ||
} | ||
|
||
daytime := time.Now().String() | ||
conn.Write([]byte(daytime)) // don't care about return value | ||
conn.Close() // we're finished with this client | ||
} | ||
} | ||
|
||
func checkError(err error) { | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error()) | ||
os.Exit(1) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* EchoClient | ||
*/ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
"runtime" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Fprint(os.Stderr, "Usage: ", os.Args[0], " host:port\n") | ||
// fmt.Println("Usage: ", os.Args[0], "host:port") | ||
os.Exit(1) | ||
} | ||
service := os.Args[1] | ||
|
||
conn, err := net.Dial("tcp", service) | ||
checkError("Dial", err) | ||
|
||
for n := 0; n < 10; n++ { | ||
conn.Write([]byte("Hello " + string(n+48))) | ||
|
||
var buf [512]byte | ||
n, err := conn.Read(buf[0:]) | ||
checkError("Read", err) | ||
|
||
fmt.Println(string(buf[0:n])) | ||
} | ||
os.Exit(0) | ||
} | ||
|
||
func checkError(errStr string, err error) { | ||
if err != nil { | ||
fmt.Fprint(os.Stderr, errStr, err.Error()) | ||
//log.Exit(errStr+":", err.Error()) | ||
_, file, line, ok := runtime.Caller(1) | ||
if ok { | ||
fmt.Println("Line number", file, line) | ||
} | ||
os.Exit(1) | ||
/* | ||
fmt.Println("Fatal error ", err.Error()) | ||
os.Exit(1) | ||
*/ | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* GetHeadInfo | ||
*/ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"os" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Fprintf(os.Stderr, "Usage: %s host:port ", os.Args[0]) | ||
os.Exit(1) | ||
} | ||
service := os.Args[1] | ||
|
||
tcpAddr, err := net.ResolveTCPAddr("tcp4", service) | ||
checkError(err) | ||
|
||
conn, err := net.DialTCP("tcp", nil, tcpAddr) | ||
checkError(err) | ||
|
||
_, err = conn.Write([]byte("HEAD / HTTP/1.0\r\n\r\n")) | ||
checkError(err) | ||
|
||
//result, err := readFully(conn) | ||
result, err := ioutil.ReadAll(conn) | ||
checkError(err) | ||
|
||
fmt.Println(string(result)) | ||
|
||
os.Exit(0) | ||
} | ||
|
||
func checkError(err error) { | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error()) | ||
os.Exit(1) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* IP | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Fprintf(os.Stderr, "Usage: %s ip-addr\n", os.Args[0]) | ||
os.Exit(1) | ||
} | ||
name := os.Args[1] | ||
|
||
addr := net.ParseIP(name) | ||
if addr == nil { | ||
fmt.Println("Invalid address") | ||
} else { | ||
fmt.Println("The address is ", addr.String()) | ||
} | ||
os.Exit(0) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* IPDaytimeClient | ||
*/ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Println("Usage: ", os.Args[0], "host:port") | ||
os.Exit(1) | ||
} | ||
service := os.Args[1] | ||
|
||
conn, err := net.Dial("udp", service) | ||
checkError(err) | ||
|
||
_, err = conn.Write([]byte("anything")) | ||
checkError(err) | ||
|
||
var buf [512]byte | ||
n, err := conn.Read(buf[0:]) | ||
checkError(err) | ||
|
||
fmt.Println(string(buf[0:n])) | ||
|
||
os.Exit(0) | ||
} | ||
|
||
func checkError(err error) { | ||
if err != nil { | ||
fmt.Println("Fatal error ", err.Error()) | ||
os.Exit(1) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* IPGetHeadInfo | ||
*/ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"net" | ||
"os" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Fprintf(os.Stderr, "Usage: %s host:port", os.Args[0]) | ||
os.Exit(1) | ||
} | ||
service := os.Args[1] | ||
|
||
conn, err := net.Dial("tcp", service) | ||
checkError(err) | ||
|
||
_, err = conn.Write([]byte("HEAD / HTTP/1.0\r\n\r\n")) | ||
checkError(err) | ||
|
||
result, err := readFully(conn) | ||
//result, err := io.readAll(conn) | ||
checkError(err) | ||
|
||
fmt.Println(string(result)) | ||
|
||
os.Exit(0) | ||
} | ||
|
||
func checkError(err error) { | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error()) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func readFully(conn net.Conn) ([]byte, error) { | ||
defer conn.Close() | ||
|
||
result := bytes.NewBuffer(nil) | ||
var buf [512]byte | ||
for { | ||
n, err := conn.Read(buf[0:]) | ||
result.Write(buf[0:n]) | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
return nil, err | ||
} | ||
} | ||
return result.Bytes(), nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* IPv4Mask | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Fprintf(os.Stderr, "Usage: %s dotted-ip-addr\n", os.Args[0]) | ||
os.Exit(1) | ||
} | ||
dotAddr := os.Args[1] | ||
|
||
addr := net.ParseIP(dotAddr) | ||
if addr == nil { | ||
fmt.Println("Invalid address") | ||
os.Exit(1) | ||
} | ||
mask := addr.DefaultMask() | ||
network := addr.Mask(mask) | ||
ones, bits := mask.Size() | ||
fmt.Println("Address is ", addr.String(), | ||
"\nDefault mask length is ", bits, | ||
"\nLeading ones count is ", ones, | ||
"\nMask is (hex) ", mask.String(), | ||
"\nNetwork is ", network.String()) | ||
os.Exit(0) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* LookupHost | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Fprintf(os.Stderr, "Usage: %s hostname\n", os.Args[0]) | ||
os.Exit(1) | ||
} | ||
name := os.Args[1] | ||
|
||
addrs, err := net.LookupHost(name) | ||
if err != nil { | ||
fmt.Println("Error: ", err.Error()) | ||
os.Exit(2) | ||
} | ||
|
||
for _, s := range addrs { | ||
fmt.Println(s) | ||
} | ||
os.Exit(0) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* LookupPort | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 3 { | ||
fmt.Fprintf(os.Stderr, | ||
"Usage: %s network-type service\n", | ||
os.Args[0]) | ||
os.Exit(1) | ||
} | ||
networkType := os.Args[1] | ||
service := os.Args[2] | ||
|
||
port, err := net.LookupPort(networkType, service) | ||
if err != nil { | ||
fmt.Println("Error: ", err.Error()) | ||
os.Exit(2) | ||
} | ||
|
||
fmt.Println("Service port ", port) | ||
os.Exit(0) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* Mask | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 4 { | ||
fmt.Fprintf(os.Stderr, "Usage: %s dotted-ip-addr ones bits\n", os.Args[0]) | ||
os.Exit(1) | ||
} | ||
dotAddr := os.Args[1] | ||
ones, _ := strconv.Atoi(os.Args[2]) | ||
bits, _ := strconv.Atoi(os.Args[3]) | ||
|
||
addr := net.ParseIP(dotAddr) | ||
if addr == nil { | ||
fmt.Println("Invalid address") | ||
os.Exit(1) | ||
} | ||
mask := net.CIDRMask(ones, bits) | ||
network := addr.Mask(mask) | ||
fmt.Println("Address is ", addr.String(), | ||
"\nMask length is ", bits, | ||
"\nLeading ones count is ", ones, | ||
"\nMask is (hex) ", mask.String(), | ||
"\nNetwork is ", network.String()) | ||
os.Exit(0) | ||
} |
Oops, something went wrong.