欢迎来到代码驿站!

Golang

当前位置:首页 > 脚本语言 > Golang

golang 实现tcp server端和client端,并计算RTT时间操作

时间:2021-06-02 08:22:55|栏目:Golang|点击:

server端代码:

package main 
import (
 "fmt"
 "net"
 "log"
 "os"
)
 
func main() {
 addr := "0.0.0.0:8787"
 tcpAddr, err := net.ResolveTCPAddr("tcp",addr)
 if err != nil {
 log.Fatalf("net.ResovleTCPAddr fail:%s", addr) //等价于print err后,再os.Exit(1)
 }
 
 listener, err := net.ListenTCP("tcp", tcpAddr)
 if err != nil {
 log.Fatalf("listen %s fail: %s", addr, err)
 }else {
 
 log.Println("rpc listening", addr)
 }
 
 for {
 conn, err := listener.Accept()
 if err != nil {
 log.Println("listener.Accept error:", err)
 continue
 }
 
 go handle_Client(conn)
 }
} 
 
func handle_Client(conn net.Conn) {
 defer conn.Close()
 
 var write_buffer []byte = []byte("ok")
 read_buffer := make([]byte, 128)
 
 n, err1 := conn.Read(read_buffer)
 if err1 != nil {
 fmt.Println("ser Read failed:", err1)
 os.Exit(1)
 }
 
 m, err2 := conn.Write(write_buffer)
 if err2 != nil {
 fmt.Println("ser send error:", err2)
 os.Exit(1)
 }
 
 fmt.Println("ser read cli send data count:", n, "msg:", string(read_buffer))
 fmt.Println("ser send data to cli count:", m, "msg:", string(write_buffer)) 
 fmt.Println("********one connetion end*********")
}

client端代码:

package main 
import (
 "fmt"
 "net"
 "os"
 "time"
)
 
func main() {
 conn, err := net.Dial("tcp", "127.0.0.1:8787")
 if err != nil {
 fmt.Println("dial failed:", err)
 os.Exit(1)
 }
 defer conn.Close() 
 
 var write_buffer []byte = []byte("Hi,server. I'm client.")
 read_buffer := make([]byte, 128)
 
 start_time := time.Now()
 n, err2 := conn.Write(write_buffer)
 if err2 != nil {
 fmt.Println("cli send error:", err2)
 os.Exit(1)
 }
 
 m, err1 := conn.Read(read_buffer)
 if err1 != nil {
 fmt.Println("cli Read failed:", err1)
 os.Exit(1)
 }
 response_time := time.Since(start_time)
 RTT_time := float64(response_time.Nanoseconds()) / 1e+06 //ns-->ms 
 fmt.Println(RTT_time) 
 fmt.Println("cli send data to ser count:", n, "msg:", string(write_buffer)) 
 fmt.Println("cli read ser data count:", m, "msg:", string(read_buffer))
}

补充:go 语言 TCP服务器接受客户端字符串信息

我就废话不多说了,大家还是直接看代码吧~

func hand_conn(conn net.Conn) ([]byte){
  defer conn.Close()
  //clientarr := conn.RemoteAddr() //获取连接到的对像的IP地址
  result := bytes.NewBuffer(nil)
  var buf [512]byte
  for {
    n, err := conn.Read(buf[0:])
    result.Write(buf[0:n])
    if err != nil {
      if err == io.EOF {
       break
      }
    }
  }
  //explan_rec_infos(string(result.Bytes()))
  return result.Bytes()
}
func main() {
 fmt.Println("Hello World! go server")
  server, err := net.Listen("tcp", ":9993")
  if err == nil { //若果err为nil,则成功创建
    index := 1
    for {
      index++
      con, _ := server.Accept()
      fmt.Println("有一个客户连接" , index)
      hand_conn(con)
    }
  } else {
    return
  }
}

上一篇:golang数组-----寻找数组中缺失的整数方法

栏    目:Golang

下一篇:Go递归修改文件名的实例代码

本文标题:golang 实现tcp server端和client端,并计算RTT时间操作

本文地址:http://www.codeinn.net/misctech/133869.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有