Created
August 27, 2023 04:11
-
-
Save ShreyanJain9/3b2718cdae21ba7384d9ee86730259fa to your computer and use it in GitHub Desktop.
Nex Protocol Client in Go
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 ( | |
"bufio" | |
"fmt" | |
"net" | |
"os" | |
) | |
const ( | |
ServerAddr = "localhost:1900" // Change this to your server address | |
) | |
func main() { | |
conn, err := net.Dial("tcp", ServerAddr) | |
if err != nil { | |
fmt.Printf("Error connecting to server: %s\n", err) | |
return | |
} | |
defer conn.Close() | |
fmt.Print("Enter Nex URI (path): ") | |
scanner := bufio.NewScanner(os.Stdin) | |
if scanner.Scan() { | |
path := scanner.Text() | |
sendRequest(conn, path) | |
receiveResponse(conn) | |
} | |
} | |
func sendRequest(conn net.Conn, path string) { | |
fmt.Fprintf(conn, "%s\n", path) | |
} | |
func receiveResponse(conn net.Conn) { | |
scanner := bufio.NewScanner(conn) | |
for scanner.Scan() { | |
fmt.Println(scanner.Text()) | |
} | |
if err := scanner.Err(); err != nil { | |
fmt.Printf("Error reading response: %s\n", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment