With Go you use a Go routine per connection and then you can read in chunks and delay in an imperative style. No need for generators or async.
> On Jun 29, 2021, at 10:25 AM, LetGo <non3co...@gmail.com> wrote: > > Thanks for the answer! (: > In python it was straightforward to implement and it works like a charm. It > sends small packets with delay between each other without even care if it is > UDP or TCP: > > <prooff.png> > > <proof2.png> > > > The connection ( if UDP/ TCP ) is handled independently, the sending part ( > the one I pasted ) is equal for both of them, its just an "if" in the code. > The server ( which I control ) does riassembling by himself, so its fine. > I know that providing a solution is not an easy task, I know it for sure. > I'm just looking for a pro ( a saint ahaha ) with some patience that provides > an example of code of how it should look like to work as I wish it does. > I do not expect do make it work in first place, its almost impossible, but we > all know, with some errors you get the solution before or after(: > In this case I don't really know where to start and how to do it ( im a > newbie in golang ), so I came here to ask help > > Il giorno martedì 29 giugno 2021 alle 14:22:32 UTC+2 Brian Candler ha scritto: >> What sort of socket - TCP or UDP? TCP is an infinite stream, and there is >> no reliable way to divide it into "chunks" (except possibly using the URG >> pointer, which is very rarely used). There is no guarantee that waiting 1.6 >> seconds between sends will actually send in separate segments being sent, >> nor that the receiver will be able to determine that these were separate >> segments - they may well be merged. >> >> To do it reliably, you'd want to add your own framing, e.g. send an 4-byte >> length followed by that number of bytes. >> >> If you show a *complete* go program, together with a description of how it >> isn't working to your satisfaction, then maybe someone can help you fix it. >> If you want to make it behave the same as some given python program, then >> maybe you should show the python program as well. >> >> On Tuesday, 29 June 2021 at 11:51:57 UTC+1 LetGo wrote: >>> I forgot this: >>> // Manage connection for different behavior >>> func handleConn(conn net.Conn, binPath string) { >>> >>> msg := "Status?\n" >>> >>> if binPath != "" { >>> // Execute command and send Standard I/O net.Conn >>> cmd := exec.Command(binPath) >>> cmd.Stdin = conn >>> cmd.Stdout = conn >>> cmd.Stderr = conn >>> cmd.Run() >>> fmt.Println("Disconnected!?") >>> time.Sleep(5 * time.Second) >>> pippo, err2 := conn.Write([]byte(msg)) >>> if err2 != nil { >>> fmt.Println(pippo) >>> } >>> >>> >>> >>> >>> } else { >>> // Copy Standard I/O in a net.Conn >>> go io.Copy(os.Stderr, conn) >>> go io.Copy(os.Stdout, conn) >>> io.Copy(conn, os.Stdin) >>> >>> } >>> } >>> >>> Il giorno martedì 29 giugno 2021 alle 12:27:00 UTC+2 LetGo ha scritto: >>>> I have a proxy written in python with some logic that I would like to >>>> implement in a golang tool of mine, but I don't really know how to do it. >>>> It splits the data sent via socket (stdout) in small chunks, with a delay >>>> between each other >>>> >>>> I have a variable which gets a random number from a list: >>>> .... >>>> listbytes = [87, 88, 89, 90] >>>> .... >>>> >>>> I have also a function which splits in small chunk of bytes(n) the >>>> data(lst): >>>> ............ >>>> >>>> def chunks(lst, n): >>>> >>>> "Yield successive chunks from lst, where n is a list of possible sizes" >>>> >>>> i = 0 >>>> >>>> while i < len(lst): >>>> >>>> k = min(random.choice(n), len(lst) - i) >>>> >>>> yield lst[i:i + k] >>>> >>>> i += k >>>> >>>> ............. >>>> >>>> Both things are executed this way: >>>> ... >>>> # get the data >>>> data = s.recv(BUFFER_SIZE) >>>> >>>> if s == s_src: >>>> d = LOCAL_DATA_HANDLER(data) >>>> for chunk in chunks(d, listbytes): >>>> #sleep 1.6 seconds before sending the next chunk of data >>>> time.sleep(1.6) >>>> #send the chunked data >>>> s_dst.send(bytes(chunk) ) >>>> ... >>>> >>>> >>>> How do I implement the same exact logic illustrated here? >>>> >>>> func (nObj NetObject) RunClient(cmd string) { >>>> // Try connection >>>> >>>> for { >>>> conn, err := net.Dial(nObj.Type, nObj.Service) >>>> fmt.Print("connect") >>>> // msg := "status" >>>> if err != nil { >>>> fmt.Println("fail") >>>> } >>>> if err == nil { >>>> fmt.Println("ok") >>>> // defer conn.Close() >>>> defer conn.Close() >>>> log.Println("Connected to", conn.RemoteAddr()) >>>> handleConn(conn, cmd) >>>> >>>> >>>> //handleConn(conn, cmd) >>>> fmt.Println("After handle") >>>> } >>>> fmt.Println("Before sleep") >>>> time.Sleep(5 * time.Second) >>>> } >>>> >>>> } >>>> Can you help me please? > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/f82a73ad-164e-4af5-b183-9d4bf0b54911n%40googlegroups.com. > <prooff.png> > <proof2.png> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/BF8C0CC5-D02C-4C1D-A299-0E1355AC176D%40ix.netcom.com.