Golang transfer file with socket

Golang transfer file with socket,第1张

概述package mainimport ( "bufio" "code.google.com/p/mahonia" "fmt" "io" "net" "os")func main() { fmt.Println("create a server or client?") reader := bufio.NewReader(os.S
package mainimport (    "bufio"    "code.Google.com/p/mahonia"    "fmt"    "io"    "net"    "os")func main() {    fmt.Println("create a server or clIEnt?")    reader := bufio.NewReader(os.Stdin)    input,_,_ := reader.Readline()    if string(input) == "server" {        Server()    }    if string(input) == "clIEnt" {        ClIEnt()    } else {        fmt.Println(Show("err arguments,entering again!.\r\n  alternaltive argument is server or clIEnt"))        os.Exit(0)    }}func Show(s string) string {    enc := mahonia.NewEncoder("gbk") //中文转码有错误的函数。    return enc.ConvertString(s)}func Server() {    exit := make(chan bool)    ip := net.ParseIP("127.0.0.1")    addr := net.TCPAddr{ip,8888,""}    go func() {        Listener,err := net.ListenTCP("tcp",&addr) //TCPListener Listen        if err != nil {            fmt.Println("Initialize error",err.Error())            exit <- true            return        }        fmt.Println("Server Listening...")        tcpcon,err := Listener.AcceptTCP() //TCPConn clIEnt        if err != nil {            fmt.Println(err.Error())            //continue        }        fmt.Println("ClIEnt connect")        data := make([]byte,1024)        if err != nil {            fmt.Println("tcpcon.Read(data)" + err.Error())        }        //recv file name        wc,err := tcpcon.Read(data)        fo,err := os.Create("F:\uploads\" + string(data[0:wc]))        if err != nil {            fmt.Println("os.Create" + err.Error())        }        fmt.Println("the file's name is:",string(data[0:wc]))        //recb file size        wc,err = tcpcon.Read(data)        fmt.Println("the file's size is:",string(data[0:wc]))        defer fo.Close()        for {            c,err := tcpcon.Read(data) //???为何调用conn类的Read            if err != nil {                fmt.Println("tcpcon.Read(data)" + err.Error())            }            if string(data[0:c]) == "filerecvend" {                fmt.Println("string(data[0:c]) == filerecvend is true")                tcpcon.Write([]byte("file recv finished!\r\n"))                tcpcon.Close()                break            }            //write to the file            _,err = fo.Write(data[0:c])            if err != nil {                fmt.Println("write err" + err.Error())            }        }    }()    <-exit    fmt.Println(Show("server close!"))}func ClIEnt() {    fmt.Println("send ur file to the destination","input ur filename:")    reader := bufio.NewReader(os.Stdin)    input,_ := reader.Readline()    fmt.Println(string(input))    fi,err := os.Open(string(input))    if err != nil {        panic(err)    }    defer fi.Close()    fiinfo,err := fi.Stat()    fmt.Println("the size of file is ",fiinfo.Size(),"bytes") //fiinfo.Size() return int64 type    conn,err := net.Dial("tcp","127.0.0.1:8888")    if err != nil {        fmt.Println(Show("connect server fail!"),Show(err.Error()))        return    }    defer conn.Close()    //send filename    _,err = conn.Write(input)    if err != nil {        fmt.Println("conn.Write",err.Error())    }    //send file size    _,err = conn.Write([]byte(string(fiinfo.Size())))    if err != nil {        fmt.Println("conn.Write",err.Error())    }    buff := make([]byte,1024)    for {        n,err := fi.Read(buff)        if err != nil && err != io.EOF {            panic(err)        }        if n == 0 {            conn.Write([]byte("filerecvend"))            fmt.Println("filerecvend")            break        }        _,err = conn.Write(buff)        if err != nil {            fmt.Println(err.Error())        }    }}
总结

以上是内存溢出为你收集整理的Golang transfer file with socket全部内容,希望文章能够帮你解决Golang transfer file with socket所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1283669.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存