gpt4 book ai didi

c++ - 由于 recvfrom 函数阻塞程序执行导致程序卡住

转载 作者:行者123 更新时间:2023-11-28 06:25:24 28 4
gpt4 key购买 nike

目前我正在尝试创建一个 UDP 点对点网络程序。我有一个 UDP 类,它封装了为到网络上另一个客户端的单个连接设置套接字和端口所需的数据和函数,以及允许程序通过网络发送和接收数据包的函数。问题在于,虽然第二个客户端应用程序从第一个客户端接收到数据包并相应地更新其输出,但第一个客户端没有收到从第二个客户端发送的数据包更新。尽管事实上管理两个应用程序的网络代码实际上是相同的。 – 我已经为每个应用程序(最终将在不同的机器上运行)调用了 bind() 函数,因为我读到如果机器必须监听传入连接会更好。两个应用程序中也有一个 fd_set,它应该确保程序不会阻塞 recvfrom 函数,并且如果没有收到消息应该继续(但只有在应用程序启动时才会出现这种情况)。我的问题是:为什么 recvfrom 函数会停止 UDP 类中的程序执行?

这是客户端的代码:

#ifndef _UDPCLASS_H_
#define _UDPCLASS_H_

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <winsock2.h>
#include "Player.h"
using namespace std;

//Client port and IP
#define SERVERIP "127.0.0.1"
#define LISTENINGPORT 4444

class UDPClass
{

public:

//Functions
UDPClass(); //Constructor
~UDPClass(); //Destructor

struct Message
{
int objectID; //the ID number of the object
int x, y; //position
};

//Variables
SOCKET sock;
char message[MESSAGESIZE]; //Array of chars to store messages
sockaddr_in toAddr; //The address of the client socket

//Function Prototypes
//Functions related to sending packets to other players
void Initialise(); //Set up client socket
void sendMessage(); //Send data from player via message struct to server

//RECIEVING
//Functions related to recieving packets from other players
void recieveMessage();
void checkID(Message* mess);

//Variables
Message* playerStats; //Variable to hold the player's coordinates and ID
Message* player2Stats; //Variable to store player 2's statitstics such as position data in a message recieved from the server
PlayerClass* PlayerU; //Instance of class player to transfer data into message
PlayerClass* PlayerO; //Instance of player class to represent otehr player
private:
};

#endif

UDP类.cpp文件:

#include "UDPClass.h"

UDPClass::UDPClass()
{
//Initialises the socket to be a UDP socket
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
}
PlayerU = NULL;
PlayerO = NULL;
}

UDPClass::~UDPClass()
{
closesocket(sock);
WSACleanup();
}

void UDPClass::Initialise()
{
// Fill out a sockaddr_in structure with the address that
// we want to send to.
toAddr.sin_family = AF_INET;
// htons converts the port number to network byte order (big-endian).
toAddr.sin_port = htons(LISTENINGPORT);
toAddr.sin_addr.s_addr = inet_addr(SERVERIP);

//Bind the socket to the address of this address.
if (bind(sock, (LPSOCKADDR) &toAddr, sizeof(toAddr)) != 0)
{
printf("bind failed");
}
//Initialise an instance of type player class for the player of THIS client
PlayerU = new PlayerClass;
//NEW - in version 7.2 initialise an instance of the player class to store the data recieved in messages from other machines
PlayerO = new PlayerClass;

//Now we initialise a struct of type Message to contain data that we wish the other client to recieve
playerStats = new Message;
playerStats->objectID = PlayerU->getID();
//Put the players current position into Message struct containing data that will be sent to the server
playerStats->x = PlayerU->getX();
playerStats->y = PlayerU->getY();
printf("Control the player using the directional arrow keys ");
}

void UDPClass::sendMessage()
{
fflush(stdout);
playerStats->x = PlayerU->getX();
playerStats->y = PlayerU->getY();

int count = sendto(sock,
(char*) playerStats, sizeof(Message), 0, (SOCKADDR *)& toAddr, sizeof (toAddr));

if(count == SOCKET_ERROR)
{
printf("ERROR: %d \n", WSAGetLastError());
}
}

void UDPClass::recieveMessage()
{
//Variables describing the struct that defines the sockets the client is interested in listening to
fd_set readable;
FD_ZERO(&readable);
FD_SET(sock, &readable);

// The structure that describes how long to wait for something to happen.
timeval timeout;
// We want a 2.5-second timeout.
timeout.tv_sec = 2;
timeout.tv_usec = 500000;
fflush(stdin);
player2Stats = new Message;
// Read a response back from the server (or from anyone, in fact).
sockaddr_in fromAddr;
int fromAddrSize = sizeof(fromAddr);
int count = recvfrom(sock, (char*)player2Stats, sizeof(Message), 0,
(sockaddr *) &fromAddr, &fromAddrSize);


//Call checkID to give player two representation latest updates
checkID(player2Stats);

}

void UDPClass::checkID(Message* mess)
{
//Set the player stats of the other player this player is playing with to the values of the incoming message
PlayerO->SetID(mess->objectID);
PlayerO->SetX(mess->x);
PlayerO->SetY(mess->y);
}

在主应用程序中有一个连续的 while 循环执行网络功能,如下所示:

//Declare instance of a client to send and recieve messages
UDPClass client1;

//Initialise clients
client1.Initialise();
while(TRUE)
{
//Update the 2nd player position
client1.recieveMessage();
updateOther(client1);

client1.checkPlayPos(Player.x, Player.y);
client1.sendMessage();
}

updateOther 函数定义看起来像这样:

 void updateOther(UDPClass &theClient)
{
//Update the position of the 2nd player on screen by making the coordinates equal to data recieved from incoming message
Player2.x = theClient.PlayerO->getX();
Player2.y = theClient.PlayerO->getY();
}

在主while循环中,如果像这样注释掉与接收消息相关的两行代码:

/*client1.recieveMessage();
updateOther(client1);*/

程序运行良好,但如果它们被执行,程序就会卡住,所以我显然在 recvfrom 函数上做错了什么——我想知道它是否阻塞了?

最佳答案

recvfrom默认情况下始终阻塞,除非您的套接字是非阻塞的。

您可以使用 fcntl使用非阻塞套接字,然后 recvfrom 将返回 EWOULDBLOCK 而不是阻塞。

关于c++ - 由于 recvfrom 函数阻塞程序执行导致程序卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28620823/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com