2

Two computers have to communicate via TCP/IP to synchronize a certain process flow. What would be the advantage to use the wrapper classes TcpClient & TcpServer over a Socket object?

I have programmed it using the first but somehow it seems for me to complicated and could be much easier solved just using the latter. Any good advice for me?

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • 2
    http://stackoverflow.com/q/685995/5311735 – Evk Jun 06 '16 at 08:36
  • @Evk I should have searched harder :) – Mong Zhu Jun 06 '16 at 09:03
  • TcpClient and TcpServer inherit the socket class and all three are identical except the constructors are different. The socket is the client in the TcpClient and TcpServer. The TcpClient and TcpSocket are wrappers for the socket class and hide a lot of properties. Theat is why you often need to use the client. – jdweng Jun 06 '16 at 09:25

1 Answers1

2

The idea is that with the wrapper classes much of the code that you are likely to want has already been written for you.

Advantages of using the wrapper should be:

  • Validation already done
  • Less code to write
  • Already tested extensively
  • Code re-use is to be applauded where it makes sense to do so

Advantages of rolling your own:

  • You get exactly what you want
  • You can create your own syntax

Disadvantages of rolling your own:

  • You have to write ALL the code, including tests
  • If you are like me, you are probably not as knowledgeable as the specialist who wrote the wrapper
  • As a result it is likely that the resulting code could be less efficient than the code in the wrapper.

The decision is always yours. After all, you could actually rewrite the whole framework if you wanted to do so, but why would you bother?

You need to look at what is provided for you by the wrapper and decide for yourself whether it provides what you need. If it does, then I would say use it. If it fails to meet your requirements either write your own or extend the wrapper so that it does do what you want.

Hope that helps.

oldcoder
  • 342
  • 2
  • 12
  • I don't want to rewrite the classes. :) the alternative to the use of the wrapper classes `TcpClient` and `TcpServer` is the use of `Socket` not writing my own. They have almost the same functionality in respect to connecting and sending and receiving data. I have the feeling that the `Socket` class suffices entirely to implement bidirectional communication protokolls – Mong Zhu Jun 06 '16 at 09:07
  • 1
    As I said in my post, you have to make an informed decision. If the socket does what you need, use it. If it does not meet your needs, you either need to use the wrapper or write your own code to provide the additional functionality. Regrettably nobody but you can determine whether the sockets on their own will do what you want... – oldcoder Jun 06 '16 at 09:11
  • Good answer, maybe not too technical, but the approach is clear and reasonable. I up-vote for this! – X-HuMan Jun 06 '16 at 11:28