View Single Post
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