Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- // This is a simple program that will connect to a listening netcat server, send a hello, and receive a reply
- // This is meant as an example of client-side socket usage
- import (
- "fmt"
- "net"
- "bufio"
- "os"
- "time"
- "strings"
- )
- // IP addr and port number to connect to
- var address string = "127.0.0.1:4444" // CHANGE THIS
- // dial server and return connection object
- func dialServer() net.Conn {
- conn, err := net.Dial("tcp", address)
- if err != nil {
- fmt.Printf("Fatal error: %s", err)
- os.Exit(3)
- }
- return conn
- }
- func main() {
- // call dialServer
- fmt.Printf("[*] Connecting to server... ")
- conn := dialServer()
- fmt.Println("[DONE]")
- time.Sleep(3 * time.Second)
- // send hello message to netcat server
- fmt.Printf("[*] Sending hello to server... ")
- fmt.Fprintf(conn, "Hello, Netcat!\n")
- fmt.Println("[DONE]")
- time.Sleep(3 * time.Second)
- // receive reply from netcat
- fmt.Printf("[*] Receiving reply from server... ")
- reply, err := bufio.NewReader(conn).ReadString('\n')
- if err != nil {
- fmt.Println("[FAIL]")
- os.Exit(3)
- }
- reply = strings.TrimSpace(reply) // trim leading whitespace and newline
- fmt.Printf("[RECEIVED: %s]\n", reply)
- conn.Close()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement