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
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

View File

@ -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
}
currentSize += length
file.WriteAt(buffer, int64(currentSize))
_, err = file.WriteAt(buffer, int64(currentSize))
if err != nil {
return false
}
currentSize += length
check := fileSize - currentSize
if 1024 > check && check > 0 {
buffer = make([]byte, check)
}
}
return true
}

View File

@ -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
}