Easy C # Programming for Beginners?

I'm currently new to C #, and I need to understand the simple server-client architecture!

I'm currently trying to write a simple server / client program where basically the client can send a variable to the server and the server can send it to another client. The problem is that I'm really blind to this, as I'm still very new to C #, although I have some experience with Java (but still not with the network).

My question is:

  • How many files do I need to write?
  • Can anyone be good enough to provide me with a framework or an example for such a program?
  • What is a TCP server?

It is intended for an online game. One client will roll the dice, and the server must show all other clients that this is the value that the first client executed.

Any help would be greatly appreciated!

+8
c #
source share
3 answers

Answer all your questions: MSDN - Network Programming (.NET 4)

+8
source share

Since you plan to use TCP (because you want state), you need to develop a strategy. You will get a lot of information about establishing a connection and moving some data back and forth. Google will give you more than you can handle. Without doing all the work, here are a few steps to get your bearings.

1) Registration of connections. When a client comes to the network and wants to communicate with the server, you must first say "Hey, I'm here and I want to play dice." This initial handshake may be a connection identifier that is used for heart rate and / or transactions. The server will use this to identify the data and the corresponding stream, if they are open.

2) Heart beat. Now that the client has registered on the server, the client is responsible for giving a heart beat, saying that he is still there and still plans to continue working. Usually every 3 to 10 seconds is good.

3) Develop a Request / Response protocol - there will be a formal process for β€œeach team”. This formal process will include a connection identifier as well as a request identifier. The client will not accept the response if he does not receive the corresponding request identifier. In addition, each request will require a successful or unsuccessful response to determine if it matches the API or not. Inside the request, a command or action will be executed. Some people use int to send the command identifier, then use the id switch to call the entry point method (cmd id = 1 is connect (), cmd id = 2 - rolldice (), etc.). You can include an additional payload that identifies the result from the command.

In short, 1 is a handshake, 2 is a save, and 3 is a data transfer back and forth.

Now, whether you need to use a socket or WCF, I would recommend having a basic understanding of TcpClient programming, and then starting with WCF. You will be amazed at how easy the socket programming is, but the overhead is a killer. There is nothing to intimidate. This is a lot of work in coordinating calls, flows, and not to mention security. WCF, on the other hand, shaves some of this overhead.

I would check this question ...

How to use socket client with WCF service (net.tcp)?

+4
source share

1) The number of files will depend on the particular implementation. You can create this architecture simply as 1 class for the server and 1 class for the client (you can have more than one class in the file). Depending on the complexity and choices you make during development, you may have many files, or just a few.

2) A good tutorial for a simple TCP server / client can be found here

3) A TCP server is a process that waits for a connection from a TCP client. TCP stands for Transmission Control Protocol. From Wikipedia : TCP provides reliable, orderly delivery of a stream of bytes from a program on one computer to another program on another computer.

+3
source share

All Articles