Go NET package 1

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
/** * Created by Administrator on 13-12-9. */package mainimport ("FMT" "OS" "NET")/** * Converts an IP address of type string to an IP object */func main () {name: = "192.168.1.97" IP: = net. PARSEIP (name) If IP = = Nil {fmt. fprintf (OS. Stderr, "ERR: Invalid Address") return}fmt. fprintf (OS. Stdout, "IP:%s%s\n", IP, IP. String ()) Defaultmask: = IP. Defaultmask () fmt. fprintf (OS. Stdout, "Defaultmask:%s%s\n", Defaultmask, defaultmask.string ()) ones, bits: = Defaultmask.size () fmt. fprintf (OS. Stdout, "ones:%d bits:%d\n", Ones, BITS)}




/** * Created by Administrator on 13-12-9. */package Mainimport ("net" "FMT" "OS")/** * Obtain the network based on IP and Mask */func main () {name: = "192.168.1.97" IP: = net. PARSEIP (name) Mask: = IP. Defaultmask () Network: = IP. Mask (mask) fmt. fprintf (OS. Stdout, "Network:%s", network. String ())//192.168.1.0//ip:192.168.1.97//mask:255.255.255.0//network:192.168.1.0}




/** * Created by Administrator on 13-12-9. */package Mainimport ("net" "FMT" "OS")/** * The Domain name resolution IP Address * obtains all IP addresses corresponding to the domain name */func main () {domain: = "www.baidu.com" ipaddr, err: = Net. RESOLVEIPADDR ("IP", domain) if err! = Nil {fmt. fprintf (OS. Stderr, "Err:%s", err. Error ()) return}fmt. fprintf (OS. Stdout, "%s IP:%s Network:%s Zone:%s\n", ipaddr.string (), Ipaddr.ip, Ipaddr.network (), Ipaddr.zone)//Baidu, although there is only one domain name, but actually, He corresponds to telecommunications, Netcom, Unicom and other multiple IP address ns, ERR: = Net. Lookuphost (domain) if err! = Nil {fmt. fprintf (OS. Stderr, "Err:%s", err. Error ()) Return}for _, N: = range ns {FMT. fprintf (OS. Stdout, "%s", N)//115.239.210.26    115.239.210.27 These 2 addresses are opened by Baidu}}



/** * Created by Administrator on 13-12-9. */package Mainimport ("net" "FMT" "OS")/** * View the ports occupied by the host Server (service), which are TCP or UDP */func main () {port, err: = Net. Lookupport ("TCP", "Telnet")//view Telnet Server using the port if err! = Nil {fmt. fprintf (OS. Stderr, "specified service not found") return}fmt. fprintf (OS. Stdout, "Telnet port:%d", Port)}



/** * Created by Administrator on 13-12-9. */package Mainimport ("net" "FMT" "OS") Func main () {//TCPADDR inclusion IP and port//convert a host address to tcpaddr. HOST=IP:PORTPTCPADDR, err: = Net. RESOLVETCPADDR ("TCP", "www.baidu.com:80") if err! = Nil {fmt. fprintf (OS. Stderr, "Err:%s", err. Error ()) return}fmt. fprintf (OS. Stdout, "www.baidu.com:80 IP:%s PORT:%d", pTCPAddr.IP.String (), Ptcpaddr.port)}



/** * Created by Administrator on 13-12-10. */package Mainimport ("net" "FMT" "OS" "Io/ioutil") func main () {URL: = "www.baidu.com:80" premotetcpaddr, err: = Net. RESOLVETCPADDR ("TCP4", URL) if err! = Nil {fmt. fprintf (OS. Stderr, "Error:%s", err. Error ()) return}//plocaltcpaddr, err: = Net. RESOLVETCPADDR ("TCP", "127.0.0.1:7070") ptcpconn, err: = Net. DIALTCP ("TCP", nil/*plocaltcpaddr*/, PREMOTETCPADDR) if err! = Nil {fmt. fprintf (OS. Stderr, "Error:%s", err. Error ()) return}n, err: = Ptcpconn.write ([]byte ("head/http/1.0\r\n\r\n")) if err! = Nil {fmt. fprintf (OS. Stderr, "Error:%s", err. Error ()) Return}defer ptcpconn.close () fmt. fprintf (OS. Stdout, "writed:%d\n", N) buf, err: = Ioutil. ReadAll (ptcpconn) r: = Len (buf) fmt. fprintf (OS. Stdout, String (Buf[:r])) FMT. fprintf (OS. Stdout, "readed:%d\n", R)}



/** * Created by Administrator on 13-12-10. */package Mainimport ("net" "FMT" "OS" "Time") Func main () {ptcpaddr, Error: = Net. RESOLVETCPADDR ("Tcp4", ": 7070") if error! = Nil {fmt. fprintf (OS. Stdout, "Error:%s", error. Error ()) Return}ptcplistener, Error: = Net. Listentcp ("TCP4", PTCPADDR) if error! = Nil {fmt. fprintf (OS. Stdout, "Error:%s", error. Error ()) Return}defer Ptcplistener.close () for {ptcpconn, error: = PTCPLISTENER.ACCEPTTCP () if error! = Nil {fmt. fprintf (OS. Stdout, "Error:%s", error. Error ()) Continue}go Connhandler (ptcpconn)}}func Connhandler (Conn *net. Tcpconn) {Defer conn. Close () Now: = time. Now () Conn. Write ([]byte (now. String () + "\ n"))}



/** * Created by Administrator on 13-12-10. */package Mainimport ("net" "FMT" "OS" "Time") Func main () {//url: = "baidu.com:80" url: = "facebook.com:80" conn, Error: = Net . Dialtimeout ("tcp", URL, time.) Second * 10)///with timeout limit of connif error! = nil {fmt. fprintf (OS. Stderr, "Error:%s", error. Error ()) Return}n, error: = conn. Write ([]byte ("head/http/1.0\r\n\r\n")) if error! = Nil {fmt. fprintf (OS. Stderr, "Error:%s", error. Error ()) return}fmt. fprintf (OS. Stdout, "writed:%d", n) buf: = Make ([]byte, 2048) n, error = conn. Read (BUF) if error! = Nil {fmt. fprintf (OS. Stderr, "Error:%s", error. Error ()) return}fmt. fprintf (OS. Stdout, String (Buf[:n])}




/** * Created by Administrator on 13-12-10. */package Mainimport ("net" "FMT" "OS")/** * UDP server */func main () {pudpaddr, err: = Net. RESOLVEUDPADDR ("UDP", ": 7070") if err! = Nil {fmt. fprintf (OS. Stderr, "Error:%s", err. Error ()) Return}pudpconn, err: = Net. LISTENUDP ("UDP", pudpaddr) if err! = Nil {fmt. fprintf (OS. Stderr, "Error:%s", err. Error ()) Return}defer Pudpconn.close () for {buf: = make ([]byte,) n, pudpaddr, err: = PUDPCONN.READFROMUDP (BUF) if err! = Nil {fmt. fprintf (OS. Stderr, "Error:%s", err. Error ()) return}fmt. fprintf (OS. Stdout, "readed:%d", N) n, err = PUDPCONN.WRITETOUDP (buf, pudpaddr) if err! = Nil {fmt. fprintf (OS. Stderr, "Error:%s", err. Error ()) return}fmt. fprintf (OS. Stdout, "writed:%d", N)}}



/** * Created by Administrator on 13-12-10. */package Mainimport ("net" "FMT" "OS")/** * UDP client */func main () {pudpaddr, err: = Net. RESOLVEUDPADDR ("UDP", "127.0.0.1:7070") if err! = Nil {fmt. fprintf (OS. Stderr, "Error resolveudpaddr:%s", err. Error ()) Return}pudpconn, err: = Net. DIALUDP ("UDP", nil, pudpaddr) if err! = Nil {fmt. fprintf (OS. Stderr, "Error dialudp:%s", err. Error ()) return}n, err: = Pudpconn.write ([]byte ("Hello!!! ")) if err! = Nil {fmt. fprintf (OS. Stderr, "Error writetoudp:%s", err. Error ()) return}fmt. fprintf (OS. Stdout, "writed:%d", n) buf: = Make ([]byte, 1024x768) n, _, err = PUDPCONN.READFROMUDP (BUF) if err! = Nil {fmt. fprintf (OS. Stderr, "Error readfromudp:%s", err. Error ()) return}fmt. fprintf (OS. Stdout, "readed:%d  %s", N, String (Buf[:n])}




/** * Created by Administrator on 13-12-10. */package Mainimport ("net" "FMT" "OS")/* Many times, the server handles multiple port snooping! You can use select polling to handle this situation */func main () {LSR, err: = Net. Listen ("TCP", ": 7070") if err! = Nil {fmt. fprintf (OS. Stderr, "Error:%s", err. Error ()) return}for {conn, err: = LSR. Accept () if err! = Nil {fmt. fprintf (OS. Stderr, "Error:%s", err. Error ()) Continue}go Connhandler (conn)}fmt. PRINTLN ("Done!")} Func Connhandler (Conn net. Conn) {defer Conn. Close () var buf[512]bytefor {n, err: = conn. Read (buf[0:]) if err! = Nil {fmt. fprintf (OS. Stderr, "Error:%s", err. Error ()) return}_, err = conn. Write (Buf[0:n]) if err! = Nil {fmt. fprintf (OS. Stderr, "Error:%s", err. Error ()) Return}}}




/** * Created by Administrator on 13-12-10. */package Mainimport ("net" "FMT" "OS" "Bufio") func main () {conn, err: = Net. Dial ("TCP", "192.168.1.97:7070") if err! = Nil {fmt. Sprint (OS. Stderr, "Error:%s", err. Error ()) Return}go func () {var buf [512]bytefor {n, err: = conn. Read (buf[0:]) if err! = Nil {fmt. fprintf (OS. Stderr, "Error:%s", err. Error ()) return}fmt. fprintf (OS. Stdout, String (Buf[0:n])}} () for {fmt. fprintf (OS. Stdout, "\ n input:") Preader: = Bufio. Newreader (OS. Stdin) Line, _, Err: = Preader.readline () if err! = Nil {fmt. fprintf (OS. Stderr, "Error:%s", err. Error ()) Return}conn. Write (line[0:])}}



/** * Created by Administrator on 13-12-10. */package Mainimport ("net" "FMT" "OS")/** UDP Server */func Main () {packetconn, err: = Net. Listenpacket ("UDP", ": 7070") if err! = Nil {fmt. fprintf (OS. Stdout, "Error:%s", err. Error ()) Return}defer packetconn.close () var buf [512]bytefor {n, addr, err: = Packetconn.readfrom (buf[0:]) if err! = nil {f Mt. fprintf (OS. Stdout, "Error:%s", err. Error ()) return}fmt. fprintf (OS. Stdout, "recv:%s", String (Buf[0:n]) _, err = Packetconn.writeto (buf[0:n], addr) if err! = Nil {fmt. fprintf (OS. Stdout, "Error:%s", err. Error ()) Return}}}




/** * Created by Administrator on 13-12-10. */package Mainimport ("net" "FMT" "OS" "Time")/** UDP Client */func Main () {conn, err: = Net. Dial ("UDP", "127.0.0.1:7070") if err! = Nil {fmt. fprintf (OS. Stdout, "Error:%s", err. Error ()) Return}defer Conn. Close () _, err = conn. Write ([]byte ("Hello UDP")) if err! = Nil {fmt. fprintf (OS. Stdout, "Error:%s", err. Error ()) Return}var buf [512]byteconn. Setreaddeadline (time. Now (). ADD (time. Second * 1))  //block until the message is received, set the blocking time to 1 seconds n, err: = conn. Read (buf[0:]) if err! = Nil {fmt. fprintf (OS. Stdout, "Error:%s", err. Error ()) return}fmt. fprintf (OS. Stdout, "recv:%s", String (Buf[0:n])}


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.