Forum: C++ Winsock

Forum huvudsida -> Programmering -> C++ Winsock

Sidor: 1

Till botten

achnorr 16:48 - 20:e Juni 2009 | Post #1
Medlem
Inlägg: 82


Skicka PM
Har problem med att skicka data mellan en klient och en server. Får ett 10014 error av WSAGetLastError() när servern försöker ta emot/skicka data. Hittar inte feletSmiley

WSAEFAULT
10014
Bad address.
The system detected an invalid pointer address in attempting to use a pointer argument of a call. This error occurs if an application passes an invalid pointer value, or if the length of the buffer is too small. For instance, if the length of an argument, which is a sockaddr structure, is smaller than the sizeof(sockaddr).

Här är koden:
  1. // TCP.h
  2.  
  3. #ifndef TCP_H
  4. #define TCP_H
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <winsock2.h>
  9.  
  10. using namespace std;
  11.  
  12. class TCP
  13. {
  14. private:
  15. int sockfd;
  16. int new_fd;
  17.  
  18. public:
  19. TCP( void );
  20. ~TCP( void );
  21. bool Create( void );
  22. bool Connect( string Adress, unsigned short int Port );
  23. bool Bind( int localport );
  24. bool Listen( int backlogs );
  25. bool Accept( void );
  26. int Send( string data );
  27. int Recv( char *buffer, int length );
  28. void Close( void );
  29. void Shutdown( void );
  30. };
  31.  
  32.  
  33. #endif


  1. // TCP.cpp
  2.  
  3. #include "TCP.h"
  4.  
  5.  
  6. TCP::TCP( void )
  7. {
  8. new_fd = 0;
  9. WSADATA wsa;
  10. WSAStartup( MAKEWORD( 1,1 ), &wsa );
  11. }
  12. TCP::~TCP( void )
  13. {
  14. WSACleanup();
  15. }
  16. bool TCP::Create( void )
  17. {
  18. sockfd = socket( AF_INET, SOCK_STREAM, NULL );
  19.  
  20. if( sockfd == INVALID_SOCKET )
  21. {
  22. cout << "Setup error" << endl;
  23. return false;
  24. }
  25. else
  26. {
  27. cout << "Setup Ok" << endl;
  28. return true;
  29. }
  30. }
  31. bool TCP::Bind( int localport )
  32. {
  33. struct sockaddr_in addr;
  34.  
  35. addr.sin_addr.s_addr = INADDR_ANY;
  36. addr.sin_family = AF_INET;
  37. addr.sin_port = htons( localport );
  38. memset( addr.sin_zero, 0, sizeof addr.sin_zero );
  39.  
  40. if( bind( sockfd, (struct sockaddr*) &addr, sizeof sockaddr_in ) == SOCKET_ERROR )
  41. {
  42. cout << "Bind error" << endl;
  43. return false;
  44. }
  45.  
  46. cout << "bind Ok" << endl;
  47. return true;
  48. }
  49. bool TCP::Listen( int backlogs )
  50. {
  51. if( listen( sockfd, backlogs ) == SOCKET_ERROR )
  52. {
  53. cout << "Listen error" << endl;
  54. return false;
  55. }
  56. cout << "Listen Ok" << endl;
  57. return true;
  58. }
  59. bool TCP::Accept( void )
  60. {
  61. sockaddr_in their_addr;
  62. int len = sizeof sockaddr_in;
  63. memset( &their_addr, 0, sizeof sockaddr_in );
  64.  
  65. new_fd = accept( sockfd, ( sockaddr* ) &their_addr, &len );
  66. if( new_fd == SOCKET_ERROR )
  67. {
  68. cout << "Accept error" << endl;
  69. return false;
  70. }
  71. cout << "Accept Ok" << endl;
  72. return true;
  73. }
  74. bool TCP::Connect( string Adress, unsigned short Port )
  75. {
  76. struct sockaddr_in addr;
  77.  
  78. addr.sin_family = AF_INET;
  79. addr.sin_port = htons( Port );
  80. addr.sin_addr.s_addr = inet_addr( Adress.c_str() );
  81. memset( addr.sin_zero, 0, sizeof addr.sin_zero );
  82.  
  83. if( connect( sockfd, ( const sockaddr* ) &addr, sizeof sockaddr ) == SOCKET_ERROR )
  84. {
  85. cout << "Connection error" << endl;
  86. return false;
  87. }
  88.  
  89. cout << "Connection Ok" << endl;
  90. return true;
  91. }
  92.  
  93. int TCP::Send( string data )
  94. {
  95. if( new_fd == NULL )
  96. return send( sockfd, data.c_str(), strlen( data.c_str() ), NULL );
  97. else
  98. return send( new_fd, data.c_str(), strlen( data.c_str() ), NULL );
  99. }
  100. int TCP::Recv( char *buffer, int length )
  101. {
  102. if( new_fd == NULL )
  103. return recv( sockfd, buffer, length, NULL );
  104. else
  105. return recv( new_fd, buffer, length, NULL );
  106. }
  107. void TCP::Close( void )
  108. {
  109. cout << "Close Ok" << endl;
  110. closesocket( sockfd );
  111. }
  112. void TCP::Shutdown( void )
  113. {
  114. shutdown( new_fd, SD_BOTH );
  115. }


  1. // main.cpp
  2.  
  3. #pragma comment( lib, "WSock32.Lib" )
  4.  
  5. #include <iostream>
  6. #include <winsock2.h>
  7. #include <string>
  8.  
  9. #include "TCP.h"
  10.  
  11. using namespace std;
  12.  
  13. #define PORT 1337
  14. #define ADRESS "127.0.0.1"
  15.  
  16.  
  17.  
  18. int main()
  19. {
  20. TCP test;
  21.  
  22. string val;
  23. char *buffer = "";
  24.  
  25. cout << "1 - Server" <<endl << "2 - Client" << endl;
  26. cin >> val;
  27.  
  28. if( val == "1" )
  29. {
  30. cout << "--------SERVER--------" << endl;
  31. test.Create();
  32. test.Bind( PORT );
  33.  
  34. while( true )
  35. {
  36. test.Listen( 10 );
  37. test.Accept();
  38. cout << "Received: " << test.Recv( buffer, 512 ) << endl;
  39. cout << "WSAERROR: " << WSAGetLastError() << endl;
  40. cout << "Buffer: " << buffer << endl;
  41. }
  42.  
  43. }
  44. else
  45. {
  46. cout << "--------CLIENT--------" << endl;
  47. test.Create();
  48. test.Connect( ADRESS, PORT );
  49. test.Send( "hej" );
  50. cout << "WSAERROR: " << WSAGetLastError() << endl;
  51. test.Close();
  52. }
  53.  
  54. cout << "klar" << endl;
  55. cin.get();
  56. cin.get();
  57.  
  58.  
  59. return 0;
  60. }


-------------------------
Ohhoo*



Slash 19:49 - 20:e Juni 2009 | Post #2
Medlem
Inlägg: 141


Skicka PM
Jag vet inte om det är det som den klagar på, men rad 23 i main.cpp ser inte riktigt bra ut.

Det jag skulle gissa att du vill ha är:
  1. char buffer[512];





-------------------------
Ingen sigantur!



achnorr 20:02 - 20:e Juni 2009 | Post #3
Medlem
Inlägg: 82


Skicka PM
LOOL. Ett så simpelt fel har tagit upp flera timmar.
Men tackar slash!

-------------------------
Ohhoo*



Sidor: 1

Forum huvudsida -> Programmering -> C++ Winsock
Atom feed

Du får inte posta i den här tråden | Till toppen