1 |
// ClientDlg.cpp : implementation file
|
2 |
//
|
3 |
|
4 |
#include "stdafx.h"
|
5 |
#include "Client.h"
|
6 |
#include "ClientDlg.h"
|
7 |
#include ".\clientdlg.h"
|
8 |
|
9 |
#ifdef _DEBUG
|
10 |
#define new DEBUG_NEW
|
11 |
#endif
|
12 |
|
13 |
|
14 |
CString Ip, Delay;
|
15 |
unsigned long Port;
|
16 |
UINT Status( LPVOID Param );
|
17 |
void Start();
|
18 |
void startthread();
|
19 |
CButton m_led1;
|
20 |
CButton m_led2;
|
21 |
CButton m_led3;
|
22 |
|
23 |
// CAboutDlg dialog used for App About
|
24 |
|
25 |
class CAboutDlg : public CDialog
|
26 |
{
|
27 |
public:
|
28 |
CAboutDlg();
|
29 |
|
30 |
// Dialog Data
|
31 |
enum { IDD = IDD_ABOUTBOX };
|
32 |
|
33 |
protected:
|
34 |
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
|
35 |
|
36 |
// Implementation
|
37 |
protected:
|
38 |
DECLARE_MESSAGE_MAP()
|
39 |
};
|
40 |
|
41 |
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
|
42 |
{
|
43 |
}
|
44 |
|
45 |
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
|
46 |
{
|
47 |
CDialog::DoDataExchange(pDX);
|
48 |
}
|
49 |
|
50 |
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
|
51 |
END_MESSAGE_MAP()
|
52 |
|
53 |
|
54 |
// CClientDlg dialog
|
55 |
|
56 |
|
57 |
CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
|
58 |
: CDialog(CClientDlg::IDD, pParent)
|
59 |
{
|
60 |
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
|
61 |
}
|
62 |
|
63 |
void CClientDlg::DoDataExchange(CDataExchange* pDX)
|
64 |
{
|
65 |
CDialog::DoDataExchange(pDX);
|
66 |
DDX_Control(pDX, IDC_LED1, m_led1);
|
67 |
DDX_Control(pDX, IDC_LED2, m_led2);
|
68 |
DDX_Control(pDX, IDC_LED3, m_led3);
|
69 |
DDX_Control(pDX, IDC_KNAP1, m_knap1);
|
70 |
DDX_Control(pDX, IDC_KNAP2, m_knap2);
|
71 |
DDX_Control(pDX, IDC_TEMP, m_temp);
|
72 |
DDX_Control(pDX, IDC_POT, m_pot);
|
73 |
DDX_Control(pDX, IDC_IPADDRESS1, m_ip);
|
74 |
DDX_Control(pDX, IDC_PORT, m_port);
|
75 |
DDX_Control(pDX, IDC_DELAY, m_delay);
|
76 |
}
|
77 |
|
78 |
BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
|
79 |
ON_WM_SYSCOMMAND()
|
80 |
ON_WM_PAINT()
|
81 |
ON_WM_QUERYDRAGICON()
|
82 |
//}}AFX_MSG_MAP
|
83 |
ON_BN_CLICKED(IDC_IPPORT, OnBnClickedIpport)
|
84 |
ON_BN_CLICKED(IDC_Connect, OnBnClickedConnect)
|
85 |
END_MESSAGE_MAP()
|
86 |
|
87 |
|
88 |
// CClientDlg message handlers
|
89 |
/*Client socket class*/
|
90 |
class ClientSocket : public CSocket{
|
91 |
public:
|
92 |
ClientSocket() { };
|
93 |
virtual void OnReceive(int nErrorCode);
|
94 |
virtual void OnSend(int nErrorCode);
|
95 |
};
|
96 |
BOOL CClientDlg::OnInitDialog()
|
97 |
{
|
98 |
CDialog::OnInitDialog();
|
99 |
|
100 |
// Add "About..." menu item to system menu.
|
101 |
|
102 |
// IDM_ABOUTBOX must be in the system command range.
|
103 |
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
|
104 |
ASSERT(IDM_ABOUTBOX < 0xF000);
|
105 |
|
106 |
CMenu* pSysMenu = GetSystemMenu(FALSE);
|
107 |
if (pSysMenu != NULL)
|
108 |
{
|
109 |
CString strAboutMenu;
|
110 |
strAboutMenu.LoadString(IDS_ABOUTBOX);
|
111 |
if (!strAboutMenu.IsEmpty())
|
112 |
{
|
113 |
pSysMenu->AppendMenu(MF_SEPARATOR);
|
114 |
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
|
115 |
}
|
116 |
}
|
117 |
|
118 |
// Set the icon for this dialog. The framework does this automatically
|
119 |
// when the application's main window is not a dialog
|
120 |
SetIcon(m_hIcon, TRUE); // Set big icon
|
121 |
SetIcon(m_hIcon, FALSE); // Set small icon
|
122 |
|
123 |
// TODO: Add extra initialization here
|
124 |
|
125 |
// Get data from regedit
|
126 |
CString strip, strport;
|
127 |
char chaip[16];
|
128 |
char chadelay[16];
|
129 |
CRegKey reg;
|
130 |
unsigned long size = 16;
|
131 |
LONG res=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ);
|
132 |
|
133 |
if (res == ERROR_SUCCESS)
|
134 |
{
|
135 |
res=reg.QueryStringValue("IP", chaip, &size);
|
136 |
}
|
137 |
Ip = chaip;
|
138 |
m_ip.SetWindowText(Ip);
|
139 |
LONG res2=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ);
|
140 |
|
141 |
if (res2 == ERROR_SUCCESS)
|
142 |
{
|
143 |
res2=reg.QueryDWORDValue("Port", Port);
|
144 |
}
|
145 |
strport.Format("%d",Port);
|
146 |
m_port.SetWindowText(strport);
|
147 |
|
148 |
LONG res3=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ);
|
149 |
|
150 |
if (res3 == ERROR_SUCCESS)
|
151 |
{
|
152 |
res3=reg.QueryStringValue("Delay",chadelay,&size);
|
153 |
}
|
154 |
Delay = chadelay;
|
155 |
m_delay.SetWindowText(Delay);
|
156 |
reg.Close();
|
157 |
|
158 |
//connect til server
|
159 |
Start();
|
160 |
|
161 |
|
162 |
return TRUE; // return TRUE unless you set the focus to a control
|
163 |
}
|
164 |
|
165 |
void CClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
|
166 |
{
|
167 |
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
|
168 |
{
|
169 |
CAboutDlg dlgAbout;
|
170 |
dlgAbout.DoModal();
|
171 |
}
|
172 |
else
|
173 |
{
|
174 |
CDialog::OnSysCommand(nID, lParam);
|
175 |
}
|
176 |
}
|
177 |
|
178 |
// If you add a minimize button to your dialog, you will need the code below
|
179 |
// to draw the icon. For MFC applications using the document/view model,
|
180 |
// this is automatically done for you by the framework.
|
181 |
|
182 |
void CClientDlg::OnPaint()
|
183 |
{
|
184 |
if (IsIconic())
|
185 |
{
|
186 |
CPaintDC dc(this); // device context for painting
|
187 |
|
188 |
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
|
189 |
|
190 |
// Center icon in client rectangle
|
191 |
int cxIcon = GetSystemMetrics(SM_CXICON);
|
192 |
int cyIcon = GetSystemMetrics(SM_CYICON);
|
193 |
CRect rect;
|
194 |
GetClientRect(&rect);
|
195 |
int x = (rect.Width() - cxIcon + 1) / 2;
|
196 |
int y = (rect.Height() - cyIcon + 1) / 2;
|
197 |
|
198 |
// Draw the icon
|
199 |
dc.DrawIcon(x, y, m_hIcon);
|
200 |
}
|
201 |
else
|
202 |
{
|
203 |
CDialog::OnPaint();
|
204 |
}
|
205 |
}
|
206 |
|
207 |
// The system calls this function to obtain the cursor to display while the user drags
|
208 |
// the minimized window.
|
209 |
HCURSOR CClientDlg::OnQueryDragIcon()
|
210 |
{
|
211 |
return static_cast<HCURSOR>(m_hIcon);
|
212 |
}
|
213 |
|
214 |
|
215 |
ClientSocket sockClient;
|
216 |
CString strIp, strPort;
|
217 |
|
218 |
/*Clientsocket class used for client socket connections*/
|
219 |
void ClientSocket::OnReceive( int nErrorCode )
|
220 |
{
|
221 |
char buffer[4096];
|
222 |
int size;
|
223 |
CString buff;
|
224 |
|
225 |
size = Receive(buffer,4095);
|
226 |
buffer[size] = 0;
|
227 |
MessageBox(0,buffer,0,MB_OK);
|
228 |
buff = buffer;
|
229 |
if( buff == "Tændt" )
|
230 |
{
|
231 |
char test[10];
|
232 |
int i;
|
233 |
i = m_led1.GetCheck();
|
234 |
// _itot(i,test,10); Fejltjeck
|
235 |
// MessageBox(0,test,0,MB_OK);
|
236 |
if(i == 0)
|
237 |
{
|
238 |
m_led1.SetCheck(1);
|
239 |
}
|
240 |
else
|
241 |
{
|
242 |
m_led1.SetCheck(0);
|
243 |
}
|
244 |
}
|
245 |
else
|
246 |
{
|
247 |
MessageBox(0,"FEJL i onrecive",0,MB_OK);
|
248 |
}
|
249 |
Close();
|
250 |
}
|
251 |
void ClientSocket::OnSend( int nErrorCode )
|
252 |
{
|
253 |
// CString sendnu; skal ikke sende nu
|
254 |
|
255 |
// sendnu = Sendtekst; skal ikke sende nu
|
256 |
|
257 |
// Send(sendnu,sendnu.GetLength()); skal ikke sende nu
|
258 |
}
|
259 |
void CClientDlg::OnBnClickedIpport()
|
260 |
{
|
261 |
//Set new port and ip for the server in regedit
|
262 |
m_ip.GetWindowText(strIp);
|
263 |
m_port.GetWindowText(strPort);
|
264 |
m_delay.GetWindowText(Delay);
|
265 |
int test;
|
266 |
test = atoi(Delay);
|
267 |
if(test < 10000 && test > 100)
|
268 |
{
|
269 |
CRegKey reg;
|
270 |
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
|
271 |
reg.SetStringValue("","Project-data");
|
272 |
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
|
273 |
reg.SetStringValue("IP",strIp);
|
274 |
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
|
275 |
reg.SetDWORDValue("Port",atoi(strPort));
|
276 |
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
|
277 |
reg.SetStringValue("Delay",Delay);
|
278 |
reg.Close();
|
279 |
}
|
280 |
else
|
281 |
{
|
282 |
MessageBox("fejl i delay",0,MB_OK);
|
283 |
}
|
284 |
}
|
285 |
|
286 |
void Start()
|
287 |
{
|
288 |
sockClient.Create();
|
289 |
if (sockClient.Connect(Ip,Port))
|
290 |
{
|
291 |
//sockClient.Send(strPort,strPort.GetLength(),NULL);
|
292 |
//sockClient.Send(data,data.GetLength(),NULL);
|
293 |
//sockClient.Close();
|
294 |
}
|
295 |
else
|
296 |
{
|
297 |
int error = GetLastError();
|
298 |
CString tmp;
|
299 |
tmp.Format("%d", error);
|
300 |
}
|
301 |
}
|
302 |
void CClientDlg::OnBnClickedConnect()
|
303 |
{
|
304 |
|
305 |
}
|
306 |
UINT Status( LPVOID Param )
|
307 |
{
|
308 |
CString status;
|
309 |
status = "RB1";
|
310 |
Sleep(atoi(Delay));
|
311 |
sockClient.Send(status,status.GetLength(),NULL);
|
312 |
startthread();
|
313 |
return TRUE;
|
314 |
}
|
315 |
void startthread()
|
316 |
{
|
317 |
AfxBeginThread(Status,0,THREAD_PRIORITY_NORMAL,0,0,NULL);
|
318 |
} |