Receive data, and display, code interpretation

Source: Internet
Author: User
Func recv (clnsck net. Conn) {
  buf: = Make ([]byte, 1024x768)
  datalen, err: = (BUF)

  if err! = Nil {
    FMT. PRINTLN (Err)
  msg: = String (buf[:d Atalen])
  //Displays the received message

  FMT. PRINTLN (msg)
1.func recv (clnsck net. Conn)
Clnsck Net. Conn not very clear, net. Conn is a type that should be a listener type and defines a clnsck variable
2. BUF: = Make ([]byte, 1024)
But defined as a slice slice (defined by make, so each source array element is 0, altogether 1024),
The length capacity is 1024 and can read long enough (1024) character information.
Where byte refers to the 8 characters of Int8 2 (0~255 ASCLL2 code)
3.  Datalen, err: = (BUF) () where type clnsck defines the Read () method,
The parameter of its method is a slice slice, and the return value includes an int type representing the byte length (assigned to Datalen).
Another error type was assigned to err. Read to the end of the data stream (Err==io. EOF), break;
4.msg: = String (buf[:d Atalen])
Msg infers that it is a string type, buf[:d Atalen] means
From the 0 bits of the But slice until the first one of the datalen numbers,
Note that the numbers are shown here, one of the 0~255 numbers.
String () Converts all elements of the buf slice into character formatting.
Finally, the information obtained from CLNSCK does not include the last character (usually a carriage return), which can be known from the Datalen.

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