sending/receiving objects through tcpclient - c#

I have to send/recieve objects (of a custom class made by me) in my C# .NET 4.0 application and I would like a good tutorial to get me started because I've searched on Google and there seem to be a lot of problems with serialization/deserialization and although the problems were solved, there are a lot of ugly hacks.
Regards,
Alexandru Badescu

I've made a transport that does just this:
http://fadd.codeplex.com/SourceControl/changeset/view/58859#1054822
The library is work in progress, but the BinaryTransport works. It will also attempt to reconnect if it get disconnected.
Example:
public class Example
{
private BinaryTransport<Packet> _client;
private ServerExample _server;
public void Run()
{
// start server
_server = new ServerExample();
_server.Start(new IPEndPoint(IPAddress.Loopback, 1234));
// start client
_client = new BinaryTransport<Packet>(new IPEndPoint(IPAddress.Loopback, 1234));
// send stuff from client to server
_client.Send("Hello world!");
// send custom object
_client.Send(new User { FirstName = "Jonas", LastName = "Gauffin" });
}
}
public class ServerExample
{
private readonly List<BinaryTransport<Packet>> _clients = new List<BinaryTransport<Packet>>();
private SimpleServer _server;
private void OnClientAccepted(Socket socket)
{
var client = new BinaryTransport<Packet>(socket);
client.Disconnected += OnDisconnected;
client.ObjectReceived += OnObject;
_clients.Add(client);
}
private void OnDisconnected(object sender, EventArgs e)
{
var transport = (BinaryTransport<Packet>) sender;
transport.Disconnected -= OnDisconnected;
transport.ObjectReceived -= OnObject;
}
private void OnObject(object sender, ObjectEventArgs<Packet> e)
{
Console.WriteLine("We received: " + e.Object.Value);
}
public void Start(IPEndPoint listenAddress)
{
_server = new SimpleServer(listenAddress, OnClientAccepted);
_server.Start(5);
}
}
[Serializable]
public class Packet
{
public object Value { get; set; }
}
[Serializable]
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Update
I've made a new framework: http://blog.gauffin.org/2012/05/griffin-networking-a-somewhat-performant-networking-library-for-net/

If you have control over the objects you could decorate them with the [Serializable] attribute and use BinaryFormatter for serialization/deserialization.

For TCP/IP communication I highly recommend Stephen Cleary's FAQ, you should pay special attention to Message Framing. You might also want to take a look at his NitoSockets implementation.
All that assuming you can't just use WCF, of course.

Related

Send data to client OnMessage with WebSocket-sharp server

I've been trying to solve this simple problem, but I cannot make it work.
I am using WebSocketSharp.Server. The code can be seen below.
In my NetworkClass I have someData I would like to send to the client when a message is received. The problem is the OnMessage event gets fired in a different class, and I don't know how the access the instance of this class.
Broadcasting to all clients form the NetworkClass works fine and receiving messages form the client works fine as well.
public class IndexRoute : WebSocketBehavior {
protected override void OnMessage(MessageEventArgs e) {
Console.WriteLine("Received message form client: "+e.Data);
//TODO: send someData string to client
}
}
public class NetworkClass {
String someData = "TestData";
WebSocketServer server = new WebSocketServer("ws://127.0.0.1:5000");
public NetworkClass() {
server.AddWebSocketService<IndexRoute>("/");
server.Start();
Console.WriteLine("Server started");
}
public void broadcastData() {
server.WebSocketServices["/"].Sessions.Broadcast(someData);
Console.WriteLine("Broadcast");
}
}
I combined the answers of Jeroen van Langen and Jesper. The key was to pass the instance of the NetworkClass to the IndexRoute class and access the variables from there.
server.AddWebSocketService<IndexRoute>("/", () => new IndexRoute(this));
works but is marked as obsolete.
public class IndexRoute : WebSocketBehavior {
private NetworkClass _instanceOfNetworkClass;
public IndexRoute(NetworkClass instanceOfNetworkClass)
{
_instanceOfNetworkClass = instanceOfNetworkClass;
}
protected override void OnMessage(MessageEventArgs e) {
Console.WriteLine("Received message form client: "+e.Data);
//Broadcast someData string to clients
Sessions.Broadcast(_instanceOfNetworkClass.someData);
}
}
public class NetworkClass {
public String someData = "TestData";
WebSocketServer server = new WebSocketServer("ws://127.0.0.1:5000");
public NetworkClass() {
server.AddWebSocketService<IndexRoute>("/", () => new IndexRoute(this));
server.Start();
Console.WriteLine("Server started");
}
public void broadcastData() {
server.WebSocketServices["/"].Sessions.Broadcast(someData);
Console.WriteLine("Broadcast");
}
}
I don't have any experience with WebSocketSharp, but the docs says you can give an alternate WebSocketBehavior construction function to the AddWebSocketService<>
Let me know if it works: (else i'll remove it)
public class IndexRoute : WebSocketBehavior {
private string _someData;
// define the constructor which accepts the someData
// save it in a field.
public IndexRoute(string someData)
{
_someData = someData;
}
protected override void OnMessage(MessageEventArgs e) {
Console.WriteLine("Received message form client: "+e.Data);
//TODO: send someData string to client
// do something with _someData
}
}
public class NetworkClass {
String someData = "TestData";
WebSocketServer server = new WebSocketServer("ws://127.0.0.1:5000");
public NetworkClass() {
// pass the construction function.
// (construct the IndexRoute and pass the someData to it.)
server.AddWebSocketService<IndexRoute>("/", () => new IndexRoute(someData));
server.Start();
Console.WriteLine("Server started");
}
public void broadcastData() {
server.WebSocketServices["/"].Sessions.Broadcast(someData);
Console.WriteLine("Broadcast");
}
}
I didn't tested it, but it might give you a step in the right direction to solve it.
When you inherit WebSocketBehavior, you get methods and properties that allow you to find out information about the connection you are currently talking to. In this case, just use one of the Send methods to send to the connection that sent the message:
protected override void OnMessage(MessageEventArgs e) {
Console.WriteLine("Received message form client: "+e.Data);
Send(/* pick one of the overloads to suit your needs */);
}
You can also use the property Context to get a WebSocketContext with information about the connection, or ID to find the session (connection) ID, which you could pass to another object. If the question is how you find your instance of NetworkClass, you are already constructing the IndexRoute yourself, just pass this in as an additional parameter.

Validation Exception Queue using MSMQ in C#

I'm new in Microsoft Message Queue in Windows Server, I need to push, if the EmployeeID is NULL.
The Employee Model Class is
public class Employee
{
public string EmployeeID { get; set; }
public string EmployeeName { get; set; }
}
public void ValidationProcess(Employee emp)
{
if((emp != null) || (emp.EmployeeID == null))
{
// Push into Validation Exception Queue using MSMQ
}
}
Once the Data pushed into that Validation Exception Queue, it should be processed by separate process. Every 1hr the process need to initiate and it should call the following method
public void ValidationExceptionProcess(object obj)
{
// Some Inner Process
// Log the Error
}
Kindly guide me how to create and process it.
First Step:
Install MSMQs as a windows feature on the server/pc
Then:
- Create the queue if it does not exist
- Push the message in the queue asynchronously
Useful guide
Code example for pushing and retrieving messages from msmq:
public class ExceptionMSMQ
{
private static readonly string description = "Example description";
private static readonly string path = #".\Private$\myqueue";
private static MessageQueue exceptionQueue;
public static MessageQueue ExceptionQueue
{
get
{
if (exceptionQueue == null)
{
try
{
if (MessageQueue.Exists(path))
{
exceptionQueue = new MessageQueue(path);
exceptionQueue.Label = description;
}
else
{
MessageQueue.Create(path);
exceptionQueue = new MessageQueue(path);
exceptionQueue.Label = description;
}
}
catch
{
throw;
}
finally
{
exceptionQueue.Dispose();
}
}
return exceptionQueue;
}
}
public static void PushMessage(string message)
{
ExceptionQueue.Send(message);
}
private static List<string> RetrieveMessages()
{
List<string> messages = new List<string>();
using (ExceptionQueue)
{
System.Messaging.Message[] queueMessages = ExceptionQueue.GetAllMessages();
foreach (System.Messaging.Message message in queueMessages)
{
message.Formatter = new XmlMessageFormatter(
new String[] { "System.String, mscorlib" });
string msg = message.Body.ToString();
messages.Add(msg);
}
}
return messages;
}
public static void Main(string[] args)
{
ExceptionMSMQ.PushMessage("my exception string");
}
}
An other widely used way to do that would also be to use out of the box loggers which already contains this functionality like Enterprise Library or NLog which provide easy interfaces to do that.
For retrieving messages I would recommend a separate windows service which would periodically read messages and process them. An good example on how to do that is given here: Windows service with timer
Update: Windows Service Example:
MSMQConsumerService.cs
public partial class MSMQConsumerService : ServiceBase
{
private System.Timers.Timer timer;
public MSMQConsumerService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
this.timer = new System.Timers.Timer(30000D); // 30000 milliseconds = 30 seconds
this.timer.AutoReset = true;
this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.ProcessQueueMessages);
this.timer.Start();
}
protected override void OnStop()
{
this.timer.Stop();
this.timer = null;
}
private void ProcessQueueMessages(object sender, System.Timers.ElapsedEventArgs e)
{
MessageProcessor.StartProcessing();
}
}
and the MessageProcessor.cs
public class MessageProcessor
{
public static void StartProcessing()
{
List<string> messages = ExceptionMSMQ.RetrieveMessages();
foreach(string message in messages)
{
//write message in database
}
}
}

Windows Phone 8.1 TCP sockets

I am developing a Windows Phone 8.1 chat app and discovered multiple difficulties during programming a socket interoperability.
The main issue are 'events' on a socket, and main question is: How to subscribe to OnData event, e.g. how to detect the event, when data came from the server?
I tried to solve it by direct way: infinite reading from InputStream, and that works with 50/50 luck: sometimes that method freeze the UI stream (even the reading method is async).
I googled for that issue, because it's a very strange behavior for that important component, and didn't found any solutions for this even on MSDN. At SO I found only same solutions like mine, but they also freeze UI flow.
For reference, my app infrastructure is the main app that uses facade class from shared library, the facade class get a generic, that specify concrete behavior (this was done for reason of multiple data transports). For TCP transport, I wrote that:
public class TCPTransport : ITransportProvider
{
private bool _connected { get; set; }
private StreamSocket _socket { get; set; }
private DataReader _input { get; set; }
public TCPTransport()
{
_socket = new StreamSocket();
}
public async Task<bool> Connect(string host, int port)
{
try
{
await _socket.ConnectAsync(new HostName(host), port.ToString());
if (OnConnect != null)
OnConnect();
_connected = true;
_input = new DataReader(_socket.InputStream)
{
InputStreamOptions = InputStreamOptions.Partial
};
_read();
return _connected;
}
catch(Exception e)
{
Debug.WriteLine("[warn] cant conect to " + host + ":" + port + ". Additional: " + e.Message);
return false;
}
}
async private void _read()
{
while (true)
{
if (!_connected || _socket == null) break;
uint buffer = await _input.LoadAsync(2048);
string data = _input.ReadString(buffer);
if (OnData != null && data.Length != 0)
OnData(data);
}
}
public async Task<bool> Send(string data)
{
if (!_connected) return false;
try
{
await _socket.OutputStream.WriteAsync(Encoding.UTF8.GetBytes(data).AsBuffer());
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("[warn] Exception send: " + e.Message);
}
return true;
}
public bool Close()
{
if (!_connected) return false;
_socket.Dispose();
return true;
}
public Action OnConnect { get; set; }
public Action<string> OnData { get; set; }
public Action OnClose { get; set; }
}
I suppose that I am wrong about using that solution, but how can I listen to data from server?
For better or worse, the StreamSocket API is not designed as an event-driven API; it is designed as an asynchronous API. If you want to raise an event when data is available, use an event, not a delegate:
Here is a trivial example (hard-coded to www.microsoft.com and with no error handling) showing how to simulate the event:
Note that if you run with the debugger attached, it will temporarily freeze the UI thread. This doesn't occur when not debugging
// Create your own class to hold the data
public class DataReceivedEventArgs : EventArgs
{
public uint DataCount { get; private set; }
public DataReceivedEventArgs(uint dataCount)
{
DataCount = dataCount;
}
}
// Now inside your MainPage...
bool m_keepReading = false;
bool m_eventHandled = false;
public event TypedEventHandler<MainPage, DataReceivedEventArgs> DataReceived;
// Hook this up to (eg) a Button
private async void StartSockets(object sender, RoutedEventArgs e)
{
if (!m_eventHandled)
DataReceived += (s, args) => Debug.WriteLine(args.DataCount);
m_eventHandled = true;
m_keepReading = true;
DoSocketTestAsync();
}
// Hook this up to (eg) a Button
private void StopSockets(object sender, RoutedEventArgs e)
{
m_keepReading = false;
}
private async Task DoSocketTestAsync()
{
var socket = new StreamSocket();
await socket.ConnectAsync(new HostName("www.microsoft.com"), "http");
var writer = new DataWriter(socket.OutputStream);
writer.WriteString("GET /en-us HTTP/1.1\r\nHOST: www.microsoft.com\r\n\r\n");
await writer.StoreAsync();
ReadSocketAsync(socket);
}
private async void ReadSocketAsync(StreamSocket socket)
{
while (m_keepReading)
{
const uint size = 2048;
IBuffer buffer = new Windows.Storage.Streams.Buffer(size);
buffer = await socket.InputStream.ReadAsync(buffer, size,
InputStreamOptions.Partial);
var handler = DataReceived;
if (handler != null && buffer.Length > 0)
handler(this, new DataReceivedEventArgs(buffer.Length));
}
}
Now when you click the "start" button, assuming you have a network connection you will see some numbers start appearing in the Debug Output window.
(Also note that using private auto-implemented properties doesn't really buy you anything; just make them fields).

Instantiating a delegate method to be used in a class library

I'm building an email-monitoring framework that I'll be using for a handful of users, so I'm building a class library to wrap everything in. I'm instantiating the configuration (sender, subject, last-received, ...) in a static class. Therefore, I have something like this.
public static class MyConfig
{
public static int Sender { get; set; }
// and so on and so forth
public static void BuildMyConfig(string theSender, string theRecipient, ...)
{
Sender = theSender;
// yada yada yada...
}
}
public class Monitoring
{
public delegate void DoSomethingWithEmail(EmailContents theContents);
public void StartMonitoring() {
//When I get an email, I call the method
DoSomethingWithEmail(theEmailWeJustGot);
}
}
Obviously, what we do with the email will be something completely different in each case. What I'm trying to is instantiate that delegate. Where would I do that? The MyConfig class and then invoke it from there as a static method? The instance of the Monitoring class?
An application would look like...
public class SpecificMonitor
{
Monitoring.BuildMyConfig("foo#bar.com", "bar#foo.com", ...);
Monitoring m = new Monitoring();
m.StartMonitoring();
//But where do I build the delegate method???
}
I've gotten compiling errors with every option I've tried so far. I've also tried overriding a method instead of using a delegate, using interfaces... but I think delegation is where it's at.
Thanks in advance!
Consistent with the rest of your design (although I do not necessarily agree that the design is great) you could allow for the callback to be set in the configuration class
public static class MyConfig
{
public static string Sender { get; set; }
public static DoSomethingWithEmail EmailReceivedCallback { get; set; }
public static void BuildMyConfig(string theSender, string theRecipient,
DoSomethingWithEmail callback)
{
Sender = theSender;
EmailReceivedCallback = callback;
}
}
// Make sure you bring the delegate outside of the Monitoring class!
public delegate void DoSomethingWithEmail(string theContents);
When an incoming email is acknowledged by your application you can now pass the email to the callback assigned to the configuration class
public class Monitoring
{
public void StartMonitoring()
{
const string receivedEmail = "New Answer on your SO Question!";
//Invoke the callback assigned to the config class
MyConfig.EmailReceivedCallback(receivedEmail);
}
}
Here is an example of usage
static void Main()
{
MyConfig.BuildMyConfig("...", "...", HandleEmail);
var monitoring = new Monitoring();
monitoring.StartMonitoring();
}
static void HandleEmail(string thecontents)
{
// Sample implementation
Console.WriteLine("Received Email: {0}",thecontents);
}
Define the constructor so that when people instantiate a Monitoring object, they must define the delegate:
public class Monitoring
{
public delegate void DoSomethingWithEmail(EmailContents theContents);
public Monitoring(Delegate DoSomethingWithEmail)
{
this.DoSomethingWithEmail = DoSomethingWithEmail;
}
public void StartMonitoring() {
//When I get an email, I call the method
DoSomethingWithEmail(theEmailWeJustGot);
}
}
Then pass in the delegate you want when you instantiate each Monitoring:
Monitoring m = new Monitoring(delegate(EmailContents theContents)
{
/* Do stuff with theContents here */
});
m.StartMonitoring();

IObserver and IObservable in C# for Observer vs Delegates, Events

All I am trying to do is implementing the observer pattern.
So, I came up with this solution:
We have a PoliceHeadQuarters whose primary job is to send notifications to all those who are subscribed to it. Consider that the DSP, Inspector and SubInspector classes are subscribed to PoliceHeadQuarters.
Using Events and Delegates I wrote
public class HeadQuarters
{
public delegate void NewDelegate(object sender, EventArgs e);
public event EventHandler NewEvent;
public void RaiseANotification()
{
var handler = this.NewEvent;
if (handler != null)
{
handler(this, new EventArgs());
}
}
}
public class SubInspector
{
public void Listen(object sender, EventArgs e)
{
MessageBox.Show(string.Format("Event Notification received by sender = {0} with eventArguments = {1}", sender, e.ToString()));
}
}
public class Inspector
{
public void Listen(object sender, EventArgs e)
{
MessageBox.Show(string.Format("Event Notification received by sender = {0} with eventArguments = {1}", sender, e.ToString()));
}
}
and this is how I invoked it
var headQuarters = new HeadQuarters();
var SubInspector = new SubInspector();
var Inspector = new Inspector();
headQuarters.NewEvent += Inspector.Listen;
headQuarters.NewEvent += SubInspector.Listen;
headQuarters.RaiseANotification();
so, both Inspector and SubInspector classes get notification whenever there the function RaiseANotification() is invoked.
It seems that the DotNet Framework 4, 4.5 supports a new way called IObserver and IObservable.
Can anyone give me a super simple example using IObservable and IObserver pattern for the above scenario? I googled only to find the available examples in the internet too bloated and difficult to understand.
My hinch: (probably i think it's wrong)
class DSP : IObserver //since it observes the headquarters ?
class PoliceHeadQuarters: IObservable // since here's where we send the notifications ?
Thanks in advance.
EDIT: Somebody also said that the MSDN documentation is also incorrect for IObservable #
IObservable vs Plain Events or Why Should I use IObservable?.
Here's a modification of MSDN example to fit your framework:
public struct Message
{
string text;
public Message(string newText)
{
this.text = newText;
}
public string Text
{
get
{
return this.text;
}
}
}
public class Headquarters : IObservable<Message>
{
public Headquarters()
{
observers = new List<IObserver<Message>>();
}
private List<IObserver<Message>> observers;
public IDisposable Subscribe(IObserver<Message> observer)
{
if (!observers.Contains(observer))
observers.Add(observer);
return new Unsubscriber(observers, observer);
}
private class Unsubscriber : IDisposable
{
private List<IObserver<Message>> _observers;
private IObserver<Message> _observer;
public Unsubscriber(List<IObserver<Message>> observers, IObserver<Message> observer)
{
this._observers = observers;
this._observer = observer;
}
public void Dispose()
{
if (_observer != null && _observers.Contains(_observer))
_observers.Remove(_observer);
}
}
public void SendMessage(Nullable<Message> loc)
{
foreach (var observer in observers)
{
if (!loc.HasValue)
observer.OnError(new MessageUnknownException());
else
observer.OnNext(loc.Value);
}
}
public void EndTransmission()
{
foreach (var observer in observers.ToArray())
if (observers.Contains(observer))
observer.OnCompleted();
observers.Clear();
}
}
public class MessageUnknownException : Exception
{
internal MessageUnknownException()
{
}
}
public class Inspector : IObserver<Message>
{
private IDisposable unsubscriber;
private string instName;
public Inspector(string name)
{
this.instName = name;
}
public string Name
{
get
{
return this.instName;
}
}
public virtual void Subscribe(IObservable<Message> provider)
{
if (provider != null)
unsubscriber = provider.Subscribe(this);
}
public virtual void OnCompleted()
{
Console.WriteLine("The headquarters has completed transmitting data to {0}.", this.Name);
this.Unsubscribe();
}
public virtual void OnError(Exception e)
{
Console.WriteLine("{0}: Cannot get message from headquarters.", this.Name);
}
public virtual void OnNext(Message value)
{
Console.WriteLine("{1}: Message I got from headquarters: {0}", value.Text, this.Name);
}
public virtual void Unsubscribe()
{
unsubscriber.Dispose();
}
}
public class Program
{
public static void Main(string[] args)
{
Inspector inspector1 = new Inspector("Greg Lestrade");
Inspector inspector2 = new Inspector("Sherlock Holmes");
Headquarters headquarters = new Headquarters();
inspector1.Subscribe(headquarters);
inspector2.Subscribe(headquarters);
headquarters.SendMessage(new Message("Catch Moriarty!"));
headquarters.EndTransmission();
Console.ReadKey();
}
}
Another suggestion - you probably want to consider leveraging the reactive extensions library for any code using IObservable. The nuget package is Rx-Main and the homepage for it is here: http://msdn.microsoft.com/en-us/data/gg577609.aspx
Update: ReactiveX has been translated to many platforms and languages and is now managed as an open source project. Here is the landing page.
This will save you a lot of boilerplate code. Here's a super simple example:
var hq = new Subject<string>();
var inspectorSubscription = hq.Subscribe(
m => Console.WriteLine("Inspector received: " + m));
var subInspectorSubscription = hq.Subscribe(
m => Console.WriteLine("Sub Inspector received: " + m));
hq.OnNext("Catch Moriarty!");
It will output:
Inspector received: Catch Moriarty!
Sub Inspector received: Catch Moriarty!
Reactive Extensions is a big subject, and a very powerful library - worth investigating. I recommend the hands-on lab from the link above.
You would probably want to embed those subscriptions within your Inspector, SubInspector immplementatinos to more closely reflect your code. But hopefully this gives you an insight into what you can do with Rx.

Categories

Resources