Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "net"
- "os"
- "strconv"
- "strings"
- "sync"
- )
- func main() {
- // Check for valid command line arguments
- if len(os.Args) != 2 {
- fmt.Fprintf(os.Stderr, "Usage: %s <ip>:<port_range>\n", os.Args[0])
- os.Exit(1)
- }
- // Parse the IP address and port range
- ipAndPorts := os.Args[1]
- host, ports, err := net.SplitHostPort(ipAndPorts)
- if err != nil {
- fmt.Fprintf(os.Stderr, "Error parsing IP and port range: %v\n", err)
- os.Exit(1)
- }
- // Convert the port range to integers
- start, end := parsePortRange(ports)
- // Calculate the number of file chunks to send
- numChunks := 20 // replace with your own value
- chunksPerPort := numChunks / (end - start + 1)
- remainder := numChunks % (end - start + 1)
- // Open the file to be sent
- file, err := os.Open("path/to/file")
- if err != nil {
- fmt.Fprintf(os.Stderr, "Error opening file: %v\n", err)
- os.Exit(1)
- }
- defer file.Close()
- // Set up a wait group for the goroutines
- var wg sync.WaitGroup
- // Send file chunks to each port in the range concurrently
- for port := start; port <= end; port++ {
- wg.Add(1)
- go func(p int) {
- defer wg.Done()
- numChunksToSend := chunksPerPort
- if remainder > 0 {
- numChunksToSend++
- remainder--
- }
- sendFileChunks(host, p, file, numChunksToSend)
- }(port)
- }
- // Wait for all goroutines to finish
- wg.Wait()
- }
- // Parses a port range string in the form "start-end" and returns the start and end ports as integers
- func parsePortRange(portRange string) (int, int) {
- ports := strings.Split(portRange, "-")
- start, err := strconv.Atoi(ports[0])
- if err != nil {
- fmt.Fprintf(os.Stderr, "Error parsing port range: %v\n", err)
- os.Exit(1)
- }
- end, err := strconv.Atoi(ports[1])
- if err != nil {
- fmt.Fprintf(os.Stderr, "Error parsing port range: %v\n", err)
- os.Exit(1)
- }
- return start, end
- }
- // Sends the specified number of file chunks to a given host and port using UDP
- func sendFileChunks(host string, port int, file *os.File, numChunks int) {
- // Create the UDP address
- serverAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", host, port))
- if err != nil {
- fmt.Fprintf(os.Stderr, "Error resolving server address: %v\n", err)
- return
- }
- // Open a UDP connection to the server
- conn, err := net.DialUDP("udp", nil, serverAddr)
- if err != nil {
- fmt.Fprintf(os.Stderr, "Error opening UDP connection: %v\n", err)
- return
- }
- defer conn.Close()
- // Read and send the specified number of file chunks
- for i := 0; i < numChunks; i++ {
- buf := make([]byte, 1024)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement