please help, winsock2 10061 error

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 3
Reputation: inanc is an unknown quantity at this point 
Solved Threads: 0
inanc inanc is offline Offline
Newbie Poster

please help, winsock2 10061 error

 
0
  #1
Oct 29th, 2007
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:


  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

  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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: please help, winsock2 10061 error

 
0
  #2
Oct 29th, 2007
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 3
Reputation: inanc is an unknown quantity at this point 
Solved Threads: 0
inanc inanc is offline Offline
Newbie Poster

Re: please help, winsock2 10061 error

 
0
  #3
Oct 29th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: please help, winsock2 10061 error

 
0
  #4
Oct 29th, 2007
don't know --- try 127.0.0.1 and find out if it fixes your problem.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 3
Reputation: inanc is an unknown quantity at this point 
Solved Threads: 0
inanc inanc is offline Offline
Newbie Poster

Re: please help, winsock2 10061 error

 
0
  #5
Oct 29th, 2007
Originally Posted by Ancient Dragon View Post
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 1
Reputation: serup is an unknown quantity at this point 
Solved Threads: 0
serup serup is offline Offline
Newbie Poster

Re: please help, winsock2 10061 error

 
0
  #6
Jul 27th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC