Golang Socket Programming (III): Concurrent server

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

I. Overview

The previous article implemented a server and client communication, completed the function of lowercase capitalization, but a single-tasking response: The client sends the connection to receive the response, the program ends, and the server receives the data response data is also finished! As far as practical needs are concerned, it is not very useful, so now we add concurrency to both the client and the server.

Logic is actually very simple, is the use of Golang Gorutine, once the new connection, open a gorutine to deal with, and then respond, until the client closes the connection.

Second, the service side

1
2
3
4
5
6
7
8
9
Ten
One
A
-
-
the
-
-
-
+
-
+
A
at
-
-
-
-
-
in
-
to
+
-
the
*
$
Panax Notoginseng
-
the
+
A
the
+
-
$
$
-
-
the
-
Package   main
Import (
"NET"
"FMT"
"Strings"
)
func handle(conn net. Conn){
defer Conn. Close () //closed connection
FMT. Println ("Connect:", Conn. Remoteaddr ())
For {
//As long as the client is not disconnected, remains connected, reads the data
data: = make ([]byte, 2048)
N, err: = conn. Read (data)
//Data length of 0 indicates that the client connection has been disconnected
if n = = 0{
FMT. Printf ("%s has disconnect", Conn. Remoteaddr ())
Break
}
if err! = Nil{
FMT. PRINTLN (ERR)
Continue
}
FMT. Printf ("Receive data [%s] from [%s]", string(Data[:n]), Conn. Remoteaddr ())
//Turn Capitals
Rspdata: = Strings. ToUpper (string(Data[:n]))
_, Err = conn. Write ([]byte(rspdata))
if err! = Nil{
FMT. PRINTLN (ERR)
Continue
}
}
}
func main(){
Listener, err: = Net. Listen ("TCP", ": 8899")
if err! = Nil{
FMT. PRINTLN (ERR)
return
}
FMT. Println ("Start listen localhost:8899")
For {
//Start loop receive Client connection
conn, err: = Listener. Accept ()
if err! = Nil{
FMT. PRINTLN (ERR)
return
}
//Once a client connection is received, a new gorutine is opened to handle this connection
Go handle (conn)
}
}

Third, the client

1
2
3
4
5
6
7
8
9
Ten
One
A
-
-
the
-
-
-
+
-
+
A
at
-
-
-
-
-
in
-
to
+
 Import (
"NET"
"FMT"
)
func main(){
conn, err: = Net. Dial ("TCP", ": 8899") //connection server
if err! = Nil{
FMT. PRINTLN (ERR)
return
}
FMT. Println ("Connect to localhost:8899 Success")
defer Conn. Close ()
For {
//always cycle through user data, send to server processing
FMT. Print ("Please input send data:")
var a string
FMT. Scan (&a)
if a = = "Exit"{break} //Add an exit mechanism, the user enters exit, exits
_, Err: = conn. Write ([]byte(a))
if err! = Nil{
FMT. PRINTLN (ERR)
return
}
data: = make ([]byte, 2048)
N, err: = conn. Read (data)
if err! = Nil{
FMT. PRINTLN (ERR)
Continue
}
FMT. Println ("Response data:", string(Data[:n]))
}
}

Four, the operation

At this point, we open a server, open two clients to test:

At this point, a perfect multi-concurrent service end is complete!

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.