0

I'm designing a simple Battleship game using SFML. The player's grid is assigned to a 2D array int playerGridd[10][10] and vice versa. I tried to use SFML networking to send playerGridd through TCP, the sending part has no issue, but the receiving does.

socket.receive(playerGridd, sizeof(playerGridd), enemyGridd) does not work

sf::TcpListener listener;
if (listener.listen(port) != sf::Socket::Done) return;
std::cout << "Server is listening to port: " << port << ", waiting for 
connections..." << std::endl;

sf::TcpSocket socket;
if (listener.accept(socket) != sf::Socket::Done) return;
std::cout << "Client connected: " << socket.getRemoteAddress() << 
std::endl;

int playerGridd[gridSize][gridSize] = { 0 };
int enemyGridd[gridSize][gridSize] = { 0 };

while (state = play){
    sf::Event event;

    if (socket.send(playerGridd, sizeof(playerGridd) != 
sf::Socket::Done)) {
        cout << "Fail to send player's grid" << endl;
    }
    if (socket.receive(playerGridd, sizeof(playerGridd), enemyGridd) != 
sf::Socket::Done) {
        cout << "Fail to receive enemy's grid" << endl;
    }

1>c:\sfml\battleship\server.cpp(144): error C2664: 'sf::Socket::Status sf::TcpSocket::receive(sf::Packet &)': cannot convert argument 3 from 'int [10][10]' to 'size_t &'
  • It looks like you need to read the documentation for [`receive`](https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1TcpSocket.php#a90ce50811ea61d4f00efc62bb99ae1af). – molbdnilo Jun 24 '19 at 08:21
  • Sorry, i'm a newbie so the documentation does not make sense to me. Would you please specify what i did wrong ? – Thanh Tùng Jun 24 '19 at 08:37
  • The third argument should be a `size_t`, not an array. You should probably get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Jun 24 '19 at 08:49
  • You will save yourself a lot of trouble if you use the [sf:Packet](https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Packet.php) class instead of doing the byte-pushing manually. – nvoigt Jun 24 '19 at 09:29

0 Answers0