資源簡介
C++簡單局域網聊天工具,
代碼片段和文件信息
#pragma?comment(lib“ws2_32.lib“)
#include?“afx.h“
#include?
#include?“winsock2.h“
#include?“Winnetwk.h“
#pragma?comment(?lib?“mpr.dll“?)
using?namespace?std;
DWORD?WINAPI?threadproc(LPVOID?IpParameter)
{
SOCKET?sock=(SOCKET)IpParameter;
//接收數據
char?recvbuf[1024];
memset(recvbuf‘\0‘1024);
sockaddr_in?fro;
int?frolen=sizeof(fro);
while(1)
{
int?data?=?recvfrom(sockrecvbuf10240(sockaddr?*)&fro&frolen);
if(data==SOCKET_ERROR?)
{
cout<<“recvfrom?failed“< cout< return?-1;
}
cout< cout<<“客戶端發來:“< Sleep(1000);
}
return?0;
}
void?GetNameAndIp()
{
struct???hostent???*host;
struct???in_addr???*ptr;
DWORD???dwScope?=?RESOURCE_CONTEXT;
NETRESOURCE???*NetResource?=?NULL;
HANDLE???hEnum;
WNetOpenEnum(dwScope?NULL?NULL?NULL?&hEnum);
WSADATA???wsaData;
WSAStartup(MAKEWORD(1?1)?&wsaData);
if?(hEnum)
{
DWORD???Count?=?0xFFFFFFFF;
DWORD???BufferSize?=?10240;
LPVOID???Buffer?=?new?char[10240];
WNetEnumResource(hEnum?&Count?Buffer?&BufferSize);
NetResource?=?(NETRESOURCE*)Buffer;
char?szHostName[200];
for?(unsigned?int?i?=?0;?i? {
if?(NetResource->dwUsage?==?RESOURCEUSAGE_CONTAINER?&&?NetResource->dwType?==?RESOURCETYPE_ANY)
{
if?(NetResource->lpRemoteName)
{
CString???strFullName?=?NetResource->lpRemoteName;
if?(0?==?strFullName.Left(2).Compare(_T(“\\\\“)))
strFullName?=?strFullName.Right(strFullName.GetLength()?-?2);
gethostname(szHostName?strlen(szHostName));
USES_CONVERSION;
char?*pchar?=?T2A(strFullName);
host?=?gethostbyname(pchar);
if?(host?==?NULL)???continue;
ptr?=?(struct?in_addr?*)???host->h_addr_list[0];
string?str?=?““;
for?(int?n?=?0;?n<4;?n++)
{
CString?addr;
if?(n?>?0)
{
str?+=?“.“;
}
int?value?=?(unsigned?int)((unsigned?char*)hos
- 上一篇:51單片機實現8個流水燈
- 下一篇:C++數據結構與算法源代碼第四版清華大學出版社
評論
共有 條評論