-
March 19th, 2013, 06:27 AM
#16
Re: getting a binding error in server program
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!
Victor Nijegorodov
-
March 19th, 2013, 06:30 AM
#17
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
Last edited by beginner91; March 19th, 2013 at 06:33 AM.
-
March 19th, 2013, 06:39 AM
#18
Re: getting a binding error in server program
From MSDN:
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!
Victor Nijegorodov
-
March 19th, 2013, 06:39 AM
#19
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);
}
Last edited by beginner91; March 19th, 2013 at 07:05 AM.
-
March 19th, 2013, 06:44 AM
#20
Re: getting a binding error in server program
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!
Victor Nijegorodov
-
March 19th, 2013, 06:49 AM
#21
Re: getting a binding error in server program
yes i know what that error means
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?
-
March 19th, 2013, 07:00 AM
#22
Re: getting a binding error in server program
Originally Posted by beginner91
yes i know what that error means
Then why do you ignore the way to fix it:
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.
Victor Nijegorodov
-
March 19th, 2013, 07:01 AM
#23
Re: getting a binding error in server program
but i have done that:
Code:
setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen);
-
March 19th, 2013, 07:03 AM
#24
Re: getting a binding error in server program
Victor Nijegorodov
-
March 19th, 2013, 07:06 AM
#25
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
-
March 19th, 2013, 07:09 AM
#26
Re: getting a binding error in server program
It's you who has to look at your own code!
Your setsockopt call is commented out!
Victor Nijegorodov
-
March 19th, 2013, 07:11 AM
#27
Re: getting a binding error in server program
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.
Victor Nijegorodov
-
March 19th, 2013, 07:12 AM
#28
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
-
March 19th, 2013, 07:13 AM
#29
Re: getting a binding error in server program
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.
Victor Nijegorodov
-
March 19th, 2013, 07:17 AM
#30
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
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
|