0

我正在尝试通过套接字从在同一 Windows 7 pc 上运行的另一个程序接收程序的数据。为此,我制作了两个单独的程序,一个用于发送,另一个用于接收。发送程序显示成功,但接收程序无限期等待。当我将接收套接字置于非阻塞模式时,我收到错误代码 10035,即资源不可用。有没有我必须做的任何系统设置,比如防火墙或任何事情。虽然在禁用防火墙后我得到了同样的错误。我搜索了 stackoverflow.com 但无法解决我的问题。我在下面给出了发送和接收功能的代码。 对于发送功能:

#include "stdafx.h"
#ifndef UNICODE
#define UNICODE
#endif
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
// Link with ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")
using namespace System;
int main(array<System::String ^> ^args)
{   
    char ch;
    int iRun =1;
    int iResult;
    WSADATA wsaData;
    SOCKET SendSocket = INVALID_SOCKET;
    sockaddr_in RecvAddr;
    unsigned short Port = 51234;
    char SendBuf[1024]="Testing";
    int BufLen = 1024;
    //----------------------
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != NO_ERROR) {
        wprintf(L"WSAStartup failed with error: %d\n", iResult);
        return 1;
    }
    //---------------------------------------------
    // Create a socket for sending data
    SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (SendSocket == INVALID_SOCKET) {
        wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }
    //---------------------------------------------
    // Set up the RecvAddr structure with the IP address of
    // the receiver (in this example case "178.0.0.100")
    // and the specified port number.
    RecvAddr.sin_family = AF_INET;
    RecvAddr.sin_port = htons(Port);
    RecvAddr.sin_addr.s_addr = inet_addr("178.0.0.100");
    //---------------------------------------------
    // Send a datagram to the receiver
    wprintf(L"Sending a datagram to the receiver...\n");
    while(iRun) {
        iResult = sendto(SendSocket,
                     SendBuf, BufLen, 0, (SOCKADDR *) & RecvAddr, sizeof (RecvAddr));
       if (iResult == SOCKET_ERROR) {
        wprintf(L"sendto failed with error: %d\n", WSAGetLastError());
        //closesocket(SendSocket);
        //WSACleanup();
        //return 1;
    }
        wprintf(L"send success :data bytes: %d\n", iResult);
    } 
    //---------------------------------------------
    // When the application is finished sending, close the socket.
    wprintf(L"Finished sending. Closing socket.\n");
    iResult = closesocket(SendSocket);
    if (iResult == SOCKET_ERROR) {
        wprintf(L"closesocket failed with error: %d\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }
    //---------------------------------------------
    scanf("enter any number to terminate %c",&ch);
    // Clean up and quit.
    wprintf(L"Exiting.\n");
    WSACleanup();
    return 0;   
    //Console::WriteLine(L"Hello World");
    //return 0;
}

对于接收功能

#include "stdafx.h"    
#ifndef UNICODE
#define UNICODE
#endif    
#define WIN32_LEAN_AND_MEAN    
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>    
// Link with ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")    
using namespace System;    
int main(array<System::String ^> ^args)
{
    char ch;
    int iRun =1;
    int iResult = 0;    
    WSADATA wsaData;
    DWORD nonBlocking =1;    
    SOCKET RecvSocket;
    sockaddr_in RecvAddr;    
    unsigned short Port = 51234;    
    char RecvBuf[1024];
    int BufLen = 1024;    
    sockaddr_in SenderAddr;
    int SenderAddrSize = sizeof (SenderAddr);    
    //-----------------------------------------------
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != NO_ERROR) {
        wprintf(L"WSAStartup failed with error %d\n", iResult);
        return 1;
    }
    //-----------------------------------------------
    // Create a receiver socket to receive datagrams
    RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (RecvSocket == INVALID_SOCKET) {
        wprintf(L"socket failed with error %d\n", WSAGetLastError());
        return 1;
    }
            // Setting socket to non blocking mode
    if(ioctlsocket(RecvSocket, FIONBIO, &nonBlocking)!= 0)
            printf("can't Set socket to non blocking mode \n");
    //-----------------------------------------------
    // Bind the socket to any address and the specified port.
    RecvAddr.sin_family = AF_INET;
    RecvAddr.sin_port = htons(Port);
    RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);    
    iResult = bind(RecvSocket, (SOCKADDR *) & RecvAddr, sizeof (RecvAddr));
    if (iResult != 0) {
        wprintf(L"bind failed with error %d\n", WSAGetLastError());
        return 1;
    }
    //-----------------------------------------------
    // Call the recvfrom function to receive datagrams
    // on the bound socket.
    wprintf(L"Receiving datagrams...\n");
    while(iRun) {
    iResult = recvfrom(RecvSocket,
                       RecvBuf, BufLen, 0, (SOCKADDR *) & SenderAddr, &SenderAddrSize);
        if (iResult == SOCKET_ERROR) {
        wprintf(L"recvfrom failed with error %d\n", WSAGetLastError());
        Sleep(10);
        }
        //wprintf(L"recvfrom Success %d\n", iResult);
        //wprintf(L"Received Data %s \n",RecvBuf[BufLen]);
    }     
    //-----------------------------------------------
    // Close the socket when finished receiving datagrams
    wprintf(L"Finished receiving. Closing socket.\n");
    iResult = closesocket(RecvSocket);
    if (iResult == SOCKET_ERROR) {
        wprintf(L"closesocket failed with error %d\n", WSAGetLastError());
        return 1;
    }    
    //-----------------------------------------------
   scanf("enter any number to terminate %c",&ch);
    // Clean up and exit.
    wprintf(L"Exiting.\n");
    WSACleanup();
    return 0;

    //Console::WriteLine(L"Hello World");
    //return 0;
}

任何人都可以帮忙。

问候

马亨德拉

4

1 回答 1

0

你查了吗?Winsock 错误代码 10035 是WSAEWOULDBLOCK. 您处于非阻塞模式,您尝试的操作无法完成,因为发送时您的发送缓冲区已满或接收时您的接收缓冲区为空。

于 2013-04-05T03:29:46.627 回答