4
votes

I want to have a server that

  • Can send and receive TCP messages.

  • Can send and receive UDP messages.

multiple clients that

  • Can send and receive TCP messages
  • Can send and receive UDP messages.

I use basic sockets and select for my server and the network SFML module for the client side (http://www.sfml-dev.org/tutorials/2.4/network-socket.php).

On the server side I create my socket, bind the TCP and UDP port to 4005.

On the client side I use the connect(host, ip) call for TCP, and I bind the UDP port to 4006, as my UDP port is already binded to 4005 on the server side.

At the moment, my TCP is working fine, I can send a receive a message on both sides(client and server).

I am trying to send a UDP packet from server to client. To do so, I am taking the sockaddr_in from the accepted client socket I want to send a message(the sockaddr_in filled by the accept function).

But when I send it, my client doesn't receive it.

I send it this way :

unsigned int UDPSocket::send(Message &buffer, sockaddr_in const &addr)
{
  sockaddr_in cpy;

  std::memcpy(&cpy, &addr, sizeof(sockaddr_in));
  //return ::send(_sock, buffer.getData().data(), buffer.getData().size(), 0);
  std::cout << "sending to " << buffer.getClientId() << std::endl;
  int fromsize = sizeof(sockaddr_in);
  cpy.sin_port = htons(4005 +1);
  std::cout << "sending to " << 4005 + 1 << std::endl;
  int ret = ::sendto(_sock, buffer.getMessage(), buffer.getSize(), 0, (struct sockaddr *) &cpy, fromsize);
  perror("sendto");
  std::cout << ret << std::endl;
  return ret;
}

ret is superior to 0 and perror return success, so the message should be sended.

I receive it this way :

bool              Network::updateQueueUDP()
{
  unsigned char   data[sizeof(Protocol::rtypePacket)];
  bool            ret = false;
  std::size_t     received;
  sf::IpAddress   sender;
  unsigned short  senderPort;

  if (_udpSocket.receive(data, sizeof(data), received, sender, senderPort) == sf::Socket::Done)
  {
    Message *msg = new Message();
    msg->setMessage(data, received, 0);
    _udpQueue.push(msg);
    std::cout << "pushing" << std::endl;
    ret = true;
  }
  return ret;
}

But receive never returns sf::Socket::Done

Do you have an idea why I don't receive anything in the UDP of my client ?

1
Where would I find the code or specification for _udpSocket.receive? I suspect you just don't understand precisely how it functions and it might not be the right function to use for receiving from UDP or sf::Socket::Done might not be the right return value to check for.Omnifarious
Well, hmmm... what return value are you getting?Omnifarious
@Omnifarious I got sf::Socket::NotReadyDimitri Danilov
Oh! You put the socket into non-blocking mode. Does your program wait until select says it's readable before calling updateQueueUDP? Also, one other case is that you might have too small a buffer, but that would result in Partial and not NotReady.Omnifarious

1 Answers

0
votes

I gived to the sendto function the wrong file descriptor, so it is normal it wouldn't send.