First of all, when you talking about "server", it should be something which you develop yourself, because you will need to have some custom application-layer protocol:
http://en.wikipedia.org/wiki/Application_layer[
^].
More exactly, a
service. The term "server" is more often used in a narrow sense of
client-server model, which is very limiting, in particular, for your problem. Please see:
http://en.wikipedia.org/wiki/Client-server[
^].
It is limiting, because a narrow sense of this term, client-server model implies the application of use
pull technology, in contract to
push technology. Please read this:
http://en.wikipedia.org/wiki/Pull_technology[
^],
http://en.wikipedia.org/wiki/Push_technology[
^].
In a chat, the pull approach would require all clients to
poll the server on a regular basis, even if no messages are available. This way, if you poll frequently, you just waste CPU time, and, more importantly, waste traffic; if you do it rarely you still waste traffic, but you also becomes unresponsive. So, the right thing to do is to have clients listeners as well, listening to server, and the server should push all relevant messages to all clients as they appear. More exactly, each client should
subscribe to server events. Technically, on a TCP level (for example), it could be implemented by
System.Net.TcpListener
. In connection to that, one important aspect is multithreading.
You can find further ideas in my past answer:
Multple clients from same port Number[
^].
In fact, you can do networking on several different level, from raw sockets to "classical" remoting to (self-hosted) WCF. Pleas see my short overview of them in my past answers:
how i can send byte[] to other pc[
^],
Communication b/w two Windows applications on LAN.[
^].
In all cases, you still need to develop your application-level protocol. In fact, such thing always exists, even if it is trivial or if the developers do not call it this way.
Now, the most adequate form of "real" service (network or not) is the Windows Service. Please see:
http://en.wikipedia.org/wiki/Windows_service[
^],
http://msdn.microsoft.com/en-us/library/y817hyb6.aspx[
^],
http://msdn.microsoft.com/en-us/library/zt39148a.aspx[
^].
Finally, as to the question about the form of data sent via the sockets or network channels: in .NET, data is always transmitted as array of bytes. All other data is
serialized to array of byte and
deserialized back. So, you need to learn
serialization:
http://en.wikipedia.org/wiki/Serialization#.NET_Framework[
^],
http://msdn.microsoft.com/en-us/library/vstudio/ms233843.aspx[
^].
Serialization can be highly automated and hidden from the developer in higher-level framework APIs. One advanced way of serialization is
Data Contracts. At the same time, this approach is the most robust, universal and easiest to use; most importantly, it is
non-intrusive . It was introduced in FCL with WCF, but can be used anywhere, without WCF. In particular, this is the best way to store and load any kind of data in a file or a stream. You can store any arbitrary object graph in some stream and restored in the same exact form, in other application, remote or not, or the same. Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[
^].
Please also see my past answers where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[
^],
Creating property files...[
^],
deseralize a json string array[
^].
Good luck,
—SA