来一个udp版本的吧:
package main
import (
"fmt"
"log"
"net"
"net/http"
"net/rpc"
"os"
)
type UDPPush struct {
}
type PushRequest struct {
A string
}
type PushResponse struct {
Data string
}
var socket *net.UDPConn
var remoteAddr *net.UDPAddr
var err error
func main() {
// 创建UDP监听
socket, err = net.ListenUDP("udp4", &net.UDPAddr{
IP: net.IPv4(0, 0, 0, 0),
Port: 8080,
})
if err != nil {
fmt.Println("监听失败!", err)
return
}
fmt.Println("start UDP server!")
defer socket.Close()
go doUDP() //通过协程来使主进程进行rpc的创建
// 以下是创建rpc服务器
rpc.Register(new(UDPPush)) // 注册rpc服务
rpc.HandleHTTP() // 采用http协议作为rpc载体
lis, err := net.Listen("tcp", "127.0.0.1:8095")
if err != nil {
log.Fatalln("fatal error: ", err)
}
fmt.Fprintf(os.Stdout, "%s", "start rpc connection!\n")
http.Serve(lis, nil)
}
func (*UDPPush) PushData(req PushRequest, res *PushResponse) error {
doPush(req)
res.Data = "success!"
return nil
}
func doPush(req PushRequest) {
// 发送数据
sendData := []byte(req.A)
fmt.Println("remoteAddr:", remoteAddr, "sendData:", sendData)
_, err = socket.WriteToUDP(sendData, remoteAddr)
if err != nil {
return
fmt.Println("发送数据失败!", err)
}
}
func doUDP() {
for {
// 读取数据
data := make([]byte, 4096)
var read int
read, remoteAddr, err = socket.ReadFromUDP(data)
if err != nil {
fmt.Println("读取数据失败!", err)
continue
}
fmt.Println(read, remoteAddr)
fmt.Printf("%s\n\n", data)
sendData := []byte("this is from server")
_, err = socket.WriteToUDP(sendData, remoteAddr)
if err != nil {
return
fmt.Println("发送数据失败!", err)
}
}
}
udp客户端
package main
import (
"fmt"
"net"
)
func main() {
// 创建连接
socket, err := net.DialUDP("udp4", nil, &net.UDPAddr{
IP: net.IPv4(192, 168, 2, 104),
Port: 8080,
})
if err != nil {
fmt.Println("连接失败!", err)
return
}
defer socket.Close()
// 发送数据
sendData := []byte("hello server!")
_, err = socket.Write(sendData)
if err != nil {
fmt.Println("发送数据失败!", err)
return
}
for {
// 接收数据
data := make([]byte, 4096)
read, remoteAddr, err := socket.ReadFromUDP(data)
if err != nil {
fmt.Println("读取数据失败!", err)
return
}
fmt.Println(read, remoteAddr)
fmt.Printf("%s\n", data)
}
}
rpc客户端:
package main
import (
"fmt"
"log"
"net/rpc"
)
// 算数运算请求结构体
type PushRequestClient struct {
A string
}
type PushResponseClient struct {
Data string
}
func main() {
conn, err := rpc.DialHTTP("tcp", "127.0.0.1:8095")
if err != nil {
log.Fatalln("dialing error: ", err)
}
req := PushRequestClient{"this data is from rpc client....\n"}
var res PushResponseClient
err = conn.Call("UDPPush.PushData", req, &res) // 推送消息,TCPPush和rpcServer里面的是一样的,并且PushData在rpcServer里面是可导出的
if err != nil {
log.Fatalln("push error: ", err)
}
fmt.Println(res.Data)
}
搜索
标签
study
ab
amap
apache
apahe
awk
aws
bat
centos
CFS
chrome
cmd
cnpm
composer
consul
crontab
css
curl
cygwin
devops
di
docker
docker,docker-compose
ethereum
excel
fiddler
fluentd
framework
front-end
git
gitgui
github
glide
go
golang
gorm
grafana
gzip
ioc
item2
iterm2
javascript
jenkins
jsonp
kafka
laradock
laravel
larval
linux
liunux
log
mac
mac, wi-fi
macos
magento
mariaDB
minikube
mongoDB
msp
mysql
netbeans
nginx
nodejs
nohup
npm
nsq
oracle
php
php-fpm
php7
phpstorm
php扩展
Protobuf
python
redis
scp
server
shell
soap
socket
socket5
sql
sre
ssdb
ssh
ssl
study
sublime
swift
system
td-agent
uml
v2ray
vagrant
vagrnat
vim
vpn
vue
vue.js
webpack
webrtc
websocket
webtatic
windows
windows7
word
wps
xdebug
yarn
yii2
yum
zookeeper
世界国家
互联网
以太坊
分类
前端
小程序
打印机
排序算法
搞笑
权限
粤语
缓存
网络
虚拟机
视频
设计模式
项目管理
热门文章
友情链接