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.
bind()
in your client code. While it's technically possible (and required in very specific situations), the default behavior without a client-sidebind()
is usually what you want at the application layer. Also, keep in mind that you can callconnect()
on a UDP socket. It doesn't do any handshaking, but it does mean you can usesend/recv
instead ofsendto/recvfrom
, which may end up making the client logic simpler. – Tom