Re: getting a binding error in server program
i assumed the code from the MSDN website would be right.
ok its set to true now
it is still displaying the error for setsockopt and now it is also displaying the error message from this code:
Code:
if(listen(m_serversocket,0) < 0)
{
CString text;
text.Format(_T("ERROR listening in the server socket: %d"), WSAGetLastError()); //---error---//
AfxMessageBox(text);
exit(1);
}
error is 10045
Re: getting a binding error in server program
Well, it is already one step forward!
But why don't you want to find the meaning adn desription of this error? Don't you have MSDN? Or are you banned by Google?
BTW, this error means:
Quote:
WSAEOPNOTSUPP (10045)
Operation not supported.
The attempted operation is not supported for the type of object referenced. Usually this occurs when a socket descriptor to a socket that cannot support this operation, for example, trying to accept a connection on a datagram socket.
For the listen function there is some additional info:
Quote:
The referenced socket is not of a type that supports the listen operation.
Re: getting a binding error in server program
ok so the error i get with listen is 10045 which is:
Quote:
Operation not supported. The attempted operation is not supported for the type of object referenced. Usually this occurs when a socket descriptor to a socket that cannot support this operation is trying to accept a connection on a datagram socket.
i don't know how to solve this problem. description of the error doesn't help
Re: getting a binding error in server program
You got this WSAEOPNOTSUPP (10045) trying to call function listen, so the error description in this case is much shorter:
Quote:
The referenced socket is not of a type that supports the listen operation.
It means that you did something was wrong between socket creation and calling listen.
The easiest way to find out what is wrong is to compare your code with some working example (from MSDN or other sites) for listening socket.
Re: getting a binding error in server program
i have changed so much code so i started again.
here is the current code:
Code:
WSADATA wsaData;
SOCKET ListeningSocket, NewConnection;
SOCKADDR_IN ServerAddr;
int Port = 7171;
char recvbuff[1024];
int i, nlen;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
{
CString text;
text.Format(_T("ERROR WSAStartup failed: %d"), WSAGetLastError());
AfxMessageBox(text);
exit (1);
}
//ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ListeningSocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (ListeningSocket == INVALID_SOCKET)
{
CString text;
text.Format(_T("ERROR at socket, error code: %d"), WSAGetLastError());
AfxMessageBox(text);
WSACleanup();
exit (1);
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
if (bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR)
{
CString text;
text.Format(_T("ERROR bind failed, Error code: %d"), WSAGetLastError());
AfxMessageBox(text);
closesocket(ListeningSocket);
WSACleanup();
exit (1);
}
if (listen(ListeningSocket, 5) == SOCKET_ERROR)
{
CString text;
text.Format(_T("ERROR listen: Error listening on socket: %d"), WSAGetLastError());
AfxMessageBox(text);
closesocket(ListeningSocket);
WSACleanup();
exit (1);
}
while(1)
{
NewConnection = SOCKET_ERROR;
while(NewConnection == SOCKET_ERROR)
{
NewConnection = accept(ListeningSocket, NULL, NULL);
//getsockname(ListeningSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));
memset(&ServerAddr, 0, sizeof(ServerAddr));
nlen = sizeof(ServerAddr);
int ret = sendto(ListeningSocket, recvbuff, strlen(recvbuff), 0, (sockaddr*)&ServerAddr, nlen);
if(ret < 0)
{
CString text;
text.Format(_T("ERROR Error broadcasting to the clients: %d"), WSAGetLastError());
AfxMessageBox(text);
}
//getpeername(NewConnection, (SOCKADDR *)&SenderInfo, &nlen);
}
}
if( shutdown(NewConnection, SD_SEND) != 0)
printf("\n\nServer: Well, there is something wrong with the shutdown. The error code: %ld\n", WSAGetLastError());
CString text;
text.Format(_T("ERROR Something wrong with shutdown, error code: %d"), WSAGetLastError());
AfxMessageBox(text);
SetTimer(0x01, 100, NULL);
}
now when i run it the error message "ERROR bind failed, Error code: 10049" displays
that error means
Quote:
Cannot assign requested address.
The requested address is not valid in its context. This normally results from an attempt to bind to an address that is not valid for the local computer. This can also result from connect, sendto, WSAConnect, WSAJoinLeaf, or WSASendTo when the remote address or port is not valid for a remote computer (for example, address or port 0).
Re: getting a binding error in server program
Didn't you find some working example of socket server application?
Re: getting a binding error in server program
not one that uses broadcast. its very difficult to find. if you could link any examples that would be great
Re: getting a binding error in server program
Why do you use INADDR_BROADCAST instead of INADDR_ANY for your server socket?
Re: getting a binding error in server program
i was told to use it. its a requirement
Re: getting a binding error in server program
Quote:
Originally Posted by
beginner91
i was told to use it. its a requirement
The first "requirement" for you is reading the documentation!
Understand?
If you read the documentation about INADDR_BROADCAST you'd know that it is used to send a broadcast (see http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx)
To receive the broadcasts socket uses INADDR_ANY (see the http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx)
Re: getting a binding error in server program
sorry those links don't work for me
Re: getting a binding error in server program
but i understand. so INADDR_ANY is used in the server and INADDR_BROADCAST is used in the client
Re: getting a binding error in server program
Quote:
Originally Posted by
beginner91
sorry those links don't work for me
Sorry! My fault... :blush:
I've edited links.
Re: getting a binding error in server program
ok so i changed it to INADDR_ANY but i'm still getting the same error with listen
Re: getting a binding error in server program
Quote:
Originally Posted by
beginner91
ok so i changed it to INADDR_ANY but i'm still getting the same error with listen
with listen or with bind?