简体   繁体   English

我可以在TCP和UDP中使用相同的sockaddr_in吗?

[英]Can I use the same sockaddr_in in TCP and UDP?

In my serveur, I bind the TCP and UDP on two different ports. 在我的服务器中,我将TCP和UDP绑定在两个不同的端口上。 I first connect my Client with TCP (via accept, etc) 我首先用TCP连接我的客户端(通过接受等)

Then I want to use UDP to communicate between my server and my client. 然后我想使用UDP在我的服务器和我的客户端之间进行通信。 So I tried to use the same sockaddr_in like that : 所以我尝试使用相同的sockaddr_in

void AUDPMonitor::sendMessage(Message &msg)
{
  for (ISocket *socket: *_fdListClients)
  {
    if (msg.getClientId() == socket->getSock())
    {
      UDPSocket *UdpSocket = reinterpret_cast<UDPSocket *>(socket);
      UdpSocket->send(msg, socket->getUserAddr());
      break;
    }
  }
}

The _fdListClients is a vector of Socket I got from the TCP connexion. _fdListClients是我从TCP连接获得的Socket的向量。 There is no error messages but my client doesn't receive anything. 没有错误消息,但我的客户端没有收到任何信息。

So I want to know if it's possible to use the same sockaddr_in or if it's impossible. 所以我想知道是否可以使用相同的sockaddr_in或者它是不可能的。

Edit : When I accept the client socket 编辑:当我接受客户端套接字时

  socklen_t     client_sin_len;
  sockaddr_in   *client_sin = new sockaddr_in;

  client_sin_len = sizeof(sockaddr_in   );
  std::cout << "New User ! " << std::endl;
  if ((cs = accept(fd, reinterpret_cast<struct sockaddr *>(client_sin), &client_sin_len)) == -1)

You can use the same copy of the sockaddr_in for TCP and UDP, but they have to be on different sockets. 您可以使用sockaddr_in的相同副本用于TCP和UDP,但它们必须位于不同的套接字上。

A given socket created with AF_INET also specifies either SOCK_STREAM making TCP or SOCK_DGRAM making it UDP. 使用AF_INET创建的给定套接字还指定生成TCP的SOCK_STREAM或使其成为UDP的SOCK_DGRAM

So if you have this: 所以,如果你有这个:

struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(12345);

You can pass this to bind with a TCP socket to bind to TCP/12345, and you can pass it to bind with a UDP socket to bind to UDP/12345. 您可以将此bind与TCP套接字绑定以绑定到TCP / 12345,您可以将其传递bind UDP套接字绑定以绑定到UDP / 12345。

you can use the same SOCAKADDR_IN structure for a TCP or UDP server or client but whenever you want to use it for a different one you should change the values of port and address because servers cannot be bound to the same port. 您可以为TCP或UDP服务器或客户端使用相同的SOCAKADDR_IN结构,但是每当您想要将它用于另一个时,您应该更改端口和地址的值,因为服务器不能绑定到同一端口。

so if we have two remote stations running a TCP server and a UDP Server then the client to connect them: 因此,如果我们有两个运行TCP服务器和UDP服务器的远程站,则客户端连接它们:

SOCKET servTcp; // a remote bound and listing tcp socket waiting for remote clients through the blocking function `accept`
SOCKET servUdp; // a remote bound Udp server waiting for clients

// for the client: //为客户:

SOCKET clientTcp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKET clientUdp = socket(AF_INET, SOCK_DGRAM,  IPPROTO_UDP);

SOCKADDR_IN sa_serv;
sa_serv.sin_addr.S_un.S_addr = inet_addr("127.168.0.1");
sa_serv.sin_family = AF_INET;
sa_serv.sin_port   = htons(777);

now the socket clientTcp can connect to the remote tcp server using this address structure. 现在socket clientTcp可以使用这种地址结构连接到远程tcp服务器。

to use the same SOCKADDR_IN structure sa_serv for the the udp server: 为udp服务器使用相同的SOCKADDR_IN结构sa_serv:

as long as the tpc and the udp servers on the same remote machine we only change the port: 只要同一台远程机器上的tpc和udp服务器我们只更改端口:

sa_serv.sin_port   = htons(1000); // the port is unique for servers to bind on.

now we the udp socket clientUdp can use use sa_serv to send and receive data from the remote udp serv. 现在我们udp套接字clientUdp可以使用sa_serv来发送和接收来自远程udp serv的数据。

  • SOCKADDR_IN is the same for udp and tcp just change the value of port and sometimes address if server. 对于udp和tcp,SOCKADDR_IN是相同的,只需更改端口的值,有时会在服务器上进行寻址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM