I'm learning to work with sockets, and every example in every article or book demonstrates how a server should work using a desktop console or wpf application. The authors usually say that the same code can be easily implemented on the server.
Maybe it's easy, but still -- how? Do I have to use WCF or maybe something else? Is there any good book which can help me to become the lord of sockets?
The "same code on the server" comment means you're really developing a communications interface protocol. You send messages in a particular way, and expect to receive in the same way. Eg. fixed length messages vs agreed terminator; how message data is formatted etc.
If you put your code into it's own set of classes you can then host your code in any sort of application - console, wpf as you've stated but also.. windows services. In my case i've got it so that running the app in debug mode runs winforms, but as release mode runs as a service.
The answer to this SO question has some useful links.
Chat service application
I highly recommend Vadym's posts and Stephen Cleary's nito documentation.
Related
As per my knowledge, I knew that messages can posted from one application to other application using "SendMessage" or "PostMessage".
I tried using SignalR to communicate b/w the applications. The problem here i found was server should be launched as a console application and clients will be my winforms. Ideally I don't want to show this console application as it seems to be weird for the user. If there is any work around for this approach please suggest.
After signalR I came across EventAggregrator in c#. Can we use EventAggregrator to communicate b/w two different C# applications? If yes could some one give an example on how to do this.
Until a few years ago, Socket class was the right way to do this. There are probably more modern, robust and easier ways to achieve the same result nowadays, but you may want to look at what's under the hood.
Socket class on MSDN:
https://msdn.microsoft.com/it-it/library/system.net.sockets.socket%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
Client-server example on CodeProject:
https://www.codeproject.com/articles/463947/working-with-sockets-in-csharp
This is my first time dabbling in windows services.
I have a service I would like to manage, I would like to be able to connect to this service via a command line / REPL of sorts to avoid the development time of working on a user interface. I was thinking we could communicate much like attaching to an Asterisk daemon or somewhat like connecting to a MySQL server which to me seems like nothing more than a simple custom shell spawned to handle requests. However, I am always concerned about how efficient my code is and would like to keep to common practices. This will be connecting on the same local machine.
My proposed solution:
I believe I can make simple network stream, to create a simple Read - Eval - Print - Loop.
Another option is to use WCF, however my question would then be, how efficient is this as opposed to packet handling?
My question:
What are some standard practices for communicating with or managing services on the local machine?
I'm trying to learn more about service-oriented design, any resources that could help explain common practice models would be much appreciated.
Of course there are so many ways to do this. The way I would recommend is to make sure you use log4net (or some other logging framework) and log the important info. Create the solution with 3 projects, the first will be the "service logic" or the business service, with the second being the windows service wrapper that starts that service, and the third being a console app that does much the same as the windows service only giving you the ability to interact as you wish. The advantage of the console logging appender is that you still get the console output without actually writing to the console... it give good separation.
I will give another option that I have used in the past, but would give with caution. You can selfhost a WCF service inside a windows service. It gives a nice interface that gets away form the messy self rolled TCP server approach. The caution is that if done wrong it can eat up lots of memory and CPU cycles.
I'm writing a web server app for the first time, and I'm not really sure that I know what I'm doing.
Basically I have some server side C# code and a native iOS app. I need to be able to push updates from the server to the app. The method which we have decided to use is Long Polling, and I can see three ways of doing this:
1) Writing my own web server in C# - not neccesarily tempting as it requires re-inventing the wheel
2) Using WCF - I've seen a few articles about how to implement long polling over WCF, but the tutorials that I've seen all seem to use clients which are implemented in .NET WCF which is not applicable for me as I need to use an iOS app.
3) Something else, possibly using IIS - I don't really know where to begin with this option.
Can anyone recommend a good tutorial, or exemplar project which uses standard HTTP to implement long polling with a C# server? So long as it's using standard HTTP, I'm confident with the iOS side of things.
Obviously if there's an even neater way of doing things then I'm all ears as well.
I would highly recommend that you investigate SignalR which allows you to achieve exactly what you are after. There are many iOS tutorials as well as HTML / JavaScript and of course C#.
One of the benefits of SignalR is that it tries to use the best technology available on the various devices and down-grades until it works. So, will start with Web Sockets for example and fail down to long-polling if nothing better is available.
I've got I admit that I'm probably too dumb to fully learn and understand WCF. :(
On the other hand I had learned and used xmpp pretty well ( using MatriX XMPP library ).
So I wanted to ask, maybe there are some other libraries that help passing data from one computer to another to make life simple?
i.e. a library that would open a port/connection and both listen to incoming commands from other computers, as well as be able to send such commands to other computers that are listening to it.
Thanks!
WCF is not a hard technology... there is a learning curve, but ultimately someone who has taken the time to learn how to do it can easily implement the functionality in a couple of minutes.
i.e. a library that would open a
port/connection and both listen to
incoming commands from other
computers, as well as be able to send
such commands to other computers that
are listening to it.
Not really. Most of WCF involves setting up your app.config files so that they work on both ends. And I doubt there are any libraries that can do that for you.
You simply HAVE to generate and implement a service contract and implement the endpoints/meta data.
MSDN actually has a very decent tutorial. Getting Started Tutorial
Even if you DID find a "helper" you should still take the time to learn the technology so that you are prepared to troubleshoot and fix it when it breaks.
And if:
I've got I admit that I'm probably too
dumb to fully learn and understand
WCF. :(
were true... you wouldn't be a programmer.
My journey to understanding WCF happened right here on Stackoverflow... you can check it out at : Cross Application Communication (C#).
It might help you understand a little bit better.
Two options come to mind.
If you want to use WCF, the check out ECollective from SOA Collective. Uses managed mode Discovery to create a config-free WCF client, abstracting all of the things that makes WCF hard like bindings and behaviors.
If you want to use something other than WCF, check out NServiceBus.
Try the Idesign Website ... they have a WCF library that you can download and use in your projects.
First of all, sorry for my english. It's not my native language.
Here is the problem: I'm writing client-server application based on .net remoting. The application is some kind of calculator.
Client application has some field(number A and number B, and label for result) and some possible actions, represented by the buttons: Add, Substract, Multiply, Divide, etc..
Server application is a console application, that should habe following functions:
make this calculations
detect, what actions are done by the specific client.
Eg of output:
Server started
Client A(IP: 192.168.0.133) connected<br>
Client A Add 18 to 12<br>
Client A disconnected
The main problem is - how to get actions on server and how can i detect, what clients do.
Thx for help.
I know 2 common ways for tracing with .NET Remoting:
Turn on trace in app.config
Use Remoting Analyzator Studio, an open-source project from Codeproject.com.
This may not be ideal, but you can create a custom sink that hooks into the remoting channel which logs all remoting traffic. You can learn how to create a custom sink from Ingo Rammer's Advanced .NET Remoting book.
I've used his book to create custom sinks in the past.
We modified Mono TCP remoting channel to do this. It takes like 30 minutes and you can add information about the method call being invoked, time to execute and so on. It turns out to be a extremely good tool to measure performance on real servers. You can parse the output and learn which methods are taking more time, sending or receiving more data and so on. Really helpful.