-
March 20th, 2013, 06:11 AM
#61
Re: getting a binding error in server program
i thought i only needed to change this line:
Code:
ServerAddr.sin_addr.s_addr = INADDR_ANY;
-
March 20th, 2013, 06:18 AM
#62
Re: getting a binding error in server program
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.
Victor Nijegorodov
-
March 20th, 2013, 06:23 AM
#63
Re: getting a binding error in server program
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);
}
-
March 20th, 2013, 06:29 AM
#64
Re: getting a binding error in server program
Originally Posted by beginner91
so i'm missing the recvform function?
Please, read the documentation about socket programming! Use MSDN and Google to find the examples!
Victor Nijegorodov
-
March 20th, 2013, 06:31 AM
#65
Re: getting a binding error in server program
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?
-
March 20th, 2013, 06:40 AM
#66
Re: getting a binding error in server program
Originally Posted by beginner91
it is difficult to look up a document for every function especially when i don't know the functions needed.
Yes, it is.
But you must if, of cousre, you want to become a programmer!
Originally Posted by beginner91
do you know of any fully completed examples/source code?
No, because I never used datagram sockets, only TCP.
But I'm sure Google knows! Just ask him!
Victor Nijegorodov
-
March 21st, 2013, 02:20 AM
#67
Re: getting a binding error in server program
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
|