Simple socket file transfer in the Go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Learning go language for a week, the department boss and let other languages, casually write a go file transfer example, mainly with net package, inside there are all kinds of nonstandard, please forgive me. Server and client are written in main (), TCP listening IP and port, receive file save path can be changed according to their own circumstances, see the following code:

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 ()} E LSE {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")//Chinese transcoding has the wrong function. 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", &AMP;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, 1024x768) 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 ' 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)//??? Why call the Conn class of the 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, 1024x768) 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 ())}}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.