-
getting a binding error in server program
i created a client server program in MFC but when i run the server the error message "ERROR binding in the server socket" displays.
this is the first time i am using code like this so I don't know how to fix this error.
here is the code:
Code:
void CSocketTestServerDlg::StartServer()
{
WORD w = MAKEWORD(1,1);
WSADATA wsadata;
::WSAStartup(w, &wsadata);
char opt = 1;
setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char*)&opt, sizeof(char));
SOCKADDR_IN brdcastaddr;
int portno = 1818;
memset(&brdcastaddr,0, sizeof(brdcastaddr));
brdcastaddr.sin_family = AF_INET;
brdcastaddr.sin_port = htons(portno);
brdcastaddr.sin_addr.s_addr = INADDR_BROADCAST;
m_serversocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(m_serversocket == -1)
{
AfxMessageBox("Socket Initialiation Error");
}
int len = sizeof(brdcastaddr);
char sbuf[1024];
int ret = sendto(m_serversocket, sbuf, strlen(sbuf), 0, (sockaddr*)&brdcastaddr, len);
if(ret < 0)
{
AfxMessageBox("ERROR binding in the server socket");
exit(1);
}
if(listen(m_serversocket,0) < 0)
{
AfxMessageBox("ERROR listening in the server socket");
exit(1);
}
SetTimer(0x01, 100, NULL);
}
-
Re: getting a binding error in server program
From MSDN article "sendto":
Quote:
Return value
If no error occurs, sendto returns the total number of bytes sent, which can be less than the number indicated by len. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
So, why did you miss/ingore to call WSAGetLastError?
-
Re: getting a binding error in server program
like a said i am new to this kind of code. but adding WSAGetLastError doesn't do anything
AfxMessageBox("ERROR binding in the server socket", WSAGetLastError() );
-
Re: getting a binding error in server program
Quote:
Originally Posted by
beginner91
like a said i am new to this kind of code. but adding WSAGetLastError doesn't do anything
AfxMessageBox("ERROR binding in the server socket", WSAGetLastError() );
WSAGetLastError gives zou the error code, one of the possible codes listed in the sendto article!
So which one is in your case?
-
Re: getting a binding error in server program
the program doesn't run when i add WSAGetLastError to the messagebox. it just builds then nothing happens
do i need to add more code?
-
Re: getting a binding error in server program
Quote:
Originally Posted by
beginner91
the program doesn't run when i add WSAGetLastError to the messagebox. it just builds then nothing happens
do i need to add more code?
You must first learn the basics of C++/Visual C++ programming! Like using MessageBox, preparing/formatting text for output and so on...
The next steps are reading documentation and debugging.
Only after that you wil be probably able to develop more complicated things like sockets.
Example of using WSAGetLastError:
Code:
CString text;
text.Format(_T("ERROR calling the socket 'sendto' function: %d"), WSAGetLastError());
AfxMessageBox(text);
-
Re: getting a binding error in server program
thank you for the code. the error code is 10013
-
Re: getting a binding error in server program
It is WSAEACCES (10013)
From MSDN (October 2000):
Quote:
WSAEACCES [/B](10013)
Permission denied.
An attempt was made to access a socket in a way forbidden by its access permissions. An example is using a broadcast address for "sendto" without broadcast permission being set using setsockopt(SO_BROADCAST).
From the MSDN sendto:
Quote:
WSAEACCES
The requested address is a broadcast address, but the appropriate flag was not set. Call setsockopt with the SO_BROADCAST parameter to allow the use of the broadcast address.
-
Re: getting a binding error in server program
so the problem is with is line of code:
setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char*)&opt, sizeof(char));
i am using the SO_BROADCAST parameter so what is the problem with the code?
-
Re: getting a binding error in server program
BTW, You called setsockopt but didn't check its error code. Why?
-
Re: getting a binding error in server program
ok i didn't check the error code for anything in my program so i made some changes
i used the code from the here http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
Code:
int iResult = 0;
WSADATA wsaData;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
BOOL bOptVal = FALSE;
int bOptLen = sizeof (BOOL);
int iOptVal = 0;
int iOptLen = sizeof (int);
// WORD w = MAKEWORD(1,1);
// WSADATA wsadata;
// ::WSAStartup(w, &wsadata);
//char opt = 1;
//setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char*)&opt, sizeof(char));
bOptVal = TRUE;
iResult = getsockopt(m_serversocket, SOL_SOCKET, SO_KEEPALIVE, (char *) &iOptVal, &iOptLen);
if (iResult == SOCKET_ERROR)
{
CString text;
text.Format(_T("getsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
AfxMessageBox(text);
}
else
{
CString text;
text.Format(_T("SO_KEEPALIVE Value: %ld\n"), iOptVal);
AfxMessageBox(text);
}
iResult = setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char *) &iOptVal, &iOptLen);
if (iResult == SOCKET_ERROR)
{
CString text;
text.Format(_T("setsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
AfxMessageBox(text);
}
else
AfxMessageBox("ERROR binding in the server socket");
iResult = getsockopt(m_serversocket, SOL_SOCKET, SO_KEEPALIVE, (char *) &iOptVal, &iOptLen);
if (iResult == SOCKET_ERROR)
{
CString text;
text.Format(_T("getsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
AfxMessageBox(text);
} else
{
CString text;
text.Format(_T("SSO_KEEPALIVE Value: %ld\n"), iOptVal);
AfxMessageBox(text);
}
closesocket(m_serversocket);
WSACleanup();
but now i am getting this compile error:
Error 2 error C2664: 'setsockopt' : cannot convert parameter 5 from 'int *__w64 ' to 'int'
-
Re: getting a binding error in server program
Well, if you cannot correctly do so simple copy/paste operation then I cannot help you more... :eek:
Your code differs from the MSDN original one!
-
Re: getting a binding error in server program
One more thing: please, use Code tags while posting code snippets! See Announcement: Before you post....
-
Re: getting a binding error in server program
i did not copy and paste since i had different names for things but it is based from the MSDN code
-
Re: getting a binding error in server program
-
Re: getting a binding error in server program
Quote:
Originally Posted by
beginner91
i had different names for things but it is based from the MSDN code
Well, it is based from the MSDN code but with your own bugs implemented in there!
Please, compare!
-
Re: getting a binding error in server program
ok i fixed that error i was getting.
now a load of the error messages i added are displaying when u run the program:
getsockopt for SO_KEEPALIVE failed with error: 10038
setsockopt for SO_KEEPALIVE failed with error: 10038
getsockopt for SO_KEEPALIVE failed with error: 10038
ERROR binding in the server socket: 10013
-
Re: getting a binding error in server program
From MSDN:
Quote:
WSAENOTSOCK (10038)
Socket operation on non-socket.
An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for select, a member of an fd_set was not valid.
I'd recommend you to first test MSDN example without your own changes.
Only after it will work - try to change it according to your needs!
-
Re: getting a binding error in server program
ok i moved some code around. the problem was it wasn't reading a socket.
now the only error message displaying is the one that started this problem:
ERROR binding with server socket 10013
all the code:
Code:
void CSocketTestServerDlg::StartServer()
{
int iResult = 0;
WSADATA wsaData;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
BOOL bOptVal = FALSE;
int bOptLen = sizeof (BOOL);
int iOptVal = 0;
int iOptLen = sizeof (int);
// WORD w = MAKEWORD(1,1);
// WSADATA wsadata;
// ::WSAStartup(w, &wsadata);
//char opt = 1;
//setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char*)&opt, sizeof(char));
SOCKADDR_IN brdcastaddr;
int portno = 1818;
memset(&brdcastaddr,0, sizeof(brdcastaddr));
brdcastaddr.sin_family = AF_INET;
brdcastaddr.sin_port = htons(portno);
brdcastaddr.sin_addr.s_addr = INADDR_BROADCAST;
m_serversocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); //listening socket
if(m_serversocket == -1)
{
AfxMessageBox("Socket Initialiation Error");
}
//if(bind(m_serversocket, (SOCKADDR*)&serveraddr,sizeof(SOCKADDR_IN)) < 0)
int len = sizeof(brdcastaddr);
char sbuf[1024];
int ret = sendto(m_serversocket, sbuf, strlen(sbuf), 0, (sockaddr*)&brdcastaddr, len);
if(ret < 0)
{
CString text;
text.Format(_T("ERROR binding in the server socket: %d"), WSAGetLastError());
AfxMessageBox(text);
exit(1);
}
if(listen(m_serversocket,0) < 0)
{
AfxMessageBox("ERROR listening in the server socket");
exit(1);
}
bOptVal = TRUE;
iResult = getsockopt(m_serversocket, SOL_SOCKET, SO_KEEPALIVE, (char *) &iOptVal, &iOptLen);
if (iResult == SOCKET_ERROR)
{
CString text;
text.Format(_T("getsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
AfxMessageBox(text);
}
else
{
CString text;
text.Format(_T("SO_KEEPALIVE Value: %ld\n"), iOptVal);
AfxMessageBox(text);
}
//------------------------ set socket ---------------------------------//
iResult = setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen);
if (iResult == SOCKET_ERROR)
{
CString text;
text.Format(_T("setsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
AfxMessageBox(text);
}
else
{
AfxMessageBox("ERROR binding in the server socket");
}
iResult = getsockopt(m_serversocket, SOL_SOCKET, SO_KEEPALIVE, (char *) &iOptVal, &iOptLen);
if (iResult == SOCKET_ERROR)
{
CString text;
text.Format(_T("getsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
AfxMessageBox(text);
}
else
{
CString text;
text.Format(_T("SSO_KEEPALIVE Value: %ld\n"), iOptVal);
AfxMessageBox(text);
}
closesocket(m_serversocket);
WSACleanup();
SetTimer(0x01, 100, NULL);
}
-
Re: getting a binding error in server program
Quote:
Originally Posted by
beginner91
ok i moved some code around. the problem was it wasn't reading a socket.
now the only error message displaying is the one that started this problem:
ERROR binding with server socket 10013
Please, go back to the post#4!
-
Re: getting a binding error in server program
yes i know what that error means
Quote:
It is WSAEACCES (10013)
From MSDN (October 2000):
WSAEACCES [/B](10013)
Permission denied.
An attempt was made to access a socket in a way forbidden by its access permissions. An example is using a broadcast address for "sendto" without broadcast permission being set using setsockopt(SO_BROADCAST).
From the MSDN sendto:
WSAEACCES
The requested address is a broadcast address, but the appropriate flag was not set. Call setsockopt with the SO_BROADCAST parameter to allow the use of the broadcast address.
but i then started adding all the error messages so the problem was never solved. what do i need to do to fix it?
-
Re: getting a binding error in server program
Quote:
Originally Posted by
beginner91
yes i know what that error means
Then why do you ignore the way to fix it:
Quote:
The requested address is a broadcast address, but the appropriate flag was not set. Call setsockopt with the SO_BROADCAST parameter to allow the use of the broadcast address.
-
Re: getting a binding error in server program
but i have done that:
Code:
setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen);
-
Re: getting a binding error in server program
-
Re: getting a binding error in server program
please look at post #19
i edited the code and put in the line //----------- set socket ---------// so it is easy to find
-
Re: getting a binding error in server program
It's you who has to look at your own code! :mad:
Your setsockopt call is commented out!
-
Re: getting a binding error in server program
Quote:
Originally Posted by
beginner91
ok i moved some code around. the problem was it wasn't reading a socket.
now the only error message displaying is the one that started this problem:
ERROR binding with server socket 10013
all the code:
Code:
void CSocketTestServerDlg::StartServer()
{
int iResult = 0;
WSADATA wsaData;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
BOOL bOptVal = FALSE;
int bOptLen = sizeof (BOOL);
int iOptVal = 0;
int iOptLen = sizeof (int);
// WORD w = MAKEWORD(1,1);
// WSADATA wsadata;
// ::WSAStartup(w, &wsadata);
//char opt = 1;
//setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char*)&opt, sizeof(char));
SOCKADDR_IN brdcastaddr;
int portno = 1818;
memset(&brdcastaddr,0, sizeof(brdcastaddr));
brdcastaddr.sin_family = AF_INET;
brdcastaddr.sin_port = htons(portno);
brdcastaddr.sin_addr.s_addr = INADDR_BROADCAST;
m_serversocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); //listening socket
if(m_serversocket == -1)
{
AfxMessageBox("Socket Initialiation Error");
}
//if(bind(m_serversocket, (SOCKADDR*)&serveraddr,sizeof(SOCKADDR_IN)) < 0)
int len = sizeof(brdcastaddr);
char sbuf[1024];
int ret = sendto(m_serversocket, sbuf, strlen(sbuf), 0, (sockaddr*)&brdcastaddr, len);
if(ret < 0)
{
CString text;
text.Format(_T("ERROR binding in the server socket: %d"), WSAGetLastError());
AfxMessageBox(text);
exit(1);
}
...
}
Don't you see it?
Besides, this line (being uncommeted) must be after socket creation.
-
Re: getting a binding error in server program
that is the old setsockopt. sorry i just commented it out instead of removing it
i rewrote it when i added the error message code as you can see if you look at the code i posted and look for //----------- set socket ---------// comment
-
Re: getting a binding error in server program
Quote:
Originally Posted by
beginner91
that is the old setsockopt. sorry i just commented it out instead of removing it
i rewrote it when i added the error message code as you can see if you look at the code i posted and look for //----------- set socket ---------// comment
Please, post your final code now.
-
Re: getting a binding error in server program
final code:
Code:
void CSocketTestServerDlg::StartServer()
{
//---------------------------------------
// Declare variables
int iResult = 0;
WSADATA wsaData;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
BOOL bOptVal = FALSE;
int bOptLen = sizeof (BOOL);
int iOptVal = 0;
int iOptLen = sizeof (int);
SOCKADDR_IN brdcastaddr;
int portno = 1818;
memset(&brdcastaddr,0, sizeof(brdcastaddr));
brdcastaddr.sin_family = AF_INET;
brdcastaddr.sin_port = htons(portno);
brdcastaddr.sin_addr.s_addr = INADDR_BROADCAST;
//---------------------------------------
// Create a listening socket
m_serversocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(m_serversocket == -1)
{
AfxMessageBox("Socket Initialiation Error");
}
int len = sizeof(brdcastaddr);
char sbuf[1024];
int BufLen = 1024;
int ret = sendto(m_serversocket, sbuf, strlen(sbuf), 0, (sockaddr*)&brdcastaddr, len);
if(ret < 0)
{
CString text;
text.Format(_T("ERROR binding in the server socket: %d"), WSAGetLastError());
AfxMessageBox(text);
exit(1);
}
if(listen(m_serversocket,0) < 0)
{
AfxMessageBox("ERROR listening in the server socket");
exit(1);
}
bOptVal = TRUE;
iResult = getsockopt(m_serversocket, SOL_SOCKET, SO_KEEPALIVE, (char *) &iOptVal, &iOptLen);
if (iResult == SOCKET_ERROR)
{
CString text;
text.Format(_T("getsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
AfxMessageBox(text);
}
else
{
CString text;
text.Format(_T("SO_KEEPALIVE Value: %ld\n"), iOptVal);
AfxMessageBox(text);
}
iResult = setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen);
if (iResult == SOCKET_ERROR)
{
CString text;
text.Format(_T("setsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
AfxMessageBox(text);
}
else
{
AfxMessageBox("ERROR binding in the server socket");
}
iResult = getsockopt(m_serversocket, SOL_SOCKET, SO_KEEPALIVE, (char *) &iOptVal, &iOptLen);
if (iResult == SOCKET_ERROR)
{
CString text;
text.Format(_T("getsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
AfxMessageBox(text);
}
else
{
CString text;
text.Format(_T("SO_KEEPALIVE Value: %ld\n"), iOptVal);
AfxMessageBox(text);
}
closesocket(m_serversocket);
WSACleanup();
SetTimer(0x01, 100, NULL);
}
sorry for the confusion
-
Re: getting a binding error in server program
Quote:
Originally Posted by
beginner91
final code:
Code:
void CSocketTestServerDlg::StartServer()
{
//---------------------------------------
// Declare variables
int iResult = 0;
WSADATA wsaData;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
BOOL bOptVal = FALSE;
int bOptLen = sizeof (BOOL);
int iOptVal = 0;
int iOptLen = sizeof (int);
SOCKADDR_IN brdcastaddr;
int portno = 1818;
memset(&brdcastaddr,0, sizeof(brdcastaddr));
brdcastaddr.sin_family = AF_INET;
brdcastaddr.sin_port = htons(portno);
brdcastaddr.sin_addr.s_addr = INADDR_BROADCAST;
//---------------------------------------
// Create a listening socket
m_serversocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(m_serversocket == -1)
{
AfxMessageBox("Socket Initialiation Error");
}
int len = sizeof(brdcastaddr);
char sbuf[1024];
int BufLen = 1024;
int ret = sendto(m_serversocket, sbuf, strlen(sbuf), 0, (sockaddr*)&brdcastaddr, len);
if(ret < 0)
{
CString text;
text.Format(_T("ERROR binding in the server socket: %d"), WSAGetLastError());
AfxMessageBox(text);
exit(1);
}
if(listen(m_serversocket,0) < 0)
{
AfxMessageBox("ERROR listening in the server socket");
exit(1);
}
bOptVal = TRUE;
iResult = getsockopt(m_serversocket, SOL_SOCKET, SO_KEEPALIVE, (char *) &iOptVal, &iOptLen);
if (iResult == SOCKET_ERROR)
{
CString text;
text.Format(_T("getsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
AfxMessageBox(text);
}
else
{
CString text;
text.Format(_T("SO_KEEPALIVE Value: %ld\n"), iOptVal);
AfxMessageBox(text);
}
iResult = setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen);
if (iResult == SOCKET_ERROR)
{
CString text;
text.Format(_T("setsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
AfxMessageBox(text);
}
else
{
AfxMessageBox("ERROR binding in the server socket");
}
iResult = getsockopt(m_serversocket, SOL_SOCKET, SO_KEEPALIVE, (char *) &iOptVal, &iOptLen);
if (iResult == SOCKET_ERROR)
{
CString text;
text.Format(_T("getsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
AfxMessageBox(text);
}
else
{
CString text;
text.Format(_T("SO_KEEPALIVE Value: %ld\n"), iOptVal);
AfxMessageBox(text);
}
closesocket(m_serversocket);
WSACleanup();
SetTimer(0x01, 100, NULL);
}
sorry for the confusion
But the line you have highlighted does NOT execute! Just because your "program" exits about 15...20 lines "ealier"!
Again: reread my post#6 and begin from the basics and learning how to debug!
-
Re: getting a binding error in server program
i never realized the line didn't get executed.
is it because its in the wrong place?
-
Re: getting a binding error in server program
Yes.
And the third time: you must debug your code to not only realize but also be sure that something gets executed or not.
-
Re: getting a binding error in server program
ok i debugged it and went through each line
these are the lines that don't get executed:
WSADATA wsaData;
SOCKADDR_IN brdcastaddr;
char sbuf[1024];
-
Re: getting a binding error in server program
Quote:
Originally Posted by
beginner91
ok i debugged it and went through each line
these are the lines that don't get executed:
WSADATA wsaData;
SOCKADDR_IN brdcastaddr;
char sbuf[1024];
Because there is nothing to execute! They all are just the definitions!
-
Re: getting a binding error in server program
well i don't what the problem is. those are the only lines that don't get executed and then i reach the error message
-
Re: getting a binding error in server program
Quote:
Originally Posted by
beginner91
well i don't what the problem is. those are the only lines that don't get executed and then i reach the error message
Well, let's begin from the very begin. Please, answer the following questions:
1. What is the reason of the error you got?
2. What is recommended to fix this error?
3. Did you implement this recommendation?
4. If you did - then where and how?
-
Re: getting a binding error in server program
the reason for the error is from the 10013 error which is:
An attempt was made to access a socket in a way forbidden by its access permissions. An example is using a broadcast address for "sendto" without broadcast permission being set using setsockopt(SO_BROADCAST).
but my setsockopt is using SO_BROADCAST.
i have moved the setsockopt code above the sendto code
the code executes setsockopt and jumps into the else statement
this time the WSAGetLastError is 0
then it executes sendto and displays the WSAGetLastError
-
Re: getting a binding error in server program
Please, post the actual code!
-
Re: getting a binding error in server program
the only change i made was moving some code around
Code:
void CSocketTestServerDlg::StartServer()
{
//---------------------------------------
// Declare variables
int iResult = 0;
WSADATA wsaData;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
BOOL bOptVal = FALSE;
int bOptLen = sizeof (BOOL);
int iOptVal = 0;
int iOptLen = sizeof (int);
int portno = 1818;
SOCKADDR_IN brdcastaddr;
//---------------------------------------
// Create a listening socket
m_serversocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(m_serversocket == -1)
{
AfxMessageBox("Socket Initialiation Error");
WSACleanup();
}
memset(&brdcastaddr,0, sizeof(brdcastaddr));
brdcastaddr.sin_family = AF_INET;
brdcastaddr.sin_port = htons(portno);
brdcastaddr.sin_addr.s_addr = INADDR_BROADCAST;
int len = sizeof(brdcastaddr);
char sbuf[1024];
int BufLen = 1024;
iResult = setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen);
if (iResult == SOCKET_ERROR)
{
CString text;
text.Format(_T("setsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
AfxMessageBox(text);
}
else
{
//AfxMessageBox("ERROR binding in the server socket");
CString text;
text.Format(_T("ERROR : %d"), WSAGetLastError()); //---error---//
AfxMessageBox(text);
}
int ret = sendto(m_serversocket, sbuf, strlen(sbuf), 0, (sockaddr*)&brdcastaddr, len);
if(ret < 0)
{
CString text;
text.Format(_T("ERROR binding in the server socket: %d"), WSAGetLastError()); //---error---//
AfxMessageBox(text);
exit(1);
}
if(listen(m_serversocket,0) < 0)
{
AfxMessageBox("ERROR listening in the server socket");
exit(1);
}
bOptVal = TRUE;
iResult = getsockopt(m_serversocket, SOL_SOCKET, SO_KEEPALIVE, (char *) &iOptVal, &iOptLen);
if (iResult == SOCKET_ERROR)
{
CString text;
text.Format(_T("getsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
AfxMessageBox(text);
}
else
{
CString text;
text.Format(_T("SO_KEEPALIVE Value: %ld\n"), iOptVal);
AfxMessageBox(text);
}
iResult = getsockopt(m_serversocket, SOL_SOCKET, SO_KEEPALIVE, (char *) &iOptVal, &iOptLen);
if (iResult == SOCKET_ERROR)
{
CString text;
text.Format(_T("getsockopt for SO_KEEPALIVE failed with error: %d"), WSAGetLastError());
AfxMessageBox(text);
}
else
{
CString text;
text.Format(_T("SO_KEEPALIVE Value: %ld\n"), iOptVal);
AfxMessageBox(text);
}
closesocket(m_serversocket);
WSACleanup();
SetTimer(0x01, 100, NULL);
}
-
Re: getting a binding error in server program
What error do you get now?
-
Re: getting a binding error in server program
the code executes setsockopt and jumps into the else statement
this time the WSAGetLastError is 0
then it executes sendto and displays the WSAGetLastError which is still 10013
-
Re: getting a binding error in server program
Quote:
Originally Posted by
beginner91
the only change i made was moving some code around
Code:
void CSocketTestServerDlg::StartServer()
{
//---------------------------------------
// Declare variables
int iResult = 0;
WSADATA wsaData;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
BOOL bOptVal = FALSE;
int bOptLen = sizeof (BOOL);
int iOptVal = 0;
int iOptLen = sizeof (int);
int portno = 1818;
SOCKADDR_IN brdcastaddr;
//---------------------------------------
// Create a listening socket
m_serversocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(m_serversocket == -1)
{
AfxMessageBox("Socket Initialiation Error");
WSACleanup();
}
memset(&brdcastaddr,0, sizeof(brdcastaddr));
brdcastaddr.sin_family = AF_INET;
brdcastaddr.sin_port = htons(portno);
brdcastaddr.sin_addr.s_addr = INADDR_BROADCAST;
int len = sizeof(brdcastaddr);
char sbuf[1024];
int BufLen = 1024;
iResult = setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen);
...
}
Why do you pass FALSE in the bOptVal parameter? :confused:
-
Re: getting a binding error in server program
that code is from MSDN. should it be true?
it is set to false at the start but then down a few lines it is set to true
is this a mistake?
-
Re: getting a binding error in server program
Again:
you must read the documentation to understand what value you need and in which case!
So, if you need to set a SO_BROADCAST option it must be TRUE, if instead you need to remove this option then FALSE.
-
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).