views:

1762

answers:

4

Hi there,

Can anyone help, i trying to figure what i need to do, i have been given the tasks of writing a server and a client in TCP (UDP). basically multiple clients will connect to the server.. and the server sends MESSSAGES to the client.

I have no problem in creating the server and client but with tcp i am unsure whcih way to go. DOes the .net 3.5 support everything or do i need to go on the hunt for some component?

I am looking for soome good examples with c# for TCP or UDP. THis is where i am not 100% sure .. as far as i know there is UDP and TCP ... 1 is connected and 1 is not.. So which way do i go and can c# support both?? Advantages /Disadvantages?

Say if the server has to support multiple clients that i only need to open 1 port or do i need to open 2?

Also if a client crashes i need for it not to effect the SERVER hence the server can either ignore it and close connection if one is open or timeout a connection... If in fact a connection is needed again going back to tcp udp

Any ideas where i shoudl beging and choosing which protocol and amount of ports i am going to need to assign?

thanks

A: 

Everything you need is in .Net 3.5 (and probably below). Check out the documentation and examples with the UdpClient class at MSDN for insight into how to write your client/server. A quick google found some sample code for a server and client at www.java2s.com among many other networking examples in C#. Don't be put off by the domain name.

tvanfosson
+1  A: 

Is there a requirement to do this at such a low level? Why not use WCF? It fully supports messaging over TCP/IP, using binary data transfer, but it's at a much higher level of abstraction than raw sockets.

John Saunders
yep! god your right!!! I have been using wcf for my web services ... One thing though does it allow the server to PUSH information to the client?? Basically that client needs to receive msgs but it should need to POLL?? .. if this is the case i think this solved my issues
mark smith
and i presume its going to work behind NAT??
mark smith
@mark: I don't know about pure push scenarios, though I know it supports duplex channels, which provide for a callback from the service to the clients. As to NAT, see http://msdn.microsoft.com/en-us/azure/dd441706.aspx and realize they did all that just by using extensibility points you can use as well, whether you use their code to do it or not.
John Saunders
Thanks! I have gone with this answer as it seems WCF suits my needs! But thank you everyone else for comments with regards to TCP and UDP
mark smith
I think that wcf callback channels are actually two separate unidirectional connections, so nats and firewalls are going to be a problem. Using tcp sockets will provide you with real full duplex messaging after initial setup. Check out System.Net.Socket and System.IO, all you need is there.
Pablote
The .NET Service Bus people have that licked. Look at http://msdn.microsoft.com/en-us/azure/dd441706.aspx. They can do things to get through NAT, and they demonstrated this September 2009 at PDC.
John Saunders
+2  A: 

It sounds to me like you're not clear on the distinction between TCP and UDP.

TCP is connection oriented. i.e. 2 peers will have a dedicated connection. Packet delivery and ordering is guaranteed. Typically a server will present a port, and multiple clients can connect to that port (think of a HTTP server and browsers).

UDP is connectionless. It doesn't guarantee packet delivery, nor ordering. You can implement broadcast and multicast mechanisms very easily. If you need some sort of reliability, you will have to implement this on top of UDP. Sometimes you may not care, and simply issue requests and retry on no response (SNMP does this). Because it's connectionless, you don't really worry about peers being up/down. You just have to retry if required.

So your choice of protocol is dictated by the above. e.g. does your client require a dedicated connection to the server ? Are you transmitting the same data to multiple clients ? Can you tolerate packet loss (e.g. real time price updates etc.). Perhaps it's feasible to use both TCP and UDP for different requirements within your app (e.g. TCP for registering orders, UDP for transmitting price updates/events?)

I'd consider your requirements, and familiarise yourself with the limitations and features of TCP and UDP. That should make things a little clearer.

Brian Agnew
A: 

UDP cons:

  • packet size restriction means you can only send small messages (less than about 1.5k bytes).
  • Lack of stream makes it hard to secure UDP: hard to do an authentication scheme that works on lossy exchange, and just as hard to protect the integrity and confidentiality of individual messages (no key state to rely on).
  • No delivery guarantee means your target must be prepared to deal with message loss. Now is easy to argue that if the target can handle a total loss of messages (which is possible) then why bother to send them in the first place?

UDP Pros:

  • No need to store a system endpoint on the server for each client (ie. no socket). This is one major reason why MMO games connected to hundred of thousands of clients use UDP.
  • Speed: The fact that each message is routed individually means that you cannot hit a stream congestion like TCP can.
  • Broadcast: UDP can broadcast to all listeners on a network segment.

You shouldn't even consider UDP if you're considering TCP too. If you're considering TCP means you are thinking in terms of a stream (exactly once in order messages) and using UDP will put the burden of fragmentation, retry and acknowledgment, duplicate detection and ordering in your app. You'll be in no time reinventing TCP in your application and it took all engineers in the word 20 years to get that right (or at least as right as it is in IPv4).

If you're unfamiliar with these topics I recommend you go with the flow and use WCF, at least it gives you the advantage of switching in and out with relative ease various transports and protocols. Will be much harder to change your code base from TCP to UDP and vice versa if you made the wrong choice using raw .Net socket components.

Remus Rusanu