Correction réception fichier, ajout méthode util Network.go

This commit is contained in:
Jérémi N ‘EndMove’ 2022-03-12 19:12:19 +01:00
parent b198f51c1e
commit 76972cc0bc
Signed by: EndMove
GPG Key ID: 65C4A02E1F5371A4
3 changed files with 30 additions and 38 deletions

View File

@ -48,9 +48,9 @@ func (client ClientMulticast) Run() {
// Resolve interface addr // Resolve interface addr
lAddr, failedRIA := ResolveInterfaceAddr(client.netInter) lAddr, failedRIA := ResolveInterfaceAddr(client.netInter)
if failedRIA { if failedRIA {
println("finish")
return return
} }
println("Interface ", lAddr.IP.String())
// Init UDP server flux // Init UDP server flux
con, failedDU := client.DialUdp(lAddr, rAddr) con, failedDU := client.DialUdp(lAddr, rAddr)
@ -82,43 +82,21 @@ func (client ClientMulticast) ResolveAddr() (*net.UDPAddr, bool) {
// ResolveInterfaceAddr Resolves the network interface address. // ResolveInterfaceAddr Resolves the network interface address.
func ResolveInterfaceAddr(inter string) (*net.UDPAddr, bool) { func ResolveInterfaceAddr(inter string) (*net.UDPAddr, bool) {
//i, err := net.InterfaceByName(inter) //ief, _ := net.InterfaceByName(inter)
//addrs, err := i.Addrs() //addrs, _ := ief.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
//}
//for _, addr := range addrs { //for _, addr := range addrs {
// if ipv4Addr = addr.(*net.IPNet).IP; ipv4Addr != nil { // if utils.IsIPv4(addr.String()) {
// break // println("Selected IP", addr.String())
// lAddr, _ := net.ResolveUDPAddr("udp", "192.168.86.25:15502")
// return lAddr, false
// } // }
//} //}
//println(ipv4Addr.String()) return nil, true
//addr, err := net.ResolveUDPAddr("udp", ipv4Addr.String())
//if err != nil {
// println(err.Error())
// return nil, true
//}
//return addr, false
} }
// DialUdp Ouvre une connection UDP // DialUdp Ouvre une connection UDP
func (client ClientMulticast) DialUdp(lAddr *net.UDPAddr, rAddr *net.UDPAddr) (*net.UDPConn, bool) { 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 { if errDial != nil {
println(errDial.Error()) println(errDial.Error())
return nil, true return nil, true

View File

@ -8,24 +8,31 @@ import (
// ReceiveFile Permet de récupérer un fichier sur un reader // ReceiveFile Permet de récupérer un fichier sur un reader
func ReceiveFile(fileName string, fileSize int, reader *bufio.Reader) bool { 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 { if fileErr != nil {
return false return false
} }
defer file.Close() defer file.Close()
currentSize := 0
buffer := make([]byte, 1024)
// Params
buffer := make([]byte, 1024)
currentSize := 0
// Retrieving file
for currentSize < fileSize { for currentSize < fileSize {
length, err := reader.Read(buffer) length, err := reader.Read(buffer)
if err != nil { if err != nil {
return false return false
} }
_, err = file.WriteAt(buffer, int64(currentSize))
currentSize += length if err != nil {
file.WriteAt(buffer, int64(currentSize)) return false
}
currentSize += length
check := fileSize - currentSize
if 1024 > check && check > 0 {
buffer = make([]byte, check)
}
} }
return true return true
} }

View File

@ -3,8 +3,10 @@ package utils
import ( import (
"fmt" "fmt"
"net" "net"
"strings"
) )
// NetworkLister List all network interface available
func NetworkLister() { func NetworkLister() {
// Retrieve Interfaces // Retrieve Interfaces
inter, err := net.Interfaces() inter, err := net.Interfaces()
@ -22,3 +24,8 @@ func NetworkLister() {
} }
print("\n") print("\n")
} }
// IsIPv4 Check if the ip is a v4 or v6 ip
func IsIPv4(addr string) bool {
return strings.Count(addr, ":") < 2
}