Re: Why I am getting error no. 10014 (WSAEFAULT) when identifying a st

Tech-Archive recommends: Fix windows errors by optimizing your registry



Dear All,
I have written a very simple TCP server that receives a stream from a
client. I used windows xp with Visual C++ 2005 express edition. I am
facing a
problem of getting run time error no. 10014 (WSAEFAULT) returned by
WSAGetLastError() function whenever I try to identify any variable of type
string. otherwise it works fine. I would appreciate your help to clarify
to
me the reason behind this error. The code is as follow:

#include <iostream>
using std::cout;
using std::endl;

#include <winsock2.h>

#include <string>
using std::string;

int main (void)
{
WSADATA wsaData;
int Ret, ClientAddressLength, messageLength, QueueSize = 3;
SOCKET ListeningSocket, ConnectionSocket;
SOCKADDR_IN ServerAddress, ClientAddress;
unsigned short int port = 5150;
const int dataBufferSize = 1024;
char dataBuffer[dataBufferSize];
string myErrorMessage; //if I comment this line it works fine


//Load the winsock library
if ((Ret = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0 )
{
cout << "Winsock library could not be loaded"
<< "\nWSAStartup faild with error number: "
<< Ret << endl;
return -1;
}


//Creat a socket descriptor
if(( ListeningSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ))
== INVALID_SOCKET )
{
cout << "socket function returns error no. "
<< WSAGetLastError()
<< endl;
WSACleanup();
return -1;
}


//Identify the receiver IP address and port number
ServerAddress.sin_family = AF_INET;
ServerAddress.sin_port = htons(port);
ServerAddress.sin_addr.s_addr = htonl(INADDR_ANY);


//Bind the socket with the receiver address
if (bind(ListeningSocket, reinterpret_cast<SOCKADDR *>(&ServerAddress),
sizeof(ServerAddress)) == SOCKET_ERROR)
{
cout << "bind function returns error no. " << WSAGetLastError() <<
endl;
closesocket(ListeningSocket);
WSACleanup();
return -1;
}


//Listen for incomming connectiones
if (listen(ListeningSocket, QueueSize) == SOCKET_ERROR)
{
cout << "listen function returns error no. " << WSAGetLastError()
<<
endl;
closesocket(ListeningSocket);
WSACleanup();
return -1;
}


//Accept incoming connectiones
if((ConnectionSocket = accept(ListeningSocket,
reinterpret_cast<SOCKADDR *>(&ClientAddress),
&ClientAddressLength )) == INVALID_SOCKET)

ClientAddressLength should be initialized to sizeof(ClientAddress).

MassaRoddel


.



Relevant Pages