-
March 19th, 2013, 07:24 AM
#31
Re: getting a binding error in server program
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!
Victor Nijegorodov
-
March 19th, 2013, 07:27 AM
#32
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?
-
March 19th, 2013, 07:44 AM
#33
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.
Victor Nijegorodov
-
March 19th, 2013, 07:48 AM
#34
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];
-
March 19th, 2013, 07:53 AM
#35
Re: getting a binding error in server program
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!
Victor Nijegorodov
-
March 19th, 2013, 07:55 AM
#36
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
-
March 19th, 2013, 07:59 AM
#37
Re: getting a binding error in server program
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?
Victor Nijegorodov
-
March 19th, 2013, 08:12 AM
#38
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
-
March 19th, 2013, 08:21 AM
#39
Re: getting a binding error in server program
Please, post the actual code!
Victor Nijegorodov
-
March 19th, 2013, 08:23 AM
#40
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);
}
Last edited by beginner91; March 19th, 2013 at 08:30 AM.
-
March 19th, 2013, 08:28 AM
#41
Re: getting a binding error in server program
What error do you get now?
Victor Nijegorodov
-
March 19th, 2013, 08:30 AM
#42
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
-
March 19th, 2013, 08:32 AM
#43
Re: getting a binding error in server program
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?
Victor Nijegorodov
-
March 19th, 2013, 08:34 AM
#44
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?
Last edited by beginner91; March 19th, 2013 at 08:36 AM.
-
March 19th, 2013, 08:41 AM
#45
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.
Victor Nijegorodov
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|