gpt4 book ai didi

c - recvfrom() 被卡住了,我不知道为什么

转载 作者:太空宇宙 更新时间:2023-11-04 08:01:41 25 4
gpt4 key购买 nike

我有一个项目要为我的学校做。目标是创建一个使用 echo 服务的类似 ping 的程序(C 编程)。回声服务通常在 UDP 端口 7 上运行,必须使用简单 TCP/IP 激活。如果 Echo 收到一条消息,它应该会回复一条消息。这是我编写的代码:

// ping.cpp : entry point

#include "ping.h"
#pragma comment(lib,"WS2_32") //Library


int main(int argc, char *argv[]) {

//Variable
char IPAdress[] = "127.0.0.1";
WORD versionProtocoleSocket;
SOCKET pingSocket;
struct sockaddr_in destination;
char packet[MAX_PACKET_SIZE], reply[MAX_PACKET_SIZE];
int err, msgLength;

//Initialisation of Windows Socket
printf("Initialising WSA 2.2...\n");
versionProtocoleSocket = MAKEWORD(2, 2);
err = initialiseSocket(versionProtocoleSocket);
if (err != 0) {
errSocket(err);
printf("The program will stop\n");
_getch();
return EXIT_FAILURE;
}
else
printf("Initialisation success\n\n");

//Socket creation
printf("Socket creation...\n");
if ((pingSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR) {
printf("Can't create socket\nThe program is going to stop\n");
_getch();
return EXIT_FAILURE;
}
else
printf("Creation sucess\n\n");

//Préparation des variables pour l'utilisation de la fonction sendto()
destination.sin_family = AF_INET;
destination.sin_port = htons(7);
destination.sin_addr.s_addr = inet_addr(adresseIP);
msgLength = sizeof(message);
int addrSize = sizeof(destination);

//Beginning of ping
for (int i = 0; i < 5; i++)
{
//Sending
if (sendto(pingSocket, message, messageLength, 0, (struct sockaddr *)&destination, addrSize) == SOCKET_ERROR)
printf("Can't send message : %d\n", WSAGetLastError());
else
printf("Message %d sent\n", i);
//Reception
if (recvfrom(pingSocket, reply, MAX_PACKET_SIZE, 0, NULL, NULL) == SOCKET_ERROR)
printf("Error : %d\n", WSAGetLastError());
else
printf("Message %d received\n", i);
}


_getch();
return EXIT_SUCCESS;
}

int initialiserSocket(WORD version) {
WSADATA wsaData;
int err = WSAStartup(version, &wsaData);
return err;
}

void errSocket(int err) {
switch (err) {
case WSASYSNOTREADY:
printf("System is not ready\n");
break;
case WSAVERNOTSUPPORTED:
printf("Unsuported protocol version\n");
break;
case WSAEINPROGRESS:
printf("A version is already running\n");
break;
case WSAEPROCLIM:
printf("Max task reached\n");
break;
case WSAEFAULT:
printf("pointer lpWSAData is not valid\n");
break;
default:
printf("Unknown error\n");
break;
}
}

现在是 ping.h 代码:

#pragma once
#include "stdio.h"
#include "winsock2.h"
#include "conio.h"
#include "stdint.h"

int initialiseSocket(WORD version);
void errSocket(int err);

#define MAX_PACKET_SIZE 10*1024

问题是我的程序卡在 recvfrom 函数上,我不知道为什么。你能解释一下吗?

最佳答案

您没有监听数据包的套接字。你需要bind您的接收套接字到您要使用的地址和端口。

另请注意,UDP 不保证交付。如果没有任何东西在监听你的数据包,它就会被丢弃。因此,您需要在发送数据包之前 收听。显然,这会在单线程中出现问题。通常,您会有两个独立的进程(不同的程序或通过 fork 创建)。一个进程将创建一个套接字并监听端口,另一个进程将创建一个套接字并发送数据。

关于c - recvfrom() 被卡住了,我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46174121/

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