// tcp_ipDlg.cpp : implementation file
//

#include "stdafx.h"
#include "tcp_ip.h"
#include "tcp_ipDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


void send_tcp(char function,char mask,char val,unsigned int addr);

/////////////////////////////////////////////////////////////////////////////
// CTcp_ipDlg dialog

CTcp_ipDlg::CTcp_ipDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTcp_ipDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTcp_ipDlg)
	m_Status = _T("");
	m_IP_Address = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTcp_ipDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTcp_ipDlg)

	DDX_Text(pDX, IDC_EDIT3, m_IP_Address);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTcp_ipDlg, CDialog)
	//{{AFX_MSG_MAP(CTcp_ipDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK3, On1off)
	ON_BN_CLICKED(IDOK2, On2on)
	ON_BN_CLICKED(IDOK4, On2off)
	ON_BN_CLICKED(IDOK5, On3on)
	ON_BN_CLICKED(IDOK6, On3off)
	ON_BN_CLICKED(IDOK7, OnConnect)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTcp_ipDlg message handlers

BOOL CTcp_ipDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here


	m_IP_Address=("192.168.1.13");
	UpdateData(FALSE);

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTcp_ipDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTcp_ipDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}




void send_tcp(char function,char mask,char val,unsigned int addr)
	
{
	char Buffer[10];

	WSADATA wsaData;	
	struct sockaddr_in server;
	int wsaret=WSAStartup(0x101,&wsaData);
	if(wsaret)	
		return;
	SOCKET conn;
	conn=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(conn==INVALID_SOCKET)
		return;
	server.sin_addr.s_addr=addr; 
	server.sin_family=AF_INET;
	server.sin_port=htons(30704);
	if(connect(conn,(struct sockaddr*)&server,sizeof(server)))
	{
		closesocket(conn);
		return;	
	}

 Buffer[0] = function;
 Buffer[1] = mask;
 Buffer[2] = 0x00;
 Buffer[3] = 0x00;
 Buffer[4] = 0x00;
 Buffer[5] = val;
 Buffer[6] = 0x00;
 Buffer[7] = 0x00;
 Buffer[8] = 0x00;

 send(conn,Buffer,9,0);
	closesocket(conn);
	WSACleanup();
}


	







void CTcp_ipDlg::OnOK() 
{

	UpdateData(TRUE);
	CString servername =m_IP_Address; 
	unsigned int addr=inet_addr(servername);

	send_tcp(0x1b,0x01,0x01, addr);




}



void CTcp_ipDlg::On1off() 
{
	// TODO: Add your control notification handler code here
		UpdateData(TRUE);
	CString servername =m_IP_Address; 
	unsigned int addr=inet_addr(servername);

	send_tcp(0x1b,0x01,0x00, addr);

}

void CTcp_ipDlg::On2on() 
{
	// TODO: Add your control notification handler code here
		UpdateData(TRUE);
	CString servername =m_IP_Address; 
	unsigned int addr=inet_addr(servername);
	
	send_tcp(0x1b,0x02,0x02, addr);

}

void CTcp_ipDlg::On2off() 
{
	// TODO: Add your control notification handler code here
		UpdateData(TRUE);
	CString servername =m_IP_Address; 
	unsigned int addr=inet_addr(servername);

	send_tcp(0x1b,0x02,0x00, addr);

}


void CTcp_ipDlg::On3on() 
{
	// TODO: Add your control notification handler code here
	
	// TODO: Add your control notification handler code here
		UpdateData(TRUE);
	CString servername =m_IP_Address; 
	unsigned int addr=inet_addr(servername);

	send_tcp(0x1b,0x04,0x04, addr);

}

void CTcp_ipDlg::On3off() 
{
	// TODO: Add your control notification handler code here
		UpdateData(TRUE);
	CString servername =m_IP_Address; 
	unsigned int addr=inet_addr(servername);

	send_tcp(0x1b,0x04,0x00, addr);
}

void CTcp_ipDlg::OnConnect() 
{
	// TODO: Add your control notification handler code here

		UpdateData(TRUE);
	CString servername =m_IP_Address; 
	unsigned int addr=inet_addr(servername);
	send_tcp(0x19,0x07,0x07, addr);
	send_tcp(0x1a,0x07,0x00, addr);

}

void CTcp_ipDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}
