如何为FIX消息计算正确的校验和? [英] How do I calculate correct checksum for FIX messages?

查看:221
本文介绍了如何为FIX消息计算正确的校验和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码获取成功的LOGON消息,但是当我在第二条消息中发送quote-request时,它没有回复& 30秒后给我发送了一个心跳。有人告诉我,我没有正确计算校验和,所以这就是为什么Fix-server忽略了价格请求消息。你能不能给我任何线索,为什么它不能用于第二条消息?



我尝试过:



The following code is getting successful LOGON messages, but when i send quote-request in the 2nd message, its not replying & sending me a heartbeat after 30 seconds.Someone told me i'm not correctly calculating checksum, so that's why Fix-server ignoring the price-request message.Can you give me any clue why it's not working for the 2nd message?

What I have tried:

public static int total;

time = DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss.fff");

                                body = "35=V|49=" + FixID + "|56=CSERVER|34="+msgSeqenceNum+"|52=" + time + "|50=QUOTE|57=QUOTE|262=13203481x|263=1|264=1|265=1|146=1|55=1|267=2|269=0|269=1|";

                                // body length
                                bodylen = body.Length;
                                string fix = "8=FIX.4.4|9=" + bodylen + "|";

                                // message
                                message = fix + body;

                                message = message.Replace('|', '\u0001');
                            int total2 = 0;
                                messageBytes = Encoding.ASCII.GetBytes(message);//converted to 0's &1s
                                for (int i = 0; i < message.Length; i++)
                                    total2 += messageBytes[i];

                                checksum = total2 % 256;
                                checksumStr = checksum.ToString().PadLeft(3, '0');

                                sum = "10=" + checksumStr + "|";
                                m = fix + body + sum;
                                m = m.Replace('|', '\u0001');

推荐答案

引用:

你能告诉我为什么它不能用于第二条消息吗?

Can you give me any clue why it's not working for the 2nd message?





查看总计定义的位置..在获得第二条消息的校验和之前,是否重置了total的值?



我会打破这段代码,得到校验和





look where you have 'total' defined .. do you reset the value of total before you get the checksum for your second message ?

I would break out this code, to get the checksum

message = message.Replace('|', '\u0001');
 byte[] messageBytes = Encoding.ASCII.GetBytes(message);
 for (int i = 0; i < message.Length; i++)
 total += messageBytes[i];

 int checksum = 0;
 foreach (byte chData in messageBytes)
 {
     checksum += chData;
 }

 checksum = total % 256;
 string checksumStr = checksum.ToString().PadLeft(3, '0');





进入一个单独的函数/程序/方法 - 你重复代码,真正的坏主意 - 如果你打破每个位置的代码,你可以去





into a separate function/procedure/method - you repeat the code, real bad idea - if you break that code out of each location you can then go

checksumStr = getChecksum(message);





例如..那么你可以删除这场灾难



for example .. then you can remove this disaster

Quote:

public static int total;

public static int total;

from your client



虽然我是在这里,我将通过连接字符串来构建这样的消息,我将花费2美分 - 它几乎不可用于测试 - 我希望有人为我做一个消息构建器类,为每种类型的消息(所以它作为一个抽象类开始,你将值传递给它,它很好地构建了消息 - 你的消息因为它们很难扩展,调试等 - 我想你很幸运,你不为我工作:)。

from your client

While I'm here, I'll throw in my 2 cents worth on building messages like that by concatenating strings - its barely ok for a test - I'd expect someone working for me to make a message builder class, for each type of message (so it starts as an abstract class), that you pass the values to it and it builds the message nicely - your messages as they are are hard to extend, debug etc - I guess you're lucky you're not working for me :)


这篇关于如何为FIX消息计算正确的校验和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆