Advertisement
Galro

Golang udp server 2

Feb 16th, 2023
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "net"
  6.     "os"
  7.     "strconv"
  8.     "strings"
  9.     "sync"
  10. )
  11.  
  12. func main() {
  13.     // Check for valid command line arguments
  14.     if len(os.Args) != 2 {
  15.         fmt.Fprintf(os.Stderr, "Usage: %s <ip>:<port_range>\n", os.Args[0])
  16.         os.Exit(1)
  17.     }
  18.  
  19.     // Parse the IP address and port range
  20.     ipAndPorts := os.Args[1]
  21.     host, ports, err := net.SplitHostPort(ipAndPorts)
  22.     if err != nil {
  23.         fmt.Fprintf(os.Stderr, "Error parsing IP and port range: %v\n", err)
  24.         os.Exit(1)
  25.     }
  26.  
  27.     // Convert the port range to integers
  28.     start, end := parsePortRange(ports)
  29.  
  30.     // Calculate the number of file chunks to send
  31.     numChunks := 20 // replace with your own value
  32.     chunksPerPort := numChunks / (end - start + 1)
  33.     remainder := numChunks % (end - start + 1)
  34.  
  35.     // Open the file to be sent
  36.     file, err := os.Open("path/to/file")
  37.     if err != nil {
  38.         fmt.Fprintf(os.Stderr, "Error opening file: %v\n", err)
  39.         os.Exit(1)
  40.     }
  41.     defer file.Close()
  42.  
  43.     // Set up a wait group for the goroutines
  44.     var wg sync.WaitGroup
  45.  
  46.     // Send file chunks to each port in the range concurrently
  47.     for port := start; port <= end; port++ {
  48.         wg.Add(1)
  49.         go func(p int) {
  50.             defer wg.Done()
  51.             numChunksToSend := chunksPerPort
  52.             if remainder > 0 {
  53.                 numChunksToSend++
  54.                 remainder--
  55.             }
  56.             sendFileChunks(host, p, file, numChunksToSend)
  57.         }(port)
  58.     }
  59.  
  60.     // Wait for all goroutines to finish
  61.     wg.Wait()
  62. }
  63.  
  64. // Parses a port range string in the form "start-end" and returns the start and end ports as integers
  65. func parsePortRange(portRange string) (int, int) {
  66.     ports := strings.Split(portRange, "-")
  67.     start, err := strconv.Atoi(ports[0])
  68.     if err != nil {
  69.         fmt.Fprintf(os.Stderr, "Error parsing port range: %v\n", err)
  70.         os.Exit(1)
  71.     }
  72.     end, err := strconv.Atoi(ports[1])
  73.     if err != nil {
  74.         fmt.Fprintf(os.Stderr, "Error parsing port range: %v\n", err)
  75.         os.Exit(1)
  76.     }
  77.     return start, end
  78. }
  79.  
  80. // Sends the specified number of file chunks to a given host and port using UDP
  81. func sendFileChunks(host string, port int, file *os.File, numChunks int) {
  82.     // Create the UDP address
  83.     serverAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", host, port))
  84.     if err != nil {
  85.         fmt.Fprintf(os.Stderr, "Error resolving server address: %v\n", err)
  86.         return
  87.     }
  88.  
  89.     // Open a UDP connection to the server
  90.     conn, err := net.DialUDP("udp", nil, serverAddr)
  91.     if err != nil {
  92.         fmt.Fprintf(os.Stderr, "Error opening UDP connection: %v\n", err)
  93.         return
  94.     }
  95.     defer conn.Close()
  96.  
  97.     // Read and send the specified number of file chunks
  98.     for i := 0; i < numChunks; i++ {
  99.         buf := make([]byte, 1024)
  100.    
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement