New to C# socket programming - c#

I am new to socket programming with C#. I am developing a client-server application with multiple clients. I am not quite sure how to form a communication between the server and its clients.
Is it a good idea for the clients to send keyword messages (i.e. 'string') that will invoke the server to do something?
Is it a good idea for the clients to send instances of certain classes (that I created) which need to be processed?
Is it a good idea to send/receive files (I am using XML) between the server and the clients?
I am sorry if my questions are naive as this is my first experience developing an application with a client-server architecture.
I found this link which has an example of such an application but I was wondering if anyone knew any other websites/books/tutorials with explanations.

If you create a modern c# application you seldom need to resort to plain sockets to create a client-server application. Instead you can create a WCF service. (Pick a tutorial and try).
Using a WCF service instead of plain sockets will save you from a lot of plumbing work with creating a protocol, parsing and that kind of stuff.

Related

How to create WPF internet chat application

I want to create a simple chat application using C# and WPF that uses internet to connect between clients and server. I found a lot of tutorials for chat apps, but they all work over local network, I am confused and don't know to what technology to use to create my app. Could anybody point me in the right direction?
You really have a lot of directions you can go for this. The simplest is a simple plain sockets client-server star topography. This will work over both a public and private network, but you will encounter data theft as the data will be unencrypted.
For any internet application, you really should use the SslStream to secure your communications between the server and client. This allows you to specify TLS1.2 as your transport which will provide great security. You can consume that server stream from WPF via standard Sockets or from Windows Store Apps using StreamSockets. Mind you, you will need to learn a LOT about how SSL keys work and configuring the connections in code.

C# Multi-Client socket Chat Application

I'm a newbie C# socket programmer and I'm trying to create a C# socket chat application...
here are the requirements of the app..
*it can accept more than one client.
*it should handle those new client on a separate thread.
*the server has a list of active clients and displays them in a list(GUI)
*the server should know when a client is disconnected and will remove it from the list(probably a hashtable or array) of active clients.
honestly, I don't know where to start.
I've been reading through various articles on the net about sockets, multithreading and the likes, but I'm still a bit confused about the whole client/server interaction thing..
Any help/suggestions will be greatly appreciated.. :D
Take a look at ZeroMQ. It was designed for applications like you're describing, works very well, and is free.

Server Console/Service APP <-> WPF Client communication framework sought

I am looking for an open-source framework that can handle communication between my backend host and a WPF frontend client. The following are points I need to consider:
Client is a WPF desktop app, host is a C# console APP but can also run as Windows Service
Host can accept connection requests, client connects, but I still seek bi-direction communication capabilities.
The client and host are not in the same network (need to send messages over the internet)
Just one (possibly more but less than 10) client will connect to the host.
I like to be able to handle the following communication patterns: One-way message, two-way request/response, and streaming from host to client.
The content will consist of serialized POCOs/DTOs and serialized time series data. Each serialized DTO will be of approximately size 400 bytes, the serialized time series can be a lot larger, potentially several megabytes.
Some messages will be sent scheduled, meaning for example, the host sends a new DTO every second. (if the framework includes such scheduling mechanism then even better)
I like to be able to stream data to the client, such as a client that receives and then updates data on its GUI in real-time.
My question is: What might be the best C# based framework to handle this? (I run on .Net 4.5) I do not consider WCF because it seems way too thick and complex for what I try to handle. I also do not want to delve into web programming, so Rest/Soap type frameworks are not under consideration. I would be delighted if anyone could recommend a tcp/websocket based framework or similar that is light weight enough to potentially send messages a lot more frequently than just every second.
Thanks a lot in advance.
Any reason why you are not considering HTTP? It is just over TCP but you don't need to worry about firewalls and stuff, especially given the fact that you want to do this over internet. Self-hosted ASP.NET Web API can be a good candidate. Fire and forget and request/response can be accomplished fairly straight forward and for streaming it has PushStreamContent. ASP.NET Web API is light weight compared to WCF and is open source. Just a suggestion, that's all.
I ended up using ZeroMQ for full-duplex communication between WPF client and Server. It is lightweight, very fast and pretty reliable for my needs.

Java android client communication with C# server

I am currently writing an application having a client server architecture.
The client is a Java android application
The server is a C# application.
The client will pull data from the server but in some cases push some data to the C# server as well.
The data that server needs to forward the clients is list of data structures (perhaps in the form of XML?), sometime binary data like files.
The client and server are communicating over a wireless network.
Speed and scalability is my top most priority in the design of the system,...
I have to write server as well as the client myself. I will be using sockets for communication.
I need your advise on the form of protocol I should use to exchange data between the Java client and C# server.
Should I write similar data structures (which seems redundant) in java and C# and serialize them ??
or should I exchange xml ??
I am not sure yet what is the best way to do it ..
Essentially there will be commands from client and server will respond with data
Please advise me on this topic the data communicated could be be as large as several gigs over wifi so speed is very important.
Well, there's always JSON. It should be well-supported on both ends and is easy for your server to generate and client to consume. Not sure it helps with your bandwidth concerns any...
I believe WCF might be approperiate for this, WCF uses soap so a Java implementation should work well. WCF also supports steaming, so transferring large files is possible, though I'm not sure if Java supports the streaming protocol.
As for performance, you will probably be limited by the speed of the device and not the protocol.
Have a look at this session from TechEd 2011: "My Customers Are Using iPhone/Android,But I'm a Microsoft Guy. Now What?"
http://channel9.msdn.com/Events/TechEd/NorthAmerica/2011/DPR304
It would probably be worth looking into MonoDroid if you want to share code between client and server (and if serialize/de-serialize makes sense).
As I don't know what you're building, I would advise you to read up on REST before you continue though. It should give you valuable pointers on how to create a nice API that can be easily consumed by various clients.

is there a way to let the web server (not the Client which is the trivial case) send a messege to a Client application?

I'm about to start to develop and application in C# but I realized that I haven't the enough knowledge to develop it yet :S.
The thing's that I need to find out a way to let the Web server comunicate with my application, i.e., in short, is there a way to let the web server (not the Client which is the trivial case) send a messege to a Client application?
I know that I way to solve it's to make Client applications periodically send messages to the web server but that's not what I want 'cause polling generates overhead
Sorry about my english! I'm not a native speaker.
Thanks in advance!
Generally this type of interaction is achieved with Comet or WebSockets - I'm not sure how your app will be communicating with the server, but I would bet you can do what you're trying to do using one of those.
You could implement a WCF service in your client that could listen for a connection from the server (or anything else). The server can communicate with the client as easy as calling the API.
Getting started with WCF is really easy using the wizards in VS.
Here is a link that talks about using WCF with ASP, but it can be used outside of asp as well.
It seems like you meant "push" messaging, the challenge around this is for the server to keep track of the lost of clients and manage who should recieve which message.
If you want to get it done with minimal overhead you can check out the Amazon Simple Notification Service.
SNS is a cloud-based messaging and notification service hosted and managed for you, SNS is based on a topic/subscriber model and you set it up via a few simple API calls, it is metered but quite inexpensive for the most part.
edit: For C# Libraries and frameworks to do it yourself, I am not an expert in the C# world so I think other answerers will know it better.
Disclosure: I work at amazon so I am naturally inclined to like their product

Categories

Resources