Well, it is based from the MSDN code but with your own bugs implemented in there!
Please, compare!
Printable View
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
From MSDN:I'd recommend you to first test MSDN example without your own changes.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.
Only after it will work - try to change it according to your needs!
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);
}
yes i know what that error means
but i then started adding all the error messages so the problem was never solved. what do i need to do to fix it?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 have done that:
Code:setsockopt(m_serversocket, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen);
where?????
please look at post #19
i edited the code and put in the line //----------- set socket ---------// so it is easy to find
It's you who has to look at your own code! :mad:
Your setsockopt call is commented out!
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
final code:
sorry for the confusionCode: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);
}