views:

140

answers:

2

Hello guys, I need to develop a client server system where I can have multiple clients communicating with one server at the same time. I want to communicate xml serialized objects and also need to send and receive other commands to invoke methods. Now, I am just starting with socket programming in C# and .Net and found that the asynchronous I/O is the way to go so that the methods dont block the execution of code. Also there are many examples of how to

make a simple client server system. So I have a basic understanding of how that works.

Anyway, what still is not clear to me is how I can set up a server which can manage connections to multiple clients? Can I just create a new socket per connection and then store those in some kind of list? Do I need some kind of multiplexing to achieve this? Do I have to listen at multiple ports? What`s the best way here?

And the other thing is if I need to develop my own protocol to differentiate between what I am actually sending over the network --> xml serialized object or a command which might be just a string encoded in ascII or something. Or would I develop my own protocol just to send these commands?

Any kind of help is apreciated! If someone knows a good book which covers this sort of stuff, let me know. Cheers

I forgot to mention that some of my clients which are supposed to communicate with my server will be pda and I therefore use the compact framework... So this might bring in some restrictions...

A: 

You may find several of my TCP/IP .NET FAQ entries helpful, particularly using a socket as a server socket, which explains how listening servers create new client connections, and XML over TCP/IP, which discusses the decisions you have to make for an XML-over-TCP/IP protocol.

Stephen Cleary
Just read the link u posted above. That really was useful. What I did not get is what do you mean by local port? Lets say I am listening at port 8221 for any IP Adress. How can I then create a new socket which would handle the incoming connection with the local port? Would the client then also have to change the port, or how does that work? Thanks alot!
jagse
The creation of the new socket bound to an unused local port, and the migration of the client to the new socket, is all handled for you by the TCP/IP stack. So, as far as server code is concerned, you can continue listening on 8221; and as far as client code is concerned, it has successfully connected to 8221 (though it actually communicates to the dynamic port selected by the server's TCP/IP stack).
Stephen Cleary
What I dont get is, how I my listening 'server socket' can pass on the connection to a newly created socket. I would suggest I have a socket and call the BeginAccept like follows:socket.BeginAccept(new AsyncCallback ( OnClientConnect ),null);but then in my OnClientConnect callback, how do I pass on this connection to a new socket?
jagse
Sorry! My bad. Didnt see the Accept() method returns a new socket! Thank you for your help.
jagse
+1  A: 

I would abandon your plan to use Sockets and switch to WCF Windows Communication Foundation. It's far more elegant and is designed to do all the things you wanted, in a considerably easier and simpler way than .NET sockets.

If you want a guide of how to use it, there are a set of amazing Microsoft webcasts by Michele Leroux Bustamante that will have you up and running in no time.

SLC
Allright. I will have a look at it. Problem might be that my clients actually are PDAs and therefore are restricted in a few ways as I use the Compact Framework for implementation. Of course the server would not be a PDA. So I might be able to make use of the WCF for implementing the server. Thanks for your hint!
jagse
Now I found out that the WCF also is available in the compact framework. Would it be possible with WCF to pull data from the client as well?
jagse
Absolutely. You can set up a two-way WCF using callbacks, so the server can call a method on the client (which can have a return parameter). I used WCF to set up a client/server game that sends lots of information back and forth. It's no problem at all!
SLC
Allright then. This sounds really good. I will check it out. Thanks alot! Maybe you know some kind of tutorial of how to implement a two way WCF using callbacks. That would be great. Anyway, Thanks.
jagse
You dont know if the two way communication is possible with a pda in general? Cheers.
jagse
Seems like it does not work with PDAs. According to this blog: http://blogs.msdn.com/andrewarnottms/archive/2007/09/13/calling-wcf-services-from-netcf-3-5-using-compact-wcf-and-netcfsvcutil-exe.aspx Callback contracts are not available in the Compact Framework...
jagse