Re: Why I am getting error no. 10014 (WSAEFAULT) when identifying a st
- From: "Arkady Frenkel" <arkadyf@xxxxxxxxxxxxxxxx>
- Date: Wed, 17 May 2006 09:09:29 +0200
But you forgot to mention what winsock function failed in your code?
Arkady
"Bassam" <Bassam@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:5CA6866C-96F7-4CAD-8CFD-7C32D95ED934@xxxxxxxxxxxxxxxx
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)
{
cout << "accept function returns error no. " << WSAGetLastError() <<
endl;
closesocket(ListeningSocket);
WSACleanup();
return -1;
}
cout << "Connection has established with the peer client: "
<< inet_ntoa(ClientAddress.sin_addr) << endl;
if ((Ret = recv(ConnectionSocket, dataBuffer, sizeof(dataBuffer), 0))
== SOCKET_ERROR)
{
cout << "recv function returns error no. " << WSAGetLastError() <<
endl;
closesocket(ConnectionSocket);
WSACleanup();
return -1;
}
cout << "The received data is: " << dataBuffer << endl;
closesocket(ConnectionSocket);
closesocket(ListeningSocket);
//Frre up all the resources reserved by the socket
if (WSACleanup() == SOCKET_ERROR)
{
cout << "Failed to return system resources"
<< "\nWSACleanup failed with error number: " <<
WSAGetLastError()
<< endl;
}
return 0;
}
As it has declared on the top of the program, whenever I identify a
variable
of type string (myErrorMessage in this program) to use it somewhere in the
program I get the mentioned error.
Your help is appreciated
Regards
Bassam
.
- Follow-Ups:
- References:
- Prev by Date: Re: WSACleanup deadlock
- Next by Date: Re: LSP - Getting remote and local port for connection
- Previous by thread: Why I am getting error no. 10014 (WSAEFAULT) when identifying a st
- Next by thread: Re: Why I am getting error no. 10014 (WSAEFAULT) when identifying
- Index(es):
Relevant Pages
|