Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C# socket TcpClient
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 ...
Posted 29-Jan-13 8:06am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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,
Abhinav S at 30-Jan-13 12:09pm
5! I guess you covered quite a lot in your answer.
Sergey Alexandrovich Kryukov at 30-Jan-13 12:25pm
Thank you, Abhinav.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This tutorial[^] helped me.
source.compiler at 30-Jan-13 7:03am
its not a relevant article to my question ! but it was good article !
Marco Alessandro Bertschi at 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)

  Print Answers RSS
0 OriginalGriff 279
1 Nirav Prabtani 253
2 Sergey Alexandrovich Kryukov 209
3 _Amy 165
4 Richard MacCutchan 135
0 OriginalGriff 7,799
1 Sergey Alexandrovich Kryukov 6,966
2 Maciej Los 3,994
3 Peter Leow 3,708
4 CHill60 2,742

Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100