From 76972cc0bcb687974c6913b37d1fee723b8764d7 Mon Sep 17 00:00:00 2001 From: EndMove Date: Sat, 12 Mar 2022 19:12:19 +0100 Subject: [PATCH] =?UTF-8?q?Correction=20r=C3=A9ception=20fichier,=20ajout?= =?UTF-8?q?=20m=C3=A9thode=20util=20Network.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/network/ClientMulticast.go | 40 +++++----------------- pkg/utils/FileReceiver.go | 21 ++++++++---- pkg/utils/{NetworkLister.go => Network.go} | 7 ++++ 3 files changed, 30 insertions(+), 38 deletions(-) rename pkg/utils/{NetworkLister.go => Network.go} (67%) diff --git a/pkg/network/ClientMulticast.go b/pkg/network/ClientMulticast.go index 1bda350..173bf4a 100644 --- a/pkg/network/ClientMulticast.go +++ b/pkg/network/ClientMulticast.go @@ -48,9 +48,9 @@ func (client ClientMulticast) Run() { // Resolve interface addr lAddr, failedRIA := ResolveInterfaceAddr(client.netInter) if failedRIA { - println("finish") return } + println("Interface ", lAddr.IP.String()) // Init UDP server flux con, failedDU := client.DialUdp(lAddr, rAddr) @@ -82,43 +82,21 @@ func (client ClientMulticast) ResolveAddr() (*net.UDPAddr, bool) { // ResolveInterfaceAddr Resolves the network interface address. func ResolveInterfaceAddr(inter string) (*net.UDPAddr, bool) { - //i, err := net.InterfaceByName(inter) - //addrs, err := i.Addrs() - //println(addrs[0]) - //addr, err := net.ResolveUDPAddr("udp", addrs[0].String()) - //if err != nil { - // println(err.Error()) - // return nil, true - //} - return nil, false - //var ipv4Addr net.IP - //ief, err := net.InterfaceByName(inter) - //if err != nil { - // println(err.Error()) - // return nil, true - //} - //addrs, err := ief.Addrs() - //if err != nil { - // println(err.Error()) - // return nil, true - //} + //ief, _ := net.InterfaceByName(inter) + //addrs, _ := ief.Addrs() //for _, addr := range addrs { - // if ipv4Addr = addr.(*net.IPNet).IP; ipv4Addr != nil { - // break + // if utils.IsIPv4(addr.String()) { + // println("Selected IP", addr.String()) + // lAddr, _ := net.ResolveUDPAddr("udp", "192.168.86.25:15502") + // return lAddr, false // } //} - //println(ipv4Addr.String()) - //addr, err := net.ResolveUDPAddr("udp", ipv4Addr.String()) - //if err != nil { - // println(err.Error()) - // return nil, true - //} - //return addr, false + return nil, true } // DialUdp Ouvre une connection UDP func (client ClientMulticast) DialUdp(lAddr *net.UDPAddr, rAddr *net.UDPAddr) (*net.UDPConn, bool) { - con, errDial := net.DialUDP("udp", nil, rAddr) + con, errDial := net.DialUDP("udp", lAddr, rAddr) if errDial != nil { println(errDial.Error()) return nil, true diff --git a/pkg/utils/FileReceiver.go b/pkg/utils/FileReceiver.go index 46b299b..873a071 100644 --- a/pkg/utils/FileReceiver.go +++ b/pkg/utils/FileReceiver.go @@ -8,24 +8,31 @@ import ( // ReceiveFile Permet de récupérer un fichier sur un reader func ReceiveFile(fileName string, fileSize int, reader *bufio.Reader) bool { - file, fileErr := os.Create(fmt.Sprintf("/home/benjamin/sbe/%s", fileName)) + file, fileErr := os.Create(fmt.Sprintf("D:\\sbe\\%s", fileName)) if fileErr != nil { return false } - defer file.Close() - currentSize := 0 - buffer := make([]byte, 1024) + // Params + buffer := make([]byte, 1024) + currentSize := 0 + + // Retrieving file for currentSize < fileSize { length, err := reader.Read(buffer) if err != nil { return false } - + _, err = file.WriteAt(buffer, int64(currentSize)) + if err != nil { + return false + } currentSize += length - file.WriteAt(buffer, int64(currentSize)) + check := fileSize - currentSize + if 1024 > check && check > 0 { + buffer = make([]byte, check) + } } - return true } diff --git a/pkg/utils/NetworkLister.go b/pkg/utils/Network.go similarity index 67% rename from pkg/utils/NetworkLister.go rename to pkg/utils/Network.go index 3e0c0de..c860368 100644 --- a/pkg/utils/NetworkLister.go +++ b/pkg/utils/Network.go @@ -3,8 +3,10 @@ package utils import ( "fmt" "net" + "strings" ) +// NetworkLister List all network interface available func NetworkLister() { // Retrieve Interfaces inter, err := net.Interfaces() @@ -22,3 +24,8 @@ func NetworkLister() { } print("\n") } + +// IsIPv4 Check if the ip is a v4 or v6 ip +func IsIPv4(addr string) bool { + return strings.Count(addr, ":") < 2 +}