views:

57

answers:

3

Hi, i want to create a client-server application using sockets on .net platform and being new to networking programming and i have a dilemma. The client will send data to server often and also the server will notify clients often. What is the best way to design it? should the server keep a thread to communicate with each client until it quits or just the clients send data to the server and it update the clients regularly(few seconds) or none of those? Some help would be great. Thanks guys. PS: The number of clients will reach thousands.

A: 

The way I did it back when I was writing a web server for a project was you have your main thread on the server that listens for new requests. Once a request comes in spawn a new thread and respond to the client from a different port. You keep that 2nd thread alive as long as your client/server are communicating. How long this is depends on many things. Will you have hundreds or thousands of clients? If so you should close the threads/ports often. But if you just have a couple clients keep them open longer.

I'm assuming the data sent to the clients is different for each client. If its the identical data then you should use UDP to broadcast that to all clients at once.

jamone
+2  A: 

The .net solution for the kind of design you're describing is called Windows Communication Foundation, or WCF for short.

WCF allows for asynchronous callbacks, which would be one way of implementing the type of architecture you describe. I did most of my learning about WCF with the book Learning WCF, by Michele Bustamante. WCF is very extensive and I would recommend against shooting from the hip without a good reference.

Rice Flour Cookies
Michele Bustamante is awesome. Here book is good and her web casts are super. A lot of great info on her web site http://www.thatindigogirl.com/
CkH
In what situation would you go for classic socket and not wcf ?
boo
@nelly: I would have a hard time coming up with a good reason to use classic socket programming these days, unless you're talking to a legacy service
Scott P
A: 

That would depend. Does your application depend on the input as it comes in, can it be reduced to chunks of code, can it handle it in any order, ..., lots of things yet unknown about your situation.

Async sockets have merit, but aren't for all types of applications.

Blocking sockets also have merit, are simpler to conceptualize and implement, and have many application uses.

You need to first identify your need, that will dictate how you implement things going forward.

jer