2013-04-05 1111 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

相關問題