开发者

Socket programming; shall I use pInvoke or write c# version of existing c++ app

开发者 https://www.devze.com 2022-12-21 09:22 出处:网络
I have been assigned t开发者_开发知识库o a task where I need to connect to a socket and receive a lot of updates (3K-4K messages per second) I also need to send some message to server for authenticati

I have been assigned t开发者_开发知识库o a task where I need to connect to a socket and receive a lot of updates (3K-4K messages per second) I also need to send some message to server for authentication and configuration.

There is an existing C++ apps given by the data provider and they have no c# sample. I have been trying to write this app with C# but having some difficulties; especially with marshalling structs containing variable length arrays etc.. Am I on the right path to do this?

I have been advised to use PInvoke but looks like I need to first modify their C++ sample to make it API like and then I can invoke it C# properly. And I am not sure about the performance with PInvoke.


Why not use C++ if the app is already in that?
If there is a reason to use C# I would always re-write the C++ if it is feasable. You would be writing new code in C# so you may as well do a complete migration rather than a cumbersome mixture.
I certainly wouldn't use pinvoke for something like network programming... it's complicated enough as it is without adding that. There's an inevitable performance hit with it although what that is exactly, "it depends".


P/Invoke has a performance penalty. C# has a very nice networking programming model and I really suggest that you give it another try. The benefit will be that you have a fully managed, more efficient and maintainable application.

Of course if the existing application is huge and you need a lot of time to port it to C#, you could consider p/invoke. However, since you still need to modify it, you lose some of the advantages of this route.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号