package mainimport("bytes"
"encoding/binary"
"fmt"
"github.com/axgle/mahonia"
//"gopkg.in/mgo.v2"
//"gopkg.in/mgo.v2/bson"
//"log"
//"io"
"net"
"os"
//"time")
const (
BZ= "PUTDATA"MongodbUrl= "localhost:27017")
type StockFile struct {
wDataSize uint16//数据长度
wChecked uint16//数据校验
szCode [12]byte //股票代码,以'\0'结尾
szName [16]int8 //股票名称,以'\0'结尾
nTradingUnit int32//交易单位(每手多少股)//_ int16 //补位字段
ltime int32//UCT
fLastClose float32//昨收
fOpen float32//今开
fHigh float32//最高
fLow float32//最低
fNewPrice float32//最新
fVolume float32//成交量
fAmount float32//成交额
fBuyPrice [5]float32 // 申买价1,2,3,4,5fBuyVolume [5]float32 // 申买量1,2,3,4,5fSellPrice [5]float32 // 申卖价1,2,3,4,5fSellVolume [5]float32 // 申卖量1,2,3,4,5}
type StockDay struct {
wDataSize uint16//数据长度0-1wChecked uint16//数据校验2-3szCode string//股票代码,以'\0'结尾4-15szName string//股票名称,以'\0'结尾
nTradingUnit int32//交易单位(每手多少股)
ltime int32//UCT
fLastClose float32//昨收
fOpen float32//今开
fHigh float32//最高
fLow float32//最低
fNewPrice float32//最新
fVolume float32//成交量
fAmount float32//成交额
fBuyPrice [5]float32 // 申买价1,2,3,4,5fBuyVolume [5]float32 // 申买量1,2,3,4,5fSellPrice [5]float32 // 申卖价1,2,3,4,5fSellVolume [5]float32 // 申卖量1,2,3,4,5}
func main() {//在7076端口监听
tcpAddr, err := net.ResolveTCPAddr("tcp", ":7076")
checkError(err)
listener, err1 := net.ListenTCP("tcp", tcpAddr)
checkError(err1)
fmt.Println("服务器准备就绪")for{//等待客户端的连接
conn, err2 :=listener.Accept()if err !=nil {/*通常服务端为一个服务,不会因为错误而退出。出错后,继续等待下一个连接请求*/fmt.Println(err2)continue}
fmt.Println("收到客户端的请求")
go ServeClient(conn)
}
}
func ServeClient(conn net.Conn) {
defer conn.Close()
defer fmt.Println("接收数据完成")
fmt.Println("开始接受数据...")
ReadHq(conn)return}/*读取验证数据*/func ReadData(conn net.Conn) string {
var data bytes.Buffer
var buf [512]bytefor{
n, err :=conn.Read(buf[0:])if err !=nil {
fmt.Println(err)return ""}//我们的数据以0做为结束的标记if buf[n-1] ==0 {//n-1去掉结束标记0
data.Write(buf[0 : n-1])break}else{
data.Write(buf[0:n])
}
}returnstring(data.Bytes())
}
func ReadHq(conn net.Conn) {
var x StockDay
var buf [1480]byte
dec := mahonia.NewDecoder("gbk")for{
_, err := conn.Read(buf[0:148])if err !=nil {
fmt.Println(err)return}//b_buf := bytes.NewBuffer(buf[0:148])//分拆网络传过来的数据包
binary.Read(bytes.NewBuffer(buf[0:2]), binary.LittleEndian, &x.wDataSize)
binary.Read(bytes.NewBuffer(buf[2:4]), binary.LittleEndian, &x.wChecked)
x.szCode= string(buf[4:16])
x.szName= dec.ConvertString(string(buf[16:32])) \\GBK 转 UTF8
binary.Read(bytes.NewBuffer(buf[32:36]), binary.LittleEndian, &x.nTradingUnit)
binary.Read(bytes.NewBuffer(buf[36:40]), binary.LittleEndian, &x.ltime)
binary.Read(bytes.NewBuffer(buf[40:44]), binary.LittleEndian, &x.fLastClose)
binary.Read(bytes.NewBuffer(buf[44:48]), binary.LittleEndian, &x.fOpen)
binary.Read(bytes.NewBuffer(buf[48:52]), binary.LittleEndian, &x.fHigh)
binary.Read(bytes.NewBuffer(buf[52:56]), binary.LittleEndian, &x.fLow)
binary.Read(bytes.NewBuffer(buf[56:60]), binary.LittleEndian, &x.fNewPrice)
binary.Read(bytes.NewBuffer(buf[60:64]), binary.LittleEndian, &x.fVolume)
binary.Read(bytes.NewBuffer(buf[64:68]), binary.LittleEndian, &x.fAmount)
binary.Read(bytes.NewBuffer(buf[68:88]), binary.LittleEndian, &x.fBuyPrice)
binary.Read(bytes.NewBuffer(buf[88:108]), binary.LittleEndian, &x.fBuyVolume)
binary.Read(bytes.NewBuffer(buf[108:128]), binary.LittleEndian, &x.fSellPrice)
binary.Read(bytes.NewBuffer(buf[128:148]), binary.LittleEndian, &x.fSellVolume)//binary.Read(b_buf, binary.LittleEndian, &x) //binary.LittleEndian 是内存中的字节序的概念,就是把低字节的放到了后面。网络传输一般用BigEndian,内存字节序和cpu有关,编程时要转化。
fmt.Println(x)//fmt.Println(b_buf)
}return}
func SendData(conn net.Conn, data string) {
buf :=[]byte(data)/*向 byte 字节里添加结束标记*/buf=append(buf, 0)
_, err :=conn.Write(buf)if err !=nil {
fmt.Println(err)
}
}
func checkError(err error) {if err !=nil {
fmt.Println(err)
os.Exit(0)
}
}