简体   繁体   English

TCP套接字中有多个用于多种用途的客户端?

[英]Multiple clients for multiple purposes in TCP socket?

After seeing some examples at my class, I know that if I want to send a "TypeA" object to server and receive a "ProcessedA" object as a result, I only need one client class. 在班上看到一些示例之后,我知道如果我想向服务器发送“ TypeA”对象并因此收到“ ProcessedA”对象,则只需要一个客户端类。

But if I want to send "TypeA", "TypeB", and "TypeC" objects (not at the same time) to server, do I need to make 3 different client classes, each one of which sends objects of one of those data types, or I only need to make one client class and write 3 different "send" methods? 但是,如果要向服务器发送“ TypeA”,“ TypeB”和“ TypeC”对象(而不是同时发送),是否需要创建3个不同的客户端类,每个客户端类都发送其中一个数据的对象类型,还是只需要制作一个客户端类并编写3种不同的“发送”方法?

You can have only one method if the objects you send inherits from one unique class or interface, and the same logic is applied to the result class. 如果您发送的对象是从一个唯一的类或接口继承的,并且对结果类采用相同的逻辑,则只能使用一种方法。 It's a bit hard to understand what you need if you don't include a sample code of what you are trying (as is noticed in another post related to your request). 如果您不包括所尝试内容的示例代码,那么很难理解您所需要的内容(正如在与您的请求相关的另一篇文章中所注意到的那样)。 If this condition can be met by your needs, you can use the instanceof operator inside the server method to detect the type of the received object and cast it to the known child type. 如果您的需求可以满足此条件,则可以在服务器方法内使用instanceof运算符检测接收到的对象的类型并将其强制转换为已知的子类型。 And apply the same logic to proccess the response in the client. 并应用相同的逻辑在客户端中处理响应。

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

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