how to change program to broadcast?
Mar 14, 2013 at 2:02pm UTC
i created a client server program in mfc where the server shows all the ip addresses of the clients in a listbox. the client just clicks a button to connect to the server.
but now i want to change the code to broadcasting but i'm not sure how.
in the client i told it what ip address to connect to but with broadcasting i don't have to do that.
so can someone help me change the code to broadcasting?
server code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
void CSocketTestServerDlg::StartServer()
{
SOCKADDR_IN serveraddr;
int portno = 1818;
memset(&serveraddr,0, sizeof (serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(portno);
serveraddr.sin_addr.s_addr = INADDR_ANY;
m_serversocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_serversocket == -1)
{
AfxMessageBox("Socket Initialiation Error" );
}
if (bind(m_serversocket, (SOCKADDR*)&serveraddr,sizeof (SOCKADDR_IN)) < 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);
}
static void f(void *p)
{
CSocketTestServerDlg *pDlg = reinterpret_cast <CSocketTestServerDlg*>(p);
pDlg->ProcessClientRequest();
}
void CSocketTestServerDlg::ProcessClientRequest()
{
SOCKADDR_IN clientaddr;
struct hostent *hostentry;
int len = sizeof (clientaddr);
SOCKET clientsocket = accept(m_serversocket, (sockaddr*)&clientaddr, &len);
if (len == -1)
{
AfxMessageBox("Error accpeting the client socket" );
}
else
{
char *p = inet_ntoa(clientaddr.sin_addr);
int portno = ntohs(clientaddr.sin_port);
// int inet_pton(int af, const char *restrict src, void *restrict dst);
CString str(p);
CListBox listbox;
listbox.AddString(str);
CString strRecvData;
c_MyListBox.AddString(CString(p));
m_bRefershData = true ;
closesocket(clientsocket);
}
}
client code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
void CSocketTestClientDlg::OnBnClickedOk() //connect button
{
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == -1)
{
AfxMessageBox("Socket Initialiation Error" );
}
SOCKADDR_IN serveraddr;
struct hostent *hostentry;
bool bSent = false ;
std::string server = "10.13.32.132" ;
int portno = 1818;
hostentry = gethostbyname(server.c_str());
char *pipaddr = inet_ntoa (*(struct in_addr *)*hostentry->h_addr_list);
memset(&serveraddr,0, sizeof (serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(portno);
serveraddr.sin_addr.s_addr = inet_addr(pipaddr);
//serv_addr.sa_data = htons(portno);
if (connect(s,(SOCKADDR*)&serveraddr,sizeof (SOCKADDR_IN)) < 0)
{
CString msg = "ERROR connecting to the server: " ;
msg += _com_error(HRESULT_FROM_WIN32(WSAGetLastError())).ErrorMessage();
AfxMessageBox(msg);
exit(1);
}
UpdateData(TRUE);
::closesocket(s);
}
Last edited on Mar 15, 2013 at 1:04pm UTC
Mar 15, 2013 at 9:29am UTC
can someone please help with this?
Mar 15, 2013 at 5:19pm UTC
Broadcasting with TCP sockets is not possible.
You'll first have to change the communication to UDP sockets:
socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
-> socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
The socket mechanisms are slightly different, e.g. you'll have no underlying confirmation of received packages.
For sending you may use then INADDR_BROADCAST as target:
1 2
targetaddress.sin_addr.S_un.S_addr = INADDR_BROADCAST;
sendto(udp_targetsocket,(const char *)buffer,msgSize,0,(const sockaddr*)&targetaddress,(int )sizeof (targetaddress));
Topic archived. No new replies allowed.