This is a creation in Article, where the information may have evolved or changed.
A friend gave a C + + real-time market interface, in which the actual volume of the structure of the deal is as follows:
1typedefstructTagstockreport2 {3WORD wdatasize;//Data Length4WORD wchecked;//Data Validation5 6 CharSzcode[stockcodesize];//stock code, ending with ' /'7 CharSzname[stocknamesize];//stock name, ending with ' /'8 9 intNtradingunit;//Trading Units (number of shares per lot)Ten One LongLtime;//UCT A - floatFlastclose;//prev. closed - floatFOpen;//Open Today the floatFHigh;//Highest - floatFLow;//Minimum - floatFnewprice;//Latest - floatFvolume;//Volume + floatFamount;//turnover - + floatFbuyprice[buysellcount];//Bid A floatFbuyvolume[buysellcount];//The amount of the purchase, at floatFsellprice[buysellcount];//Bid - floatFsellvolume[buysellcount];//Sales Volume -}tstockreport, *pstockreport;
I converted it into the corresponding structure of go, and received the push over the market through the socket.
A few questions: 1, the C language structure of the field alignment rules seem to be different from the go language, some time to go to accept the time, add an empty placeholder field. 2, if a structure, in addition to the shape, floating point, there are some character types. There is no way to assign a single mapping to all the fields of a struct. So I have to take the data slice of the socket is assembled into the structure, you boss, for this hybrid structure, there is no good unified analysis of the socket data flow, the method of assignment.
On the code:
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//data length wchecked uint16//data Check szCODE [12]byte//Stock code, to' /'End SzName [16]int8//stock name, to' /'End Ntradingunit Int32//Trading Units (number of shares per lot)_ Int16//complement Field ltime int32//UCT flastclose float32//prev. fOpen float32//today open FHigh float32//Max FLow float32//Minimum Fnewprice float32//Latest Fvolume float32//Volume Famount float32//turnover Fbuyprice [5]float32//Shen Bid 1,2,3,4,5Fbuyvolume [5]float32//Shen Buy volume 1,2,3,4,5Fsellprice [5]float32//Bid 1,2,3,4,5Fsellvolume [5]float32//Sell volume 1,2,3,4,5}type stockday struct {wdatasize uint16Data length 0-1wchecked uint16Data validation 2-3szCODE StringStock code, to' /'End 4-15SzName StringThe stock name to' /'End Ntradingunit Int32//Trading Units (how many shares per lot) Ltime Int32//UCT flastclose float32//prev. fOpen float32//today open FHigh float32//Max FLow float32//Minimum Fnewprice float32//Latest Fvolume float32//Volume Famount float32//turnover Fbuyprice [5]float32//Shen Bid 1,2,3,4,5Fbuyvolume [5]float32//Shen Buy volume 1,2,3,4,5Fsellprice [5]float32//Bid 1,2,3,4,5Fsellvolume [5]float32//Sell volume 1,2,3,4,5}func Main () {//on 7076 port monitor tcpaddr, err:= Net. RESOLVETCPADDR ("TCP",": 7076") CheckError (ERR) listener, ERR1:= Net. LISTENTCP ("TCP", Tcpaddr) checkerror (ERR1) fmt. Println ("Server Ready") for { //wait for the client connection conn, ERR2:=Listener. Accept ()ifErr! =Nil {/* Usually the server is a service and does not exit because of an error. After an error, continue to wait for the next connection request */FMT. Println (ERR2)Continue} fmt. Println ("request received from client") Go serveclient (conn)}}func serveclient (conn net. Conn) {defer Conn. Close () defer FMT. Println ("Receive data Completion") fmt. Println ("start accepting Data ...") READHQ (conn)return}/* Read validation data */func readdata (conn net. Conn) string {var data bytes. Buffer var buf [512]byte for{n, err:=Conn. Read (buf[0:])ifErr! =Nil {fmt. PRINTLN (ERR)return "" } //Our data is tagged with 0 as the endifBuf[n-1] = =0 {N-1 Remove end tag 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]) ifErr! =Nil {fmt. PRINTLN (ERR)return } B_buf: = bytes. Newbuffer (buf[0:148]) //split the data packets sent over the network 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 turn 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 is the concept of in-memory byte-order, which is to put low-byte into the back. Network transmission is generally used Bigendian, memory byte sequence and CPU, programming to convert. Fmt. PRINTLN (x)//FMT. Println (B_BUF)}return}func SendData (conn net. Conn, data string) {buf:=[]byte (data)/* Add an end tag to a byte byte */buf=Append (buf, 0) _, Err:=Conn. Write (BUF)ifErr! =Nil {fmt. PRINTLN (Err)}}func checkerror (err error) {ifErr! =Nil {fmt. PRINTLN (ERR) OS. Exit (0)}}