由于函数阻止程序执行而导致程序冻结

Program Freezing Due To recvfrom Function Blocking Program Execution

本文关键字:程序 执行 冻结 于函数 函数      更新时间:2023-10-16

目前我正在尝试创建一个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

UDPClass.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: %ldn", 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();
}

更新其他函数定义如下:

 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而不是阻塞。