简体   繁体   中英

Trying to send a FIX api message to ctrader server using Ruby but receiving no response

Trying to see if I can get a response from ctrader server.
Getting no response and seems to hang at "s.recv(1024)". So not sure what could be going wrong here.
I have limited experience with sockets and network coding.
I have checked my login credentials and all seems ok.
Note: I am aware of many FIX engines that are available for this purpose but wanted to try this on my own.
ctrader FIX guides

require 'socket'

hostname = "h51.p.ctrader.com"
port = 5201

#constructing a fix message to see what ctrader server returns
#8=FIX.4.4|9=123|35=A|49=demo.ctrader.*******|56=cServer|57=QUOTE|50=QUOTE|34=1|52=20220127-16:49:31|98=0|108=30|553=********|554=*******|10=155|

fix_message = "8=FIX.4.4|9=#{bodylengthsum}|" + bodylength + "10=#{checksumcalc}|"

s = TCPSocket.new(hostname, port)
s.send(fix_message.force_encoding("ASCII"),0)
print fix_message 
puts s.recv(1024)
s.close   

Sockets are by default blocking on read. When you request 1024 bytes, the call will block until that amount of data has been received. If the sender is not sending you 1024 bytes, the call will block forever.

You need another strategy to read from the socket in order to not block. The FIX protocol gives the msg length at the start of each message. So you need to read a very short bit, then parse the msg length, then read exactly that amount. Then you will not block waiting for data that will never arrive.

There is also another strategy for sockets that is called non-blocking mode. This is complex topic, which you need to research, but often used when you don't want to block and need to read arbitary length messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM