Go实战--实现一个简单的tcp服务端和客户端(The way to go)
2017-04-24 20:45
966 查看
生命不止,继续 go go go !!!
之前介绍了go为我们提供的net/http包,很方便的创建一些api。
今天就来点实战,写一个简单的tcp的服务端、客户端程序。
按照国际惯例,还是先介绍一点点基础知识。
* net.Listen*
Listen announces on the local network address laddr. The network net must be a stream-oriented network: “tcp”, “tcp4”, “tcp6”, “unix” or “unixpacket”. For TCP and UDP, the syntax of laddr is “host:port”, like “127.0.0.1:8080”. If host is omitted, as in “:8080”, Listen listens on all available interfaces instead of just the interface with the given host address. See Dial for more details about address syntax.
Listening on a hostname is not recommended because this creates a socket for at most one of its IP addresses.
所以我们的server端就可以这么写:
bufio.NewReader
NewReader returns a new Reader whose buffer has the default size.
Write
Write implements the Conn Write method.
net.Dial
Dial connects to the address on the named network.
See func Dial for a description of the network and address parameters.
基础知识介绍差不多了,下面我们开始写代码了。
tcp_server.go
tcp_client.go
之前介绍了go为我们提供的net/http包,很方便的创建一些api。
今天就来点实战,写一个简单的tcp的服务端、客户端程序。
按照国际惯例,还是先介绍一点点基础知识。
* net.Listen*
Listen announces on the local network address laddr. The network net must be a stream-oriented network: “tcp”, “tcp4”, “tcp6”, “unix” or “unixpacket”. For TCP and UDP, the syntax of laddr is “host:port”, like “127.0.0.1:8080”. If host is omitted, as in “:8080”, Listen listens on all available interfaces instead of just the interface with the given host address. See Dial for more details about address syntax.
Listening on a hostname is not recommended because this creates a socket for at most one of its IP addresses.
func Listen(net, laddr string) (Listener, error)
所以我们的server端就可以这么写:
ln, _ := net.Listen("tcp", "localhost:8081")
bufio.NewReader
NewReader returns a new Reader whose buffer has the default size.
func NewReader(rd io.Reader) *Reader
Write
func (c *IPConn) Write(b []byte) (int, error)
Write implements the Conn Write method.
net.Dial
func (d *Dialer) Dial(network, address string) (Conn, error)
Dial connects to the address on the named network.
See func Dial for a description of the network and address parameters.
基础知识介绍差不多了,下面我们开始写代码了。
tcp_server.go
package main import "net" import "fmt" import "bufio" import "strings" func main() { fmt.Println("Launching server...") // listen on all interfaces ln, _ := net.Listen("tcp", "localhost:8081") // accept connection on port conn, _ := ln.Accept() // run loop forever (or until ctrl-c) for { // will listen for message to process ending in newline (\n) message, _ := bufio.NewReader(conn).ReadString('\n') // output message received fmt.Print("Message Received:", string(message)) // sample process for string received newmessage := strings.ToUpper(message) // send new string back to client conn.Write([]byte(newmessage + "\n")) } }
tcp_client.go
package main import "net" import "fmt" import "bufio" import "os" func main() { // connect to this socket conn, _ := net.Dial("tcp", "localhost:8081") for { // read in input from stdin reader := bufio.NewReader(os.Stdin) fmt.Print("Text to send: ") text, _ := reader.ReadString('\n') // send to socket fmt.Fprintf(conn, text + "\n") // listen for reply message, _ := bufio.NewReader(conn).ReadString('\n') fmt.Print("Message from server: "+message) } }
相关文章推荐
- Go实战--实现一个简单聊天室chatroom(The way to go)
- Go实战--实现简单的restful api(The way to go)
- Go实战--实现一个并发时钟服务器(The way to go)
- Go实战--实现一个单向链表(The way to go)
- Go实战--实现一个自己的网络请求日志httplogger(The way to go)
- 01 TCP Socket 编程_一个简单的服务端程序和客户端程序实现
- Go实战--go中编码转换(The way to go)
- 用 TCP 协议写一个客户端和一个服务端,实现上传文件
- go语言实现一个简单的http客户端抓取远程url的方法
- 一个简单的TCP客户端、服务端会话程序(C#)
- Go实战--go语言中执行shell脚本(The way to go)
- 用 TCP 协议写一个客户端和一个服务端,实现上传文件
- Go实战--通过net/smtp发送邮件(The way to go)
- 利用Tcp和socket实现的客户端与服务端的简单通信
- Go语言服务器开发之简易TCP客户端与服务端实现方法
- Go实战--go语言操作sqlite数据库(The way to go)
- 一个简单的TCP客户端、服务端会话程序(C#)
- Go实战--net/http中JSON的使用(The way to go)
- 基于TCP实现简单的客户端服务端的消息传递
- linux网络编程之用socket实现简单客户端和服务端的通信(基于TCP)