我一直在互联网上搜索有关 C 语言套接字的更多信息,但找不到我想要的内容。
所以问题是这样的:
- 我们必须创建一个服务器来启动两个玩游戏的客户端进程。
- 这是一个简单的内存式游戏,需要两个数字作为输入
- 如果这两个数字对应相同的字母,
- 然后将该点设置为空白
- 否则设置为它对应的字母。
- 问题出在向两个客户端发送相同的数组。
- 两个客户端都可以单独玩游戏,但不能一起玩游戏。
- 玩家 1 的猜测不会出现在玩家 2 的控制台上。
所以我的问题是:如何更新每个子进程中的数组,以便将相同的数组写入两个客户端?
我并不是真的在寻找任何示例代码或任何东西,而是更多关于这个主题的概念性答案/更多资源。
谢谢!
最佳答案
粗略的伪代码概念是
初始化
connectPlayers();
if(player1)
{
generateGame();
do
{
bytesSent = sendArray to other players
} while (!allBytesSent)
}
else
{
do
{
bytesRead = readArray from server
} while (!allBytesRead)
}
游戏循环
while(!gameComplete)
{
if(!firstTime through as player 1)
{
waitforyourTurn();
read and update Game State; //reads another players guess and update array
}
result = doAGuess();
sendGuess(result); //sends guess to other player
passTurnToNextPlayer();
}
关于c - 使用套接字将相同的变量发送到多个客户端进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26900805/