- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目前我正在尝试创建一个 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 函数上做错了什么——我想知道它是否阻塞了?
最佳答案
关于c++ - 由于 recvfrom 函数阻塞程序执行导致程序卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28620823/
伙计们, 这个问题困扰我好久了,求大神指点!我在一台笔记本电脑上有服务器,它正在将数据发送到另一台笔记本电脑上的客户端,同一个局域网。我可以在服务器端和客户端的 wireshark 上看到 udp 数
这是我的 udp 服务器端代码 #include"headers.h" int main(int argc,char **argv) { //------------udp server so
给定len参数设置为10240,我必须反复调用该函数以获取所有数据。但这最终导致阻塞。如何获得所有数据并以独立于平台的方式安全返回? 顺便说一句,我在发送方使用netcat: cat ocr_pi.p
我正在使用 recvfrom 从网络接收多播消息。 系统上有两个网络接口(interface),recvfrom 只从第一个接口(interface)接收。 当第 2 个接口(interface)正常
我想处理 SIGUSR1 信号,所以我做了它需要的一切并且它工作了。但是在我的程序中,我在 recvfrom 等待,当我处理信号然后返回到 recvfrom 时,我从这个函数中得到了 -1。是否可以返
这是一个多客户端套接字程序。 当我尝试运行我的程序时,最后几个recvfrom() 不返回任何数据。但之前的recvfrom有多余的数据。有人可以帮我解决这个问题吗? 客户: while (1) {
我有一个 UDP 服务器实现,我在 recvfrom 调用中遇到段错误。 #define SIZEOF sizeof #define PKTSIZE 65535 char tmp_bu
在基于客户端-服务器架构的设置中,我的客户端通过大小为 116 字节的 UDP 套接字发送数据。但在服务器端,程序只接收24个字节。如有任何帮助,我们将不胜感激。 我尝试使用wireshark捕获数据
我正在编写一个简单的 udp 服务器应用程序,当我尝试获取远程连接用户的地址时遇到错误。 while(run == 1) { struct sockaddr_in client_addr;
我最近用 C 语言编写了一个 netflow 接收器,它通过 UDP 监听 netflow。一旦处理完流数据报,它就会将其写入系统日志。我的问题是,一旦我收到带有标准 recvfrom 函数的数据报包
我有一个简单的代码来发送和接收 ICMP 数据包,例如 ping,一切正常:我的数据包已发送并且收到了结果。我的recvfrom结果有问题,缓冲区中的ip dest/src不是服务器dest ip。
我有开源 cod(这是它的链接) 访问https://github.com/jtRIPper/dns-tcp-socks-proxy/blob/master/dns_proxy.c在鳕鱼的这一部分
在准备第一次编码 UDP 时,我正在尝试一些从 here 复制并稍微修改过的示例客户端和服务器代码。 。一切似乎都正常,除了由 recvfrom() 返回的值始终是缓冲区的大小而不是读取的字节数(如果
我正在用 C 语言编写一个简单的多线程客户端-服务器 UDP 程序,其中我一次向服务器发送多条消息,并在接收线程中等待回复。我这样做是为了测试发送和接收数据包的时间。 我已经用我的服务器在本地测试了
我需要实现以下行为:当服务器启动时,它应该使用广播检查现有服务器。然后它等待回答。 但是,如何设置等待超时? int optval = 1; char buff[BUFF_SIZE]; SOCKADD
您好,我是 Socket 编程的新手,我尝试创建一个客户端服务器应用程序,其中我的服务器是相机,我的 C++ 应用程序中是客户端。当我看到计算机和相机之间的数据包传输时,它显示相机在停止后发送了超过
我在使用 recvfrom() 时遇到一些问题,无法在 C++ 中获取所有数据包。 (然后阻塞不返回) 我发送了 1 个查询数据包,然后发送了一个响应。 它被分成多个805字节的数据包,然后以一个~2
我不明白 recvfrom 在以下情况下的行为:我有一个发送和接收 udp 数据包并休眠的循环;根据该位接收 (recvfrom) 延迟会有所不同。如果我睡了 22 毫秒或更长时间,则延迟约为 170
我在编写小型 DNS 服务器并将其 strip 化到最低限度时偶然发现了一个奇怪的行为。该程序应在 127.0.0.1:1337 上监听 DNS 查询并以拒绝回复。我通过发出 dig @localho
我正在尝试通过 udp 实现距离 vector 路由。我有以下结构: struct cost_info { uint8_t id; uint8_t pad; uint16_t
我是一名优秀的程序员,十分优秀!