问题描述
我正在尝试将基于 Linux 的网络井字游戏程序的一部分实施到 Winsock 服务器程序中。我遇到了一些问题,因为 Linux 版本在整数变量中打开了一个套接字:
int client_sock = 0;
然而,在 Winsock 应用程序中创建了一个 Csocket 对象:m_pClientSocket = new CSocket();
现在很明显,当我尝试编译将玩家移动到服务器的函数时:
if (!SendStatus(client_sock,MOVE)) {
Serverdisconnected();
}
我收到一个无效的变量类型错误,因为该函数需要一个 int 并且它正在接收一个 Csock 对象。
我的问题是如何让这些功能发挥作用?我试图得到套接字句柄和使用,但我不知道这是正确的方向,并使用这个 SendStatus(this->m_pClientSocket->GetSocketHandle(),WIN);
是给我的问题,因为它是一个静态函数。
发送状态函数:
bool SendStatus(int socket,StatusCode status)
{
char* data = (char*)&status;
size_t left = sizeof(status);
ptrdiff_t rc;
while (left)
{
rc = send(socket,data + sizeof(status) - left,left,0);
if (rc <= 0) return false;
left -= rc;
}
return true;
}
TakeTurn 函数:
bool TakeTurn(TTTBoard& board)
{
bool game_over = false;
bool input_good = false;
int row = 0,col = 0;
// display the board
board.DrawBoard();
while (!input_good)
{
printf("Enter move (row col): ");
// Make sure two integers were inputted
if (scanf_s("%d %d",&row,&col) == 2)
{
if (row < 0 || row > 2)
printf("Invalid row input. Try again.\n");
else if (col < 0 || col > 2)
printf("Invalid column input. Try again.\n");
else if (!board.IsBlank(row,col))
printf("That cell isn't blank. Try again.\n");
else
input_good = true;
}
else
printf("Invalid move input. Try again.\n");
// flush any data from the internal buffers
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
board.PlayerMakeMove(row,col);
cout << "Sending move to server" << endl;
// Send the move to the server
if (!SendStatus(client_sock,MOVE))
Serverdisconnected();
if (!SendInt(client_sock,row))
Serverdisconnected();
if (!SendInt(client_sock,col))
Serverdisconnected();
// Check for win/draw
if (board.IsWon())
{
cout << "YOU WIN!!!!" << endl;
game_over = true;
SendStatus(client_sock,WIN);
}
else if (board.IsDraw())
{
cout << "You tied ._." << endl;
game_over = true;
SendStatus(client_sock,DRAW);
}
return game_over;
}
希望我已经分享了足够的信息,我不想让问题变得臃肿,谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)