Problem with Socket and Serialization C#

前端 未结 6 1070
陌清茗
陌清茗 2021-01-27 13:23

I implemented a Client and Server model that uses Socket with thread

When I want to pass only a string from Client to the Server, it works. But I want to pass an object

6条回答
  •  孤独总比滥情好
    2021-01-27 13:40

    To answer your second question: for maximum maintainability, the class Command should be in a separate assembly that both Client and Server reference.

    To answer your first question: you are attempting to deserialize from an empty stream on your server, just as the exception tells you. You need to copy the bytes you read from the clientStream into the memoryStream before you deserialize from the memoryStream. Alternatively, use the clientStream directly rather than using the memoryStream; this may require reconsidering your protocol.

    Finally, I wholeheartedly agree with @Andrey: consider using WCF. It's way way way better than raw sockets.

提交回复
热议问题