UDP组播更改网段搜索设备ip

/***********************************************************************************************
*
*=============================================================================================*/
#pragma comment(lib, "ws2_32.lib")
#include <winsock2.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <time.h>
using namespace std;

const int MAX_BUF_LEN = 255;
#define GET_HOST_COMMAND  "GetIPAddr"
#define CLIENT_PORT 8080		//客户端端口号
#define SERVER_PORT 8080		//服务器端口号


SOCKET BuildBroadcastSocket()
{
	int nPort = SERVER_PORT;

	// 创建socket   
	int err = 0;
	SOCKET sBroardcast;
	sBroardcast = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (INVALID_SOCKET == sBroardcast)
	{
		err = WSAGetLastError();
		printf("\"socket\" error! error code is %d\n", err);
		return -1;
	}

	// 用来绑定套接字   
	SOCKADDR_IN sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(CLIENT_PORT);
	sin.sin_addr.s_addr = 0;

	//设置该套接字为广播类型,   
	bool bOpt = true;
	err = setsockopt(sBroardcast, SOL_SOCKET, SO_BROADCAST, (char*)&bOpt, sizeof(bOpt));
	if (SOCKET_ERROR == err)
	{
		err = WSAGetLastError();
		printf("\"bind\" error! error code is %d\n", err);
		//return -1;
	}

	// 绑定套接字   
	err = bind(sBroardcast, (SOCKADDR*)&sin, sizeof(SOCKADDR));
	if (SOCKET_ERROR == err)
	{
		err = WSAGetLastError();
		printf("\"bind\" error! error code is %d\n", err);
		//return -1;
	}
	return sBroardcast;
}

bool InitSocketContext()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int err = 0;

	// 启动socket api   
	wVersionRequested = MAKEWORD(2, 2);
	err = WSAStartup(wVersionRequested, &wsaData);
	if (err != 0)
	{
		return false;
	}

	if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
	{
		WSACleanup();
		return false;
	}
	return true;
}


int upd_1(int nNetTimeout)
{
	int nPort = SERVER_PORT;
	InitSocketContext();
	//printf("the client is start.\n");
	int nAddrLen = sizeof(SOCKADDR);
	char buff[MAX_BUF_LEN] = "";
	int nLoop = 0;
	char szMsg[] = GET_HOST_COMMAND;
	int nLen = sizeof(SOCKADDR_IN);
	char aaa[] = "{\"intent\":0}";
	//	if (SOCKET_ERROR == sendto(sBroardcast, aaa, strlen(aaa), 0, (sockaddr*)&sin_from, nLen))
	{
		//printf("Send UDP Failed");  
		//return -1;
	}
	//printf("send broadcast data:%s\n", GET_HOST_COMMAND);
	int nSendSize = 0;
	int num = 0;

	int optLen = sizeof(int);
	//	getsockopt(sBroardcast, SOL_SOCKET, SO_RCVTIMEO, (char*)&optVal, &optLen);
	// 用来从网络上的广播地址接收数据   
	SOCKADDR_IN sin_from;
	SOCKADDR_IN sin_to;
	sin_to.sin_family = AF_INET;
	sin_to.sin_port = htons(nPort);
	sin_to.sin_addr.s_addr = INADDR_BROADCAST;
	
	int segment;	//设置网段
	for (segment = 0; segment < 255; segment++)
	{
		for (int switchnum = 1; switchnum <= 2; switchnum++)
		{
			string cmd = "cmd /c netsh interface ip set address \"以太网\" static ";
			string IPAddress;				//ip		
			if (switchnum == 1)
			{
				IPAddress = "192.168." + to_string(segment) + ".150";
			}
			else if (switchnum == 2)
			{
				IPAddress = "192.168." + to_string(segment) + ".151";
			}
			string Mask = "255.255.255.0";												//子网掩码
			string gateway = "192.168." + to_string(segment) + ".1";					//默认网关
			cmd += IPAddress + " " + Mask + " " + gateway;
			system(cmd.c_str());
			Sleep(5000);
			printf("Segment=%d, Switch=%d\n", segment, switchnum);
			SOCKET sBroardcast = BuildBroadcastSocket();
			setsockopt(sBroardcast, SOL_SOCKET, SO_RCVTIMEO, (char*)&nNetTimeout, sizeof(int));
			for (int i = 0; i < 20; i++)
			{
				int nRet = sendto(sBroardcast, aaa, strlen(aaa), 0, (sockaddr*)&sin_to, nLen);
				if (nRet < 0)
				{
					int err = WSAGetLastError();
					//printf("\"recvfrom\" error! error code is %d\n", err);
					break;
				}
				if (0 == nRet)
				{
					break;
				}
				// 接收数据				
				nSendSize = recvfrom(sBroardcast, buff, MAX_BUF_LEN, 0, (SOCKADDR*)&sin_from, &nAddrLen);
				if (SOCKET_ERROR == nSendSize)
				{
					int err = WSAGetLastError();
					//printf("\"recvfrom\" error! error code is %d\n", err);
					break;
				}
				if (0 == nSendSize)
				{
					break;
				}
				buff[nSendSize] = '\0';
				//printf("%s\n", buff);
				Sleep(200);
				if (nSendSize > 20)
				{
					char *num1 = NULL, *num2 = NULL, *num3 = NULL, *num4 = NULL, *num5 = NULL;
					num1 = strtok_s(buff, ",", &num2);
					num2 = strtok_s(num2, ",", &num3);
					num3 = strtok_s(num3, ",", &num4);
					num4 = strtok_s(num3, ":", &num5);
					printf("\nFind Device IP:%s\n", num5);
					return 0;
				}
				num++;
			}
			closesocket(sBroardcast);
		}
	}
	//	sBroardcast = -1;
	return 0;
}

int main(int argc, char **argv)
{
	if (argc < 2)
	{
		printf("这是一个搜索相机IP的工具:\n");
		printf("\n");
		printf("\t %s 500 \n", argv[0]);
		printf("\n");
		printf("上述命令将阻塞时间设置为500毫秒搜索相机IP地址\n");
		return -1;
	}
	//for (int i = 0; i < argc; i++)
	//{
	//	printf(argv[i]);
	//	printf("\n");
	//}
	int nNetTimeout = atoi(argv[1]);
	upd_1(nNetTimeout);
	WSACleanup();
	string cmd = "cmd /c netsh interface ip set address name = \"以太网\" source = dhcp";
	system(cmd.c_str());
	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值