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

时间:2017-09-12 10:32:49

标签: c winsock2

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

package sample;

public class Person {
  private String name;
  private String surname;

  public Person(String name, String surname) {
    this.name = name;
    this.surname = surname;
  }

  public String getBasicView() {
    return name;
  }

  public String getExpandedView() {
    return name + "\n" + surname;
  }
}

现在是ping.h代码:

// 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;
    }
}

问题是我的程序停留在recvfrom函数上,我不明白为什么。 你能解释一下吗?

1 个答案:

答案 0 :(得分:2)

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

另请注意,UDP不保证传递。如果什么都没有收听你的数据包,它就会被扔掉。因此,您需要先发送 之前发送数据包。显然,这在单个线程中存在问题。通常,您将有两个单独的进程(不同的程序或通过分叉创建)。一个进程将创建一个套接字并侦听端口,另一个进程将创建一个套接字并发送数据。