由于功能阻止程序执行而导致程序冻结

时间:2015-02-20 02:41:42

标签: c++ sockets network-programming udp winsock2

目前我正在尝试创建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: %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函数的错误 - 我想知道它是否阻塞?

1 个答案:

答案 0 :(得分:1)

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

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