废话不多说先上代码:
服务端代码server.go
package main
import (
"net"
"fmt"
"time"
"bytes"
"io"
)
func main() {
add:=new(net.TCPAddr)
add.IP= net.ParseIP("127.0.0.1")
add.Port=7777
tcpl,err:=net.ListenTCP("tcp", add)
if err != nil {
fmt.Println(err)
}
for{
conn,err:=tcpl.Accept()
if err != nil {
fmt.Println(err)
}
handleClient(conn)
}
}
func handleClient(conn net.Conn) {
defer conn.Close()
var buf bytes.Buffer
_, err := io.Copy(&buf, conn)
if err != nil {
fmt.Println("读取错误")
}
fmt.Println(string(buf.Bytes()))
data:=buf.Bytes()
fmt.Println(len(data))
if len(data)==0 {
daytime := time.Now().String()
//不需要关心返回值
conn.Write([]byte(daytime))
}else{
//不需要关心返回值
conn.Write([]byte(string(buf.Bytes())))
}
}
服务端监听本地IP(127.0.0.1)7777端口。当有客户端连接时,获得一个conn 对象,coon 对象是 interface Conn的实现者,
下面先给给出interface 的结构 里面可以看到这个inteface 包含那些方法。
type Conn interface {
// Read reads data from the connection.
// Read can be made to time out and return an Error with Timeout() == true
// after a fixed time limit; see SetDeadline and SetReadDeadline.
Read(b []byte) (n int, err error)
// Write writes data to the connection.
// Write can be made to time out and return an Error with Timeout() == true
// after a fixed time limit; see SetDeadline and SetWriteDeadline.
Write(b []byte) (n int, err error)
// Close closes the connection.
// Any blocked Read or Write operations will be unblocked and return errors.
Close() error
// LocalAddr returns the local network address.
LocalAddr() Addr
// RemoteAddr returns the remote network address.
RemoteAddr() Addr
// SetDeadline sets the read and write deadlines associated
// with the connection. It is equivalent to calling both
// SetReadDeadline and SetWriteDeadline.
//
// A deadline is an absolute time after which I/O operations
// fail with a timeout (see type Error) instead of
// blocking. The deadline applies to all future and pending
// I/O, not just the immediately following call to Read or
// Write. After a deadline has been exceeded, the connection
// can be refreshed by setting a deadline in the future.
//
// An idle timeout can be implemented by repeatedly extending
// the deadline after successful Read or Write calls.
//
// A zero value for t means I/O operations will not time out.
SetDeadline(t time.Time) error
// SetReadDeadline sets the deadline for future Read calls
// and any currently-blocked Read call.
// A zero value for t means Read will not time out.
SetReadDeadline(t time.Time) error
// SetWriteDeadline sets the deadline for future Write calls
// and any currently-blocked Write call.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means Write will not time out.
SetWriteDeadline(t time.Time) error
}
最后读取响应头,读到[]byte 里面 最后转成string 返回给客户端。如果客户端没有发来任何字符,则选择时间返回给客户端。
客户端代码
package main
import (
"net"
"bytes"
"io"
"fmt"
)
func main() {
add:=new(net.TCPAddr)
add.IP= net.ParseIP("127.0.0.1")
add.Port=7777
conn,err:=net.DialTCP("tcp",nil,add)
if err!=nil{
fmt.Println(err)
}
//conn.Write([]byte("hello"))
conn.CloseWrite()
var buf bytes.Buffer
_, err = io.Copy(&buf, conn)
fmt.Println(string(buf.Bytes()))
}
客户端发起请求将收到的数据转成字符串打印出来。客户端可以选择向服务端发数据或者不发数据。如果发数据服务端会将客户端发送的数据转发给客户端,如果没有服务端会把时间转发给客户端。