2013-04-05 96 views
0

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

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

任何一个可以请帮助。

问候

马亨德拉

+0

是你所在机器的IP地址吗?你的路由表是什么样的? – 2013-04-05 02:44:03

+0

此外。在接收器中你绑定了INADDR_ANY - 这不是非常确定的。可能你可以尝试绑定到你的发件人相同的地址。如果没有什么只是使用环回地址,看看它是否工作。 – nanda 2013-04-05 02:48:09

+0

嗨尼古拉是的IP地址是我自己的pc.sorry但路由表我没有得到。 – 2013-04-05 02:52:11

回答

0

你看它? Winsock错误代码10035是WSAEWOULDBLOCK。您处于非阻塞模式,并且您尝试执行的操作无法完成,因为发送缓冲区在发送时已满,或者在接收时您的接收缓冲区为空。

+0

是的,我也认为这是问题......但如何解决这个问题......我正在第一次在套接字编程..当我把接收程序在阻塞模式下接收程序无限期地等待...... – 2013-04-05 03:45:23

+0

@MAHENDRAKUMAR如果在操作在阻塞模式下被阻塞时不知道要执行什么操作,为什么还要使用非阻塞模式?只需*使用*阻止模式。 – EJP 2013-04-05 09:31:03

相关问题