Serializing object ready to send over TCPClient Stream

后端 未结 5 1263
忘掉有多难
忘掉有多难 2021-01-03 03:05

I\'ve got a server and client set up using TcpListener and TcpClient.

I want to send an object to my server application for processing.

5条回答
  •  北海茫月
    2021-01-03 03:43

    You can simply decorate your House class with the [Serializable] attribute. (You do not need to define all the other stuff as posted in the other answer)

    You can then send this object on the wire by serializing it using the BinaryFormatter class.

    Have you considered setting up a WCF service instead of using TcpListener and TcpClient ? Makes life a lot easier.

    For instance you could define a service that returned a house

    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        House GetHouse(int houseId);
    }
    

    See this real world example.

提交回复
热议问题