[Golang] Socket programming----client and server for basic functionality

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

Supports multiple connections.

After the server runs, it enters the accept blocking state. After the accept gets a conn, a co-process is opened, with two threads blocking in read and write respectively. When a data is read, the read data is written to Readchannel before it is processed. After Writechannel obtains a data, writes the data to the Conn.

After the client runs, it accesses the server and then turns on two threads blocking in the read and write channel. After the scan gets a data, writes the data to the Writechannel, and wakes up the blocking thread to write the data to the Conn. When data is returned in the server, the read process is awakened and the data is written to Readchannel.

Of course, there are a lot of details to deal with. such as when the closure of Conn and so on.

Client source

Package Clientimport ("NET" "Git.oschina.net/sdlszjb/unix_socket/errs" "FMT") func StartClient1 () {tcpaddress, err: = Net. RESOLVETCPADDR ("TCP4", "127.0.0.1:1300") if err! = Nil {errs. Error_exit (Err)}conn, err: = Net. DIALTCP ("TCP", nil, tcpaddress) if err! = Nil {errs. Error_exit (err)}writechan: = Make (chan []byte, 1024x768) Readchan: = Make (chan []byte, 1024x768) go writeconnection (conn, Writechan) Go readconnection (conn, Readchan)//go Handlereadchannel (Readchan) for {var s stringfmt. Scan (&s) Writechan <-[]byte (s)}}func readconnection (Conn *net. Tcpconn, Channel Chan []byte] {defer conn. Close () Buffer: = Make ([]byte, 2048) for {n, err: = conn. Read (buffer) if err! = Nil {errs. Error_print (Err) return}println ("Received from:", Conn. Remoteaddr (), String (buffer[:n))//channel <-buffer[:n]}}func writeconnection (conn *net. Tcpconn, Channel Chan []byte] {defer conn. Close () for {select {case data: = <-channel:_, err: = conn. Write (data) if err! = Nil {errs. Error_exit (Err)}println ("Write to:", Conn. RemoteadDr (), String (Data)}}} 

Server-side code:

Package Serverimport ("NET" "Git.oschina.net/sdlszjb/unix_socket/errs" "FMT") var client_num int = 0func StartServer1 () { L, Err: = Net. Listen ("TCP", ": 1300") if err! = Nil {errs. Error_exit (Err)}defer L.close () for {conn, err: = L.accept () if err! = Nil {errs. Error_print (Err) continue}client_num++fmt. Printf ("A new Connection%d.\n", client_num) go handlerconnection (conn)}}func handlerconnection (conn net. Conn) {defer closeconnection (Conn) Readchannel: = Make (chan []byte, 1024x768) Writechannel: = Make (chan []byte, 1024x768) go Readcon Nection (conn, readchannel) go writeconnection (conn, writechannel) for {select {case data: = <-readchannel:if String ( data) = = "Bye" {return}writechannel <-append ([]byte ("Back"), Data ...)}}} Func writeconnection (Conn net. Conn, Channel Chan []byte) {for {select} {case data: = <-channel:println ("Write:", Conn. Remoteaddr (). String (), String (data) _, ERR: = conn. Write (data) if err! = Nil {errs. Error_print (Err) return}}}}func readconnection (conn net. Conn, Channel Chan []byte) {bUffer: = Make ([]byte, 2048) for {n, err: = conn. Read (buffer) if err! = Nil {errs. Error_print (ERR) channel <-[]byte ("Bye")//the need for further improvement here! Break}println ("RECEI:", Conn. Remoteaddr (). String (), String (Buffer[:n])) channel <-Buffer[:n]}}func closeconnection (conn net. Conn) {Conn. Close () client_num--fmt. Printf ("Now,%d connections is alve.\n", Client_num)}

 

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.