简体   繁体   English

尝试使用 Ruby 向 ctrader 服务器发送 FIX api 消息但未收到响应

[英]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.尝试查看是否可以从 ctrader 服务器获得响应。
Getting no response and seems to hang at "s.recv(1024)".没有得到回应,似乎挂在“s.recv(1024)”。 So not sure what could be going wrong here.所以不确定这里可能出了什么问题。
I have limited experience with sockets and network coding.我对 sockets 和网络编码的经验有限。
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.注意:我知道有许多 FIX 引擎可用于此目的,但我想自己尝试一下。
ctrader FIX guides ctrader FIX 指南

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. Sockets 默认在读取时阻塞。 When you request 1024 bytes, the call will block until that amount of data has been received.当您请求 1024 字节时,调用将阻塞,直到接收到该数量的数据。 If the sender is not sending you 1024 bytes, the call will block forever.如果发件人没有向您发送 1024 字节,则调用将永远阻塞。

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. FIX 协议在每条消息的开头给出 msg 长度。 So you need to read a very short bit, then parse the msg length, then read exactly that amount.所以你需要读一段很短的内容,然后解析 msg 长度,然后准确地读取那个数量。 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. sockets还有另一种策略,称为非阻塞模式。 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.这是一个复杂的主题,您需要研究,但通常在您不想阻止并需要阅读任意长度的消息时使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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