943,840 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3450
  • C++ RSS
Oct 29th, 2007
0

please help, winsock2 10061 error

Expand Post »
hello friends,

I have two program: receive.cpp and send.cpp

They re very simple, when compiling there is no error in both.

I only wanna see them to talk to each other but when run these progs,

both of them gives 10061 error. Below the codes, firstly receive.cpp:


C++ Syntax (Toggle Plain Text)
  1. //******************* SERVER PROG ****************
  2.  
  3. #include <winsock2.h>
  4. #include <stdio.h>
  5.  
  6. #define DEFAULT_BUFLEN 512
  7. #define DEFAULT_PORT "27015"
  8.  
  9. int __cdecl main() {
  10.  
  11.  
  12.  
  13. WSADATA wsaData;
  14. int iResult;
  15.  
  16. SOCKET ConnectSocket;
  17. struct sockaddr_in clientService;
  18.  
  19. char *sendbuf = "bu bir testtir.";
  20. char recvbuf[DEFAULT_BUFLEN];
  21. int recvbuflen = DEFAULT_BUFLEN;
  22.  
  23.  
  24. iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  25. if (iResult != NO_ERROR) {
  26. printf("WSAStartup failed: %d\n", iResult);
  27. return 1;
  28. }
  29.  
  30.  
  31. ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  32. if (ConnectSocket == INVALID_SOCKET) {
  33. printf("Error at socket(): %ld\n", WSAGetLastError() );
  34. WSACleanup();
  35. return 1;
  36. }
  37.  
  38. clientService.sin_family = AF_INET;
  39. clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
  40. clientService.sin_port = htons( 27015 );
  41.  
  42.  
  43. iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
  44. if ( iResult == SOCKET_ERROR) {
  45. closesocket (ConnectSocket);
  46. printf("Unable to connect to server: %ld\n", WSAGetLastError());
  47. WSACleanup();
  48. return 1;
  49. }
  50.  
  51.  
  52. iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
  53. if (iResult == SOCKET_ERROR) {
  54. printf("send failed: %d\n", WSAGetLastError());
  55. closesocket(ConnectSocket);
  56. //WSACleanup();
  57. return 1;
  58. }
  59.  
  60. printf("Bytes Sent: %ld\n", iResult);
  61.  
  62.  
  63. iResult = shutdown(ConnectSocket, SD_SEND);
  64. if (iResult == SOCKET_ERROR) {
  65. printf("shutdown failed: %d\n", WSAGetLastError());
  66. closesocket(ConnectSocket);
  67. WSACleanup();
  68. return 1;
  69. }
  70.  
  71.  
  72. do {
  73.  
  74. iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
  75. if ( iResult > 0 )
  76. printf("Bytes received: %d\n", iResult);
  77. else if ( iResult == 0 )
  78. printf("Connection closed\n");
  79. else
  80. printf("recv failed: %d\n", WSAGetLastError());
  81.  
  82. } while( iResult > 0 );
  83.  
  84.  
  85. closesocket(ConnectSocket);
  86. WSACleanup();
  87.  
  88. return 0;
  89. }

and the sender.cpp

C++ Syntax (Toggle Plain Text)
  1.  
  2. //*******************************CLIENT*********************************
  3. #include <winsock2.h>
  4. #include <stdio.h>
  5.  
  6. #define DEFAULT_BUFLEN 512
  7. #define DEFAULT_PORT 27015
  8.  
  9. int main() {
  10.  
  11.  
  12. int iResult;
  13. WSADATA wsaData;
  14.  
  15. SOCKET ConnectSocket;
  16. struct sockaddr_in clientService;
  17.  
  18. int recvbuflen = DEFAULT_BUFLEN;
  19. char *sendbuf = "Client: sending data test";
  20. char recvbuf[DEFAULT_BUFLEN] = "";
  21.  
  22.  
  23. iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  24. if (iResult != NO_ERROR) {
  25. printf("WSAStartup failed with error: %d\n", iResult);
  26. return 1;
  27. }
  28.  
  29.  
  30. ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  31. if (ConnectSocket == INVALID_SOCKET) {
  32. printf("socket failed with error: %ld\n", WSAGetLastError());
  33. WSACleanup();
  34. return 1;
  35. }
  36.  
  37.  
  38. clientService.sin_family = AF_INET;
  39. clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
  40. clientService.sin_port = htons( DEFAULT_PORT );
  41.  
  42.  
  43. iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
  44. if (iResult == SOCKET_ERROR) {
  45. printf( "connect failed with error: %d\n", WSAGetLastError() );
  46. closesocket(ConnectSocket);
  47. WSACleanup();
  48. return 1;
  49. }
  50.  
  51.  
  52. iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
  53. if (iResult == SOCKET_ERROR) {
  54. printf("send() failed with error: %d\n", WSAGetLastError());
  55. closesocket(ConnectSocket);
  56. WSACleanup();
  57. return 1;
  58. }
  59.  
  60. printf("Bytes Sent: %d\n", iResult);
  61.  
  62.  
  63. iResult = shutdown(ConnectSocket, SD_SEND);
  64. if (iResult == SOCKET_ERROR) {
  65. printf("shutdown failed with error: %d\n", WSAGetLastError());
  66. closesocket(ConnectSocket);
  67. WSACleanup();
  68. return 1;
  69. }
  70.  
  71.  
  72. do {
  73.  
  74. iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
  75. if ( iResult > 0 )
  76. printf("Bytes received: %d\n", iResult);
  77. else if ( iResult == 0 )
  78. printf("Connection closed\n");
  79. else
  80. printf("recv failed with error: %d\n", WSAGetLastError());
  81.  
  82. } while( iResult > 0 );
  83.  
  84.  
  85. closesocket(ConnectSocket);
  86. WSACleanup();
  87.  
  88. return 0;
  89. }

where I do wrong?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
inanc is offline Offline
3 posts
since Oct 2007
Oct 29th, 2007
0

Re: please help, winsock2 10061 error

Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Oct 29th, 2007
0

Re: please help, winsock2 10061 error

thanks my friend, I read the note but I still cant solve the problem.

my LAN ip is <snip>
I wonder that: if I must change the adress in the both program codes
to that ip or it must stay the same "127.0.0.1" ?
Last edited by Ancient Dragon; Oct 29th, 2007 at 12:51 pm. Reason: removed ip address
Reputation Points: 10
Solved Threads: 0
Newbie Poster
inanc is offline Offline
3 posts
since Oct 2007
Oct 29th, 2007
0

Re: please help, winsock2 10061 error

don't know --- try 127.0.0.1 and find out if it fixes your problem.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Oct 29th, 2007
0

Re: please help, winsock2 10061 error

don't know --- try 127.0.0.1 and find out if it fixes your problem.


I tried to change ip adresses but it still doesnt work
Reputation Points: 10
Solved Threads: 0
Newbie Poster
inanc is offline Offline
3 posts
since Oct 2007
Jul 27th, 2009
0

Re: please help, winsock2 10061 error

I do not see any Listen for port number in any of the applications ?
if you have setup listening for the ports in some way I do not know about, then perhaps your problem could be Firewall or Antivirus program blocking the attempt to connect to port
Reputation Points: 10
Solved Threads: 0
Newbie Poster
serup is offline Offline
1 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Concatenate strings/chars in a loop.
Next Thread in C++ Forum Timeline: ofstream corrupting file when program terminated





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC