views:

787

answers:

4

I am writing simple client-server program.

Client send some messages to server using UDP or TCP. Server must be able to support both UDP and TCP.

If client, sends message using UDP, sequence of method calls in client is socket(),bind(),sendto(),recvfrom(),close() and that in server is socket(),bind(),sendto(),recvfrom(),close().

If it uses TCP, sequence of call in server is socket(),bind(),listen(),accept(),send(),recv(),close(). and that in client is socket(),bind(),connect(),send(),recv(),close()

In my program, user/client is given choice in the start to select what he want to use UDP or TCP. So, my main problem is how can I know or differentiate in the server side, if the client is sending message using TCP or UDP. If it uses TCP, I must call listen(),accept(),send(),recv() and if it uses UDP, I don't call listen(),accept() but call sendto() and recvfrom().

So, how can I differentiate/know this in the beginning so that I can make appropriate function calls.

Thanks.

A: 

just let the TCP socket listen on port X, and do the UDP connections through port Y

Toad
you can safely listen on UDP and TCP on the same port.
Michael Krelin - hacker
A: 

Well you could set up listening sockets for all UDP and all TCP. Then when you receive a packet, you can open the header, check if it's UDP/TCP, and continue from there.

This project example here:

http://www.codeproject.com/KB/IP/CSNetworkSniffer.aspx?fid=373251&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=76#xx0xx

demonstrates (with a handy parser for TCP and UDP) how to set up the sockets/listeners and distiguish between the two.

Mark Mayo
Technically correct but practically wrong. When people ask questions about building socket-using applications, they are really asking about how to do it with the socket API, not with a lower-level slower more-complex network-sniffing API.
Liudvikas Bukys
That code example uses the classes and methods provided through the System.Net.Sockets API ...but it was more for the wording and description by the author who explains the process and what the various pats do. Then you can certainly write a higher-level app armed with that knowledge. Although I appreciate the code amount may occasionally frighten more than educate :)
Mark Mayo
Given the level of (in)expertise of the OP, I don't think it's very productive to recommend they subvert the TCP and UDP layers of the network stack. Better to work with the system than against it.
Tom
+11  A: 

Before the packet reaches you, you don't know whether it's UDP or TCP.

So you want to bind to both UDP and TCP sockets if you expect requests both ways.

Once you did, you just know which way it came by the socket you received the packet through.

Michael Krelin - hacker
A: 

When you create the socket, you pass a type - SOCK_STREAM (TCP) or SOCK_DGRAM (UDP)

So the two kinds of traffic will be on two different sockets.

Henry Troup
That doesn't help the server side. The OP already knows that the client side will decide TCP vs. UDP. The issue is supporting them both on the server side, and that's significantly more effort than simply changing the socket type.
Tom