// broadcast message SEND.cpp // ルーターは通常超えられません。 // 同期モード WSADATA m_wsaData; BOOL yes = 1; if (WSAStartup(MAKEWORD(1, 1), &m_wsaData) != 0) MessageBox("fail to TCP start\n"); m_s = socket(AF_INET, SOCK_DGRAM, 0); addr.sin_family = AF_INET; addr.sin_port = htons(12345); addr.sin_addr.s_addr = inet_addr("255.255.255.255"); setsockopt(m_s, SOL_SOCKET, SO_BROADCAST, (char *)&yes, sizeof(yes)); // SEND CString ss; GetDlgItemText(IDC_EDIT1,ss); sendto(m_s, ss,ss.GetLength(), 0, (struct sockaddr *)&addr, sizeof(addr)); -------------------------------------------------------------------------------------------------- // broadcast message RECV.cpp void CRecDlg::rec(){ WSAData wsaData; SOCKET sock; struct sockaddr_in addr; char buf[2048]; WSAStartup(MAKEWORD(2,0), &wsaData); sock = socket(AF_INET, SOCK_DGRAM, 0); addr.sin_family = AF_INET; addr.sin_port = htons(12345); addr.sin_addr.S_un.S_addr = INADDR_ANY; bind(sock, (struct sockaddr *)&addr, sizeof(addr)); memset(buf, 0, sizeof(buf)); while (1){ recv(sock, buf, sizeof(buf), 0); if ( buf[0] == '0' ) break; else { AfxMessageBox (buf); memset(buf, 0, sizeof(buf)); } } closesocket(sock); WSACleanup(); }