TCP Client for socket communication in go language

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

1. Usage:

(1) Define the remote IP address. Use Net. The Resolvetcpaddr () method, which defines a TCP address as the destination connection address.

(2) Call net. DIALTCP ("TCP", nil,remoteaddress) method to establish a connection to the remoteaddress. The three parameters here are: protocol name, local IP, remote IP

(3) You can learn to invoke some common methods in connection pairs. For learning.

(4) Try to write some messages. For example, the content of the request response header is simulated. Use: Conn. Write ([]byte ("head/http/1.0\r\n\r\n"))

(5) Receiving content: Use the ReadAll method under the Ioutil package. Direct Receive plus []byte, then enter string input.

2. Code:

Package Mainimport ("FMT" "Io/ioutil" "NET") Func main () {var remoteaddress, _ = net. RESOLVETCPADDR ("TCP4", "www.baidu.com:80")//Generate a net. Tcpaddr to the statue. var conn, err = net. Dialtcp ("TCP4", Nil, remoteaddress)//Incoming protocol, native address (nil passed), remote address, get connection. If err! = Nil {//If the connection fails. is returned. Fmt. PRINTLN ("Connection error:", err) Return}var remoteipaddress = conn. REMOTEADDR ()//Get the IP address method. Fmt. PRINTLN ("Remote IP address is:", remoteipaddress)//output: 220.181.111.188:80var localipaddress = conn. LOCALADDR () fmt. PRINTLN ("Local IP address is:", localipaddress)//output: 192.168.1.9:45712conn. Write ([]byte ("head/http/1.0\r\n\r\n"))//try to send some information. var reciverbuffer []byte//defines an empty slice to receive the result. Len, err: = conn. Read (Reciverbuffer)//Returns the number of bytes received. Bys, err: = Ioutil. ReadAll (conn)//Receive messages. If err! = Nil {fmt. PRINTLN ("Receive error:", err)}//var Recivetext = string (Reciverbuffer[0:len]) var recivetext = string (bys) fmt. PRINTLN (RECIVETEXT) Conn. Close ()//off connect FMT. PRINTLN ("Program End")}

3. Operation Effect:

The remote IP address is: 220.181.111.188:80 The local IP address is: 192.168.1.9:45857http/1.1 okdate:tue, 13:37:12 gmtcontent-type:text /htmlcontent-length:14613last-modified:wed, 02:48:32 Gmtconnection:closevary:accept-encodingset-cookie: baiduid=e966c0af917c9d123196e010ebb541e2:fg=1; Expires=thu, 31-dec-37 23:55:55 GMT; max-age=2147483647; path=/; Domain=.baidu.comset-cookie:bidupsid=e966c0af917c9d123196e010ebb541e2; Expires=thu, 31-dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.comset-cookie:pstm=1457444232; Expires=thu, 31-dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.comset-cookie:bdsvrtm=0; path=/p3p:cp= "OTI DSP COR IVA our IND COM" Server:bws/1.1x-ua-compatible:ie=edge,chrome=1pragma:no-cachecache-control : No-cachebdpagetype:1bdqid:0xc58fe0370008a163bduserid:0accept-ranges:bytes program ended successfully: Process exit code 0.

  

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.