CE_
CE_

Reputation: 1118

Can I use the same sockaddr_in in TCP and UDP?

In my serveur, I bind the TCP and UDP on two different ports. I first connect my Client with TCP (via accept, etc)

Then I want to use UDP to communicate between my server and my client. So I tried to use the same sockaddr_in like that :

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. 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.

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)

Upvotes: 4

Views: 3411

Answers (2)

Raindrop7
Raindrop7

Reputation: 3911

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.

so if we have two remote stations running a TCP server and a UDP Server then the client to connect them:

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.

to use the same SOCKADDR_IN structure sa_serv for the the udp server:

as long as the tpc and the udp servers on the same remote machine we only change the port:

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.

  • SOCKADDR_IN is the same for udp and tcp just change the value of port and sometimes address if server.

Upvotes: 1

dbush
dbush

Reputation: 225757

You can use the same copy of the sockaddr_in for TCP and UDP, but they have to be on different sockets.

A given socket created with AF_INET also specifies either SOCK_STREAM making TCP or SOCK_DGRAM making it UDP.

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.

Upvotes: 4

Related Questions