C # is there an easy way to enter TCP Socket Programming?

I worked on a program that uses databases to send small messages from one PC to another. What I did was put the database in a shared folder, connect the program to it on another PC (via Path, no less), and there it is a simple and easy way to receive messages from a PC. online. Not the best option, but it's just homework, and a quick and dirty approach gave me an assessment.

But now the homework is done, and I would like to improve what I did. A problem with the program is being deployed. Too many folders / installation paths and administration / sharing issues related to direct access to the database in the shared folder.

So, the good people here at stackoverflow advised me to try Socket Programming, which I think is a bit out of my league. But you never know ...

In addition, I know the difference between programming Sync and Async sockets. One block, the other not. The program I'm working on is a simple turn-based game, so I thought Synchronous might be good enough, because if it’s not your turn, you really can’t do anything. However, the problem is that the program is considered as "non-responsive." I tried asynchronously, but ran into problems with threads, something that I find WAY out of my league.

Logically, the program is simple. One host, one client. When a client connects, the host sends data. Then the client receives, sends its own data. And so on, until one player loses.

Sorry, only .NET 2.0 is installed in my school. No WCF or anything else. In addition, this should be done in C # Windows Forms, so XNA is missing.

So, I would like to ask ... is there an easy way to get into Socket Programming? Any guides / sample projects that might help? Pre-prepared codes that can be learned and adapted?

Most of the samples that I found and adapted are chat applications, which I thought were good enough, but making it modular just breaks it down.

+7
source share
4 answers

The chat application examples you purchased should be sufficient. I don’t understand how you call it “modular”.

You need to develop a protocol that will be sent over the connection, so to speak, an agreement on the rules, so that you know what the other is talking about. Therefore, instead of sending plain text (chat), you can send the following:

0x03 (message length)
0x0A (move command in this fictitious protocol)
0x02 (command parameter 1, X coordinate in this case, all this is defined in the draft protocol)
0x05 (parameter 2 of the command, Y coordinate in this case, all this is defined in the draft protocol)

Now it is entirely up to you what happens after you receive and interpret the data. Personally, I would go for the Async solution, since it leaves your program to do other things (graphics?). And it's easier to adapt in code, in my experience.

+1
source

I have created several classes that can be used to transport objects over a socket using BinaryFormatter .

Here are some tests for my BinaryTransport class:

http://fadd.codeplex.com/SourceControl/changeset/view/67972#1055425

Actual class:

http://fadd.codeplex.com/SourceControl/changeset/view/67972#1054822

Please note that I wrote them recently. I just noticed some small errors. But either use them or simply study classes to learn more.

0
source

I remember when I started with sockets in C #, I tried to implement a simple chat program between a client and a server, and then between several clients. Here is a tutorial that I read then: http://www.codeproject.com/KB/IP/TCPIPChat.aspx

If you want the full code, I can download my latest project, and you can study the code. It also uses multi-threading, so you can see how to deal with this situation in GUI applications.

Side Note: Wow, this database idea is the craziest thing I've seen in terms of PC-PC communication. Well done!

0
source

One interesting, useful, and easy exercise that you can do to learn about sockets (which C # simplifies) was to create a TCP-based registrar.

During development, every programmer needs a way to find out what happens under the hood at certain points. Without a registrar, you usually write something like:

 Console.WriteLine( "blah" ); 

which results in a boring, unfiltered, unorganized string thrown into the exit window.

I very easily created a TCP based logger using sockets. In one hand, you have a separate Winforms application (server), which is responsible for listening to incoming messages and beautifully displaying them on an extended content control. On the other hand, you write a very simple class (client) with a single function, for example:

 public static class MyConsole { public static void WriteLine( string message, string whatever ) { // send to the net if( mTcpSocket.Connected ) mTcpSocket.Send( message ); // in case the server is not there we still have regular output Console.WriteLine( message ); } } 

I created this registrar once and have used it ever since. Also, given its nature of tcp, with minor changes on the server side, I have successfully used it from different languages ​​like C # and Java, and now I used it from ActionScript.

0
source

All Articles