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.
客户端使用UDP或TCP向服务器发送一些消息。服务器必须能够支持UDP和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()
.
如果客户端使用UDP发送消息,则客户端中的方法调用序列是socket(),bind(),sendto(),recvfrom(),close(),而服务器中的是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()
如果它使用TCP,则服务器中的调用顺序是socket(),bind(),listen(),accept(),send(),recv(),close()。在客户端是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().
在我的程序中,用户/客户端在开始时可以选择他想要使用的UDP或TCP。所以,我的主要问题是如果客户端使用TCP或UDP发送消息,我如何知道或区分服务器端。如果它使用TCP,我必须调用listen(),accept(),send(),recv(),如果它使用UDP,我不调用listen(),accept()但调用sendto()和recvfrom() 。
So, how can I differentiate/know this in the beginning so that I can make appropriate function calls.
那么,我如何在开始时区分/了解它,以便我可以进行适当的函数调用。
Thanks.
谢谢。
4 个解决方案
#1
17
Before the packet reaches you, you don't know whether it's UDP
or TCP
.
在数据包到达之前,您不知道它是UDP还是TCP。
So you want to bind to both UDP
and TCP
sockets if you expect requests both ways.
因此,如果您希望双向请求,则希望绑定到UDP和TCP套接字。
Once you did, you just know which way it came by the socket you received the packet through.
一旦你这样做了,你就知道你收到数据包的套接字通过了哪种方式。
#2
6
When you create the socket, you pass a type - SOCK_STREAM
(TCP) or SOCK_DGRAM
(UDP)
创建套接字时,传递类型 - SOCK_STREAM(TCP)或SOCK_DGRAM(UDP)
So the two kinds of traffic will be on two different sockets.
所以这两种流量将在两个不同的套接字上。
#3
1
just let the TCP socket listen on port X, and do the UDP connections through port Y
让TCP套接字侦听端口X,并通过端口Y进行UDP连接
#4
1
Just as Henry Troup pointed out, an IP socket is defined as
就像Henry Troup所指出的那样,IP套接字被定义为
(transport, interface, port).
(运输,接口,端口)。
(UDP, 127.0.0.1, 80) is not the same IP socket as (TCP, 127.0.0.1, 80) , thus you can safely bind to both of them and listen for incoming traffic.
(UDP,127.0.0.1,80)与(TCP,127.0.0.1,80)不是同一个IP套接字,因此您可以安全地绑定到它们并侦听传入流量。
#1
17
Before the packet reaches you, you don't know whether it's UDP
or TCP
.
在数据包到达之前,您不知道它是UDP还是TCP。
So you want to bind to both UDP
and TCP
sockets if you expect requests both ways.
因此,如果您希望双向请求,则希望绑定到UDP和TCP套接字。
Once you did, you just know which way it came by the socket you received the packet through.
一旦你这样做了,你就知道你收到数据包的套接字通过了哪种方式。
#2
6
When you create the socket, you pass a type - SOCK_STREAM
(TCP) or SOCK_DGRAM
(UDP)
创建套接字时,传递类型 - SOCK_STREAM(TCP)或SOCK_DGRAM(UDP)
So the two kinds of traffic will be on two different sockets.
所以这两种流量将在两个不同的套接字上。
#3
1
just let the TCP socket listen on port X, and do the UDP connections through port Y
让TCP套接字侦听端口X,并通过端口Y进行UDP连接
#4
1
Just as Henry Troup pointed out, an IP socket is defined as
就像Henry Troup所指出的那样,IP套接字被定义为
(transport, interface, port).
(运输,接口,端口)。
(UDP, 127.0.0.1, 80) is not the same IP socket as (TCP, 127.0.0.1, 80) , thus you can safely bind to both of them and listen for incoming traffic.
(UDP,127.0.0.1,80)与(TCP,127.0.0.1,80)不是同一个IP套接字,因此您可以安全地绑定到它们并侦听传入流量。