c++ - Unable to send data over windows UDP sockets:Error Code 10035 -
i trying receive data on program program running on same windows 7 pc through sockets. have made 2 separate program, 1 sending , other receiving.send program showing success receive program waiting indefinitely.when put receive socket in non blocking mode receiving error code 10035 ie resource unavailable. there system setting have firewall or thing. although after disabling firewall getting same error.i searched stackoverflow.com not solution problem. giving code below send , receive functions. for send function:
#include "stdafx.h" #ifndef unicode #define unicode #endif #define win32_lean_and_mean #include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h> // link 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 error: %d\n", iresult); return 1; } //--------------------------------------------- // create socket sending data sendsocket = socket(af_inet, sock_dgram, ipproto_udp); if (sendsocket == invalid_socket) { wprintf(l"socket failed error: %ld\n", wsagetlasterror()); wsacleanup(); return 1; } //--------------------------------------------- // set recvaddr structure ip address of // receiver (in example case "178.0.0.100") // , 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 datagram receiver wprintf(l"sending datagram receiver...\n"); while(irun) { iresult = sendto(sendsocket, sendbuf, buflen, 0, (sockaddr *) & recvaddr, sizeof (recvaddr)); if (iresult == socket_error) { wprintf(l"sendto failed error: %d\n", wsagetlasterror()); //closesocket(sendsocket); //wsacleanup(); //return 1; } wprintf(l"send success :data bytes: %d\n", iresult); } //--------------------------------------------- // when application finished sending, close socket. wprintf(l"finished sending. closing socket.\n"); iresult = closesocket(sendsocket); if (iresult == socket_error) { wprintf(l"closesocket failed error: %d\n", wsagetlasterror()); wsacleanup(); return 1; } //--------------------------------------------- scanf("enter number terminate %c",&ch); // clean , quit. wprintf(l"exiting.\n"); wsacleanup(); return 0; //console::writeline(l"hello world"); //return 0; }
for receive function
#include "stdafx.h" #ifndef unicode #define unicode #endif #define win32_lean_and_mean #include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h> // link 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 error %d\n", iresult); return 1; } //----------------------------------------------- // create receiver socket receive datagrams recvsocket = socket(af_inet, sock_dgram, ipproto_udp); if (recvsocket == invalid_socket) { wprintf(l"socket failed error %d\n", wsagetlasterror()); return 1; } // setting socket non blocking mode if(ioctlsocket(recvsocket, fionbio, &nonblocking)!= 0) printf("can't set socket non blocking mode \n"); //----------------------------------------------- // bind socket address , 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 error %d\n", wsagetlasterror()); return 1; } //----------------------------------------------- // call recvfrom function receive datagrams // on 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 error %d\n", wsagetlasterror()); sleep(10); } //wprintf(l"recvfrom success %d\n", iresult); //wprintf(l"received data %s \n",recvbuf[buflen]); } //----------------------------------------------- // close socket when finished receiving datagrams wprintf(l"finished receiving. closing socket.\n"); iresult = closesocket(recvsocket); if (iresult == socket_error) { wprintf(l"closesocket failed error %d\n", wsagetlasterror()); return 1; } //----------------------------------------------- scanf("enter number terminate %c",&ch); // clean , exit. wprintf(l"exiting.\n"); wsacleanup(); return 0; //console::writeline(l"hello world"); //return 0; }
can 1 please help.
regards
mahendra
did up? winsock error code 10035 wsaewouldblock
. in non-blocking mode, , operation attempting cannot completed, because either send buffer full when sending or receive buffer empty when receiving.
Comments
Post a Comment