-
March 19th, 2013, 04:30 AM
#1
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);
}
Last edited by beginner91; March 19th, 2013 at 06:26 AM.
-
March 19th, 2013, 05:12 AM
#2
Re: getting a binding error in server program
From MSDN article "sendto":
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?
Victor Nijegorodov
-
March 19th, 2013, 05:27 AM
#3
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() );
-
March 19th, 2013, 05:31 AM
#4
Re: getting a binding error in server program
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?
Victor Nijegorodov
-
March 19th, 2013, 05:38 AM
#5
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?
-
March 19th, 2013, 05:45 AM
#6
Re: getting a binding error in server program
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);
Victor Nijegorodov
-
March 19th, 2013, 05:48 AM
#7
Re: getting a binding error in server program
thank you for the code. the error code is 10013
-
March 19th, 2013, 05:57 AM
#8
Re: getting a binding error in server program
BTW, You called setsockopt but didn't check its error code. Why?
Victor Nijegorodov
-
March 19th, 2013, 05:54 AM
#9
Re: getting a binding error in server program
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.
Victor Nijegorodov
-
March 19th, 2013, 05:56 AM
#10
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?
-
March 19th, 2013, 06:14 AM
#11
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'
Last edited by beginner91; March 19th, 2013 at 06:25 AM.
-
March 19th, 2013, 06:23 AM
#12
Re: getting a binding error in server program
One more thing: please, use Code tags while posting code snippets! See Announcement: Before you post....
Victor Nijegorodov
-
March 19th, 2013, 06:25 AM
#13
Re: getting a binding error in server program
-
March 19th, 2013, 06:21 AM
#14
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...
Your code differs from the MSDN original one!
Victor Nijegorodov
-
March 19th, 2013, 06:24 AM
#15
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
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
|