Click here to Skip to main content
15,612,584 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i want to make a small app for connecting to a chat application and get some data!
i want know is a generl way to connect with any server and get data or any server must provide some ways to let us connect to it! and if a server didnt do that we cant connect to it?
i know how i can write a server and client and make relationship between them! but i want know how can i write a client to another server that i dnt have any control on it( like yahoo) !
another question is this that how can i send and get data except bytes! i should cast any type to bytes then send? if yes so have can i cast a custom class that i write to bytes?

tnx all ...

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:[^].

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:[^].

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:[^],[^].

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:[^],[^],[^].

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:[^],[^].

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:[^].

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,
Share this answer
Abhinav S 30-Jan-13 12:09pm    
5! I guess you covered quite a lot in your answer.
Sergey Alexandrovich Kryukov 30-Jan-13 12:25pm    
Thank you, Abhinav.
This tutorial[^] helped me.
Share this answer
ali_heidari_ 30-Jan-13 7:03am    
its not a relevant article to my question ! but it was good article !
Marco Bertschi 1-Feb-13 4:28am    
Glad you liked the article. However, I just made a suggestion ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900