Didn't you find some working example of socket server application?
Printable View
Didn't you find some working example of socket server application?
not one that uses broadcast. its very difficult to find. if you could link any examples that would be great
Why do you use INADDR_BROADCAST instead of INADDR_ANY for your server socket?
i was told to use it. its a requirement
The first "requirement" for you is reading the documentation!
Understand?
If you read the documentation about INADDR_BROADCAST you'd know that it is used to send a broadcast (see http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx)
To receive the broadcasts socket uses INADDR_ANY (see the http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx)
sorry those links don't work for me
but i understand. so INADDR_ANY is used in the server and INADDR_BROADCAST is used in the client
ok so i changed it to INADDR_ANY but i'm still getting the same error with listen
i thought i only needed to change this line:
Code:ServerAddr.sin_addr.s_addr = INADDR_ANY;
Note also the listen applies only to sockets that support connections, that is, those of type SOCK_STREAM!
Datagram socket has to use recvfrom function.
so i'm missing the recvform function?
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 = INADDR_ANY;
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);
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);
}
}
}
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);
}
it is difficult to look up a document for every function especially when i don't know the functions needed. do you know of any fully completed examples/source code?