"", "", "", " , , , ", "", "", " " , .
. . . , , .
, . , 1. . , . , , . , , , .
, . , , . . , . . 0.5 .
:
1.1
1.1.1
1.1.1.1
, . . , . , , .
(, , , , , ) , . . .
, , , , . , . . . , , , 3.2. . -. , 3.1 - .
|
|
, , , 4.2, 1 . , : 4.2, 2, 4.2, 3 ..
, . , . , . . . :
2.1- .
.
, , . , . , 2.1
, , . 0.5 . . , , (3.1). . :
X:=z+(b-c)*7.3; (3.1)
, . . .
. . , -. , . , - . :
- :
1) ;
2) ;
- :
1) ;
2)
.
, . , 4.3 .
, . , 2.7 .
, . , , [3] [7 12]
|
|
. Ż , , I, , , , , , . .
, .
.
, , , , , , , .1.3.3.1.
, , , , , .3, .3, (.2).
, .
, .
, , .
1. . . 3-. .: , 2011
2. ., . Microsoft Windows: Windows Server 2003, Windows XP Windows 2000. - .: , 2008
3. .., . . : .- : - . , 2006. 608..
4. . . Windows. . 2-, . . .: , 2004.-480.: .
5. . Windows : Win32 64- Windows/, - 4- . - ; ; .: - " ", 2001. - 752 .;
6. . . Win32 API. . - 2007-572.
7. . Windows 95. : BHV , 1997.
8. .., .. . .: , 2009
9. .., .., .. .- .: . , 2009
-6.01
ò
ղ ֲ Ͳ
(. Ѻ)
_______ _______
( )
()
________________ _________________
( )
:___ 㳺 볺-.
____________________________________________
() _____ __ 3 _ __ ʲ-11 __
_____ 6.050102 ' __________
( )
_________________________________________________________
( )
___________________________ ___________
( ) ()
____________________________ ____________
(, , , ) ()
:
_______
ʳ : __________
: ECTS ___________
:
_________________________ ________
( ) ()
________________________ ________
( ) ()
_________________________ ________
|
|
( ) ()
_________
Ѻ - 20 __
: 31 ., 5 ., 4 ., 18 .
- .
: Win32 .
- .
Win32 Microsoft Visual Studio, .
, -; .
. .
(. ).
WIN32 API, , , , , , , .
-
//
#include "stdafx.h"
#include "MapServer.h"
#include <stdio.h>
#include <process.h>
#include <wchar.h>
#include <sstream>
#define MAX_LOADSTRING 100
#define BUF_SIZE 1024
LPCSTR nameMutex = "Global\\MapMutex";
LPTSTR nameEvent = L"Global\\MapEvent";
HANDLE hServerMutex; //
unsigned uThreadIDs[2];
HANDLE hThreads[2];
BOOL Working=true;
HANDLE hMapFile;
static HWND hwndEdit;
struct D{
TCHAR Buf[1024];
};
D* lpD;
//--------------------------------------------------
unsigned __stdcall VvodThreadFunc(void * arg) //
{
while (Working)
{
WaitForSingleObject(hEventVvod,INFINITE); //
WaitForSingleObject(hServerMutex,INFINITE); //
// ,
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)lpD->Buf);
ReleaseMutex(hServerMutex); //
}
_endthreadex(0);
return 0;
};
//---------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR ss[80];
switch (message)
{
case WM_CREATE:
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, //
NULL, //
PAGE_READWRITE, // /
0, // .
BUF_SIZE, //
"myFileMapping"); //
if (hMapFile == NULL || hMapFile == INVALID_HANDLE_VALUE)
{
MessageBox(hWnd,
" (%d).\n",
"Map", MB_OK);
return 0;
}
lpD= (D*) MapViewOfFile(hMapFile, // .
FILE_MAP_ALL_ACCESS, // /
0, 0, BUF_SIZE);
if (lpD == NULL)
{
MessageBox(hWnd,
" (%d).\n",
"Map",MB_OK);
return 0;
}
hServerMutex = CreateMutexA(NULL, true, nameMutex);
if (hServerMutex == NULL) MessageBox(hWnd, "Error mutex", "Map",MB_OK);
// e ,
|
|
hEventVvod = CreateEvent(NULL, FALSE, TRUE, nameEvent);
hwndEdit = CreateWindow(TEXT("EDIT"), NULL,
WS_CHILD | WS_VISIBLE | WS_VSCROLL |
ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
10, 40, 500, 400, hWnd, NULL, hInst, NULL);
//
hThreads[0] = (HANDLE)_beginthreadex(NULL, 0, &VvodThreadFunc, 0, 0,&uThreadIDs[0]);
ReleaseMutex(hServerMutex);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CloseHandle(hThreads[0]);
UnmapViewOfFile(lpD);
CloseHandle(hMapFile);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
-
#include "stdafx.h"
#include "MapClient1.h"
#include <stdio.h>
#define MAX_LOADSTRING 100
#define BUF_SIZE 1024
HANDLE hMapFile;
struct D{
TCHAR Buf[1024];
};
D* lpD;
bool Podkluchen=false;
HINSTANCE hInst;
TCHAR szTitle[MAX_LOADSTRING];
TCHAR szWindowClass[MAX_LOADSTRING];
LPCSTR nameMutex = "Global\\MapMutex";
LPTSTR nameEvent = L"Global\\MapEvent";
HANDLE hServerMutex; //
HANDLE hEventVvod; //
//----------------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case ID_32773: //
if(Podkluchen) return 0;
hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // /
FALSE, //
"myFileMapping"); // " "
if (hMapFile == NULL)
{
MessageBox(hWnd,
" ",
"",MB_OK);
Podkluchen=false;
return 0;
}
lpD= (D*) MapViewOfFile(hMapFile, // "."
FILE_MAP_ALL_ACCESS, // /
0, 0, BUF_SIZE);
if (lpD == NULL)
{
MessageBox(hWnd,
" (%d).\n",
"",MB_OK);
Podkluchen=false;
return 0;
}
hServerMutex = CreateMutexA(NULL, true, nameMutex);
if (hServerMutex == NULL) MessageBox(hWnd, "Error mutex",
"Map",MB_OK);
// . e ,
hEventVvod = OpenEvent(EVENT_ALL_ACCESS, FALSE, nameEvent);
Podkluchen=true;
break;
case ID_32772://
if(Podkluchen)
{
FillMemory(lpD->Buf,1024,0); //
D.Buf = ". ";
SetEvent(hEventVvod);
ReleaseMutex(hServerMutex);
}else
MessageBox(hWnd,
" \n", "1", MB_OK);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
UnmapViewOfFile(lpD);
CloseHandle(hMapFile);
ReleaseMutex(hServerMutex);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
-
#include <stdio.h>
#define MAX_LOADSTRING 100
#define IDT_TIMER1 37000
// Global Variables:
HINSTANCE hInst;
TCHAR szTitle[MAX_LOADSTRING];
TCHAR szWindowClass[MAX_LOADSTRING];
static HWND hwndEdit;
//----------------------------------
HANDLE Mail;
int flag = 0;
UINT idTimer;
BOOL Code; //
DWORD Msg; // .
|
|
DWORD Number; // Mailslot
char szBuf[512];
char temp[10];
LPSTR MailslotName = "\\\\.\\mailslot\\$Box_1$";
DWORD cbRead; // ,
TCHAR mess[2048];
TCHAR* m_mess = mess;
//--------------------------------------------------------------------------------------------
void Create (LPSTR NameMailslot)
{
sprintf(m_mess, "Mailslot \r\n");
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
// Mailslot, NameMailslot, Mail .
Mail = CreateMailslot(NameMailslot, 0, MAILSLOT_WAIT_FOREVER, NULL);
// ,
if(Mail == INVALID_HANDLE_VALUE)
{
sprintf(m_mess, "%s \r\n . Error: %d \r\n ",m_mess,GetLastError());
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
return;
}
//
sprintf(m_mess, "%s\r\n OK\r\n",m_mess);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
//
flag=1;
return;
}
//--------------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CREATE:
hwndEdit = CreateWindow(// .
TEXT("EDIT"), NULL,
WS_CHILD | WS_VISIBLE | WS_VSCROLL |
ES_READONLY | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
0, 0, 400, 200, hWnd, NULL, hInst, NULL);
break;
case WM_TIMER:
// ,
if(flag!=1)Create(MailslotName);
// Mailslot
Code = GetMailslotInfo(Mail, NULL, &Msg, &Number, NULL);
if(!Code)
{
sprintf(m_mess, "%s \r\n . Error: %d \r\n", m_mess, GetLastError());
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
return 0;
}
// Mailslot ,
if(Number!= 0)
{
if(ReadFile(Mail, szBuf, 512, &cbRead, NULL))
{
//
sprintf(m_mess, "%s \r\n : \r\n %s \r\n ",m_mess,szBuf);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
}
else
{
sprintf(m_mess, "%s \r\n : Error %d \r\n ", m_mess, GetLastError());
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
return 0;
}
}
return 0;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case ID_32771: //
if(flag ==0)
{
idTimer = SetTimer(hWnd,IDT_TIMER1, 1000, NULL);
FillMemory(m_mess,2048,0);
}
break;
case ID_32772:
sprintf(m_mess, "%s\r\n OK\r\n",m_mess);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
flag=0;
KillTimer(hWnd,IDT_TIMER1);
CloseHandle(Mail);
break;
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
if(flag!=0)
{
KillTimer(hWnd,IDT_TIMER1);
CloseHandle(Mail);
}
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
-
#include <stdio.h>
#define MAX_LOADSTRING 100
//----------------------------------
HANDLE Mail;
int flag = 0;
UINT idTimer;
BOOL Code; //
DWORD Msg; // .
DWORD Number; // Mailslot
char szBuf[512];
char temp[10];
LPSTR ServerName = "\\\\.\\mailslot\\$Box_1$";
DWORD cbRead; // ,
TCHAR mess[2048];
TCHAR* m_mess = mess;
//------------------------------------------------------------------------------------------------------
void Connect()
{
//
Mail = CreateFile(ServerName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
// ,
if(Mail == INVALID_HANDLE_VALUE)
{
sprintf(m_mess, "CreateMailslot Error: %d \r\n", GetLastError());
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
return;
}
flag=1;
return;
}
//----------------------------------------------------------------------------------------------
void Send(HWND hWnd)
{
//
DWORD cbWritten = SendMessage(hwndEdit,WM_GETTEXTLENGTH,0,0);
if(flag!=1)
{
MessageBox(hWnd, " ...","MailClient 1",MB_OK);
Connect (); //
};
//
SendMessage(hwndEdit, WM_GETTEXT, (WPARAM) cbWritten, (LPARAM) szBuf);
//
WriteFile(Mail, szBuf, strlen(szBuf) + 1,&cbWritten, NULL);
return;
}
//----------------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam)
{ int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CREATE:
hwndEdit = CreateWindow(// .
TEXT("EDIT"), TEXT(". \r\n"),
WS_CHILD | WS_VISIBLE | WS_VSCROLL |
ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
20, 10, 400, 200, hWnd, NULL, hInst, NULL);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// ---------------:
switch (wmId)
{
case IDM_32771: //
Connect();
break;
case IDM_37772: //
Send(hWnd);
break;
case ID_32776: //
sprintf(m_mess, " %d \r\n", vers);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
break;
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CloseHandle(Mail);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
-
#include "stdafx.h"
#include "PipeServer.h"
#include <process.h>
#include <stdio.h>
#define MAX_LOADSTRING 100
BOOL fConnected[2]={false,false}; //
BOOL stopwork = false; //
HANDLE hNamedPipe[2]; //
LPSTR lpszPipeName = "\\\\.\\pipe\\my_pipe"; //
char szBuf[512]; //
DWORD cbRead; // -
int flag = 0;
static HWND hwndEdit; //
TCHAR mess[2048]; //
TCHAR* m_mess = mess; //
int NP=-1; //-
unsigned uThreadIDs[2]; //
HANDLE hThreads[2]; //
//------------------- ------------------------------------------------------------
unsigned __stdcall PipeThreadFunc(void * arg) // ,
{
if(stopwork){_endthreadex(0);return 0;} // ,
DWORD err=0; //
DWORD nom = GetCurrentThreadId(); // .
// , .
int j=0;
if(uThreadIDs[0]==nom)j=0;
if(uThreadIDs[1]==nom)j=1;
if(fConnected[j])goto read_mes; //
// ,
hNamedPipe[j]=CreateNamedPipe(lpszPipeName, //
PIPE_ACCESS_DUPLEX, //
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES, //. : ,
//, - .
512, 512, //
5000, // = 5
NULL); //
if(hNamedPipe[j]==INVALID_HANDLE_VALUE)
{ // :
err=GetLastError();
sprintf(m_mess, "%s \r\n . Error: %d \r\n ",m_mess,err);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
_endthreadex(0);
return err;
}
// -----------
sprintf(m_mess, "%s \r\n %d. ... \r\n ",m_mess,j+1);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
// --------------------
fConnected[j] = ConnectNamedPipe(hNamedPipe[j], NULL);
if(!fConnected[j])
{
err=GetLastError();
CloseHandle(hNamedPipe[j]);
sprintf(m_mess, "%s \r\n . Error: %d \r\n ",m_mess,err);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
_endthreadex(0);
return err;
}
read_mes: // ================================
while(!stopwork) //
{
if(ReadFile(hNamedPipe[j], szBuf, 512, &cbRead, NULL))
{ //
sprintf(m_mess, "%s \r\n %d. = %d;\r\n %s \r\n ",
m_mess, j+1, cbRead, szBuf);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
}
else
{ //
sprintf(m_mess, "%s \r\n %d. . Error: %d \r\n ",
m_mess, j+1, err);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
}
}
CloseHandle(hNamedPipe[j]);
_endthreadex(0);
return 0;
};
//-------------------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
DWORD nowait = PIPE_NOWAIT;
switch (message)
{
case WM_CREATE:
hwndEdit = CreateWindow(// .
TEXT("EDIT"), TEXT(". \r\n"),
WS_CHILD | WS_VISIBLE | WS_VSCROLL |
ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
20, 10, 400, 200, hWnd, NULL, hInst, NULL);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case ID_32771: //
NP++; // -
if(NP>1) // - <=2
{
NP=1;
sprintf(m_mess, "%s \r\n Error. - >2!!!\r\n ",m_mess);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
return 0;
}
// .
//
hThreads[NP] = (HANDLE)_beginthreadex(NULL, 0, &PipeThreadFunc, 0, 0,
&uThreadIDs[NP]);
sprintf(m_mess, "%s \r\n %d\r\n ",m_mess,NP+1);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
break;
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
stopwork=true; //
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CloseHandle(hThreads[0]); //
CloseHandle(hThreads[1]);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
-
#include <stdio.h>
#define MAX_LOADSTRING 100
BOOL fConnected=false; //
HANDLE hNamedPipe; //
LPSTR lpszPipeName = "\\\\.\\pipe\\my_pipe"; //
char szBuf[256]; //
DWORD cbWritten; // -
static HWND hwndEdit; //
TCHAR mess[2048]; //
TCHAR* m_mess = mess; //
// Global Variables:
HINSTANCE hInst;
TCHAR szTitle[MAX_LOADSTRING];
TCHAR szWindowClass[MAX_LOADSTRING];
//----------------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
DWORD err=0;
switch (message)
{
case WM_CREATE:
hwndEdit = CreateWindow(// .
TEXT("EDIT"), TEXT(". \r\n"),
WS_CHILD | WS_VISIBLE | WS_VSCROLL |
ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
20, 10, 400, 200, hWnd, NULL, hInst, NULL);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case ID_32771: //
if(fConnected)return 0;
hNamedPipe=CreateFile(lpszPipeName,
GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL);
if(hNamedPipe == INVALID_HANDLE_VALUE)
{
err=GetLastError();
sprintf(m_mess, "%s \r\n . Error: %d \r\n ",m_mess,err);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
return 0;
}
sprintf(m_mess, "%s \r\n .\r\n ",m_mess);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
fConnected=true;
break;
case ID_32772: //
if(!fConnected) //
{
sprintf(m_mess, "%s \r\n . \r\n ",m_mess);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
return 0;
}
//
cbWritten = SendMessage(hwndEdit, WM_GETTEXTLENGTH, 0, 0);
//
SendMessage(hwndEdit, WM_GETTEXT, (WPARAM) cbWritten, (LPARAM) szBuf);
// --------------------------------------
if(WriteFile(hNamedPipe, szBuf, strlen(szBuf)+1, &cbWritten, NULL))
{
sprintf(m_mess, "\r\n \r\n %s",szBuf);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
}
else
{
sprintf(m_mess, "%s \r\n Error: %d \r\n ",m_mess,err);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
}
break;
case ID_32773: //
Task1(); //, . .
// hwndEdit
break;
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CloseHandle(hNamedPipe); //
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// SocketServer.cpp
//
#include "stdafx.h"
#include "SocketServer.h"
#include <windows.h>
#include <windowsx.h>
#include <winsock.h>
#include <commctrl.h>
#include <stdlib.h>
#include <stdio.h>
#define MAX_LOADSTRING 100
char szBuf[512];
DWORD cbWritten;
static HWND hwndEdit;
TCHAR mess[2048];
TCHAR* m_mess = mess;
#define SERV_PORT 5000 //
#define WSA_ACCEPT (WM_USER+0)
#define WSA_NETEVENT (WM_USER+1)
WSADATA wsaData; // . Windows Sockets
WORD wVersionRequested = MAKEWORD(1, 1); // Windows Sockets
int err=0;
SOCKET srv_socket=INVALID_SOCKET; //
int ClientNum = -1; //
SOCKET sock[2]; //
SOCKADDR_IN sockaddr[2]; //
//=========================================
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
HWND hWindow;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_SOCKETSERVER, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{ return FALSE; }
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SOCKETSERVER));
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SOCKETSERVER));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SOCKETSERVER);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, 400, 300, NULL, NULL, hInstance, NULL);
if (!hWnd) return FALSE;
hWindow=hWnd;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
void ServerStart(HWND hWnd)
{
if(srv_socket!= INVALID_SOCKET)
{
MessageBox(hWnd, "Socket ", "Info", MB_OK | MB_ICONINFORMATION);
return;
}
//
srv_socket = socket(AF_INET, SOCK_STREAM, 0);
if(srv_socket == INVALID_SOCKET)
{
MessageBox(hWnd, " !!!", "Error", MB_OK | MB_ICONERROR);
return;
}
// IP
SOCKADDR_IN srv_address;
srv_address.sin_family = AF_INET;
srv_address.sin_port = htons(SERV_PORT);
srv_address.sin_addr.s_addr = INADDR_ANY; // . (.. )
// IP
if(SOCKET_ERROR ==bind(srv_socket,(LPSOCKADDR) &srv_address, sizeof(srv_address)))
{
closesocket(srv_socket);
MessageBox(hWnd, " ", "Error", MB_OK | MB_ICONSTOP);
return;
}
//
if(listen(srv_socket, 4) == SOCKET_ERROR)
{
closesocket(srv_socket);
MessageBox(hWnd, " ","Error", MB_OK);
return;
}
// WSA_ACCEPT
int rc = WSAAsyncSelect(srv_socket, hWnd, WSA_ACCEPT, FD_ACCEPT);
if(rc)
{
closesocket(srv_socket);
MessageBox(hWnd, "WSAAsyncSelect error","Error", MB_OK);
return;
}
//
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM) " ");
}
void ServerStop(HWND hWnd)
{
//
// , Windows Sockets
WSAAsyncSelect(srv_socket, hWnd, 0, 0);
if(srv_socket!= INVALID_SOCKET) // ,
{
closesocket(srv_socket);
srv_socket = INVALID_SOCKET;
}
//
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM) " ");
}
BOOL AcceptClient(int j) //
{
int sockaddr_len = sizeof(sockaddr[j]);
sock[j]=accept(srv_socket,(LPSOCKADDR)&sockaddr[j],(int FAR*)&sockaddr_len);
if(sock[j]!= INVALID_SOCKET)
if(!WSAAsyncSelect(sock[j],hWindow,WSA_NETEVENT, FD_READ | FD_CLOSE))
return TRUE;
closesocket(sock[j]);
return FALSE;
}
void DisconnectClient(int j) //
{
WSAAsyncSelect(sock[j], hWindow, 0, 0);
closesocket(sock[j]);
}
void WndProc_OnWSAAccept(HWND hWnd, LPARAM lParam)
{
//
if(WSAGETSELECTERROR(lParam)) {
MessageBox(hWnd, "accept error", "Error", MB_OK);
WSAAsyncSelect(srv_socket, hWnd, 0, 0);
return;
}
if(ClientNum == 1) {
MessageBox(hWnd," >2\r\n", " !", MB_OK);
return;
}
ClientNum++;
if(!AcceptClient(ClientNum)) {
MessageBox(hWnd, " ", "Error", MB_OK);
return;
}
//
sprintf(szBuf, " %i\r\n: IP=%s Port=%u\r\n \0", ClientNum+1,
inet_ntoa(sockaddr[ClientNum].sin_addr), htons(sockaddr[ClientNum].sin_port));
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)szBuf);
}
void WndProc_OnWSANetEvent(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
char szTemp[256], szMess[256];
int Number;
// , => Number
if(sock[0]==(SOCKET) wParam) Number=0;
else if(sock[1]==(SOCKET) wParam) Number=1;
// ,
if(WSAGETSELECTEVENT(lParam) == FD_READ)
{
int rc = recv((SOCKET) wParam, szTemp, 256, 0);
if(rc){
szTemp[rc]='\0';
sprintf(m_mess, "%s \r\n %i:\r\n%s", m_mess, Number+1,szTemp);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
}
}
// ,
if(WSAGETSELECTEVENT(lParam) == FD_CLOSE)
{
WSAAsyncSelect(sock[Number], hWindow, 0, 0);
closesocket(sock[Number]);
sprintf(szTemp, " %i ", Number+1);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)szTemp);
}
}
void SendToClient(int j)
{
if(j>ClientNum) return;
cbWritten = SendMessage(hwndEdit,WM_GETTEXTLENGTH,0,0);
SendMessage(hwndEdit, WM_GETTEXT, (WPARAM) cbWritten, (LPARAM) szBuf);
szBuf[cbWritten]='\0';
if(send(sock[j],szBuf,lstrlen(szBuf),0)!= SOCKET_ERROR)
{
sprintf(szBuf, " %d\r\n %s",j+1,szBuf);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)szBuf);
}
else
SendMessage(hwndEdit,WM_SETTEXT, 0, (LPARAM)" \r\n");
}
void ClientOff(HWND hWnd, int j)
{
if(j>ClientNum) return;
sprintf(szBuf, " %i?", j+1);
if(IDYES == MessageBox(hWnd, szBuf, "Question", MB_YESNO | MB_ICONQUESTION))
DisconnectClient(j);
ClientNum--;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CREATE:
hwndEdit = CreateWindow(// .
TEXT("EDIT"), NULL,
WS_CHILD | WS_VISIBLE | WS_VSCROLL |
ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
0, 0, 400, 200, hWnd, NULL, hInst, NULL);
//===========================================================
err = WSAStartup(wVersionRequested, &wsaData);
if(err) {
MessageBox(hWnd, "WSAStartup Error","ERROR", MB_OK | MB_ICONSTOP);
return FALSE;
}
sprintf(m_mess, " %s \r\n: %s\r\n",
wsaData.szDescription, wsaData.szSystemStatus);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case ID_32771:
ServerStart(hWnd);
break;
case ID_32772:
ServerStop(hWnd);
break;
case ID_32773:
SendToClient(0);
break;
case ID_32774:
SendToClient(1);
break;
case ID_32775:
ClientOff(hWnd, 0);
break;
case ID_32776:
ClientOff(hWnd, 1);
break;
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
WSACleanup();
PostQuitMessage(0);
break;
case WSA_ACCEPT:
WndProc_OnWSAAccept(hWnd, lParam);
break;
case WSA_NETEVENT:
WndProc_OnWSANetEvent(hWnd, wParam, lParam);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
// SocketClient1.cpp: Defines the entry point for the application.
#include "stdafx.h"
#include "SocketClient1.h"
#include <winsock.h>
#include <process.h>
#include <stdio.h>
#define MAX_LOADSTRING 100
char szBuf[512];
DWORD cbWritten;
int flag = 0;
static HWND hwndEdit;
TCHAR mess[2048];
TCHAR* m_mess = mess;
//=========================================
#define SERV_PORT 5000 //
#define WSA_NETEVENT (WM_USER+1)
WSADATA wsaData; // Windows Sockets
WORD wVersionRequested = MAKEWORD(1, 1); // Windows Sockets
int err=0;
SOCKET cln_socket=INVALID_SOCKET; //
static PHOSTENT phe;
SOCKADDR_IN dest_sin; //
char szHostName[128] = "localhost"; //
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_SOCKETCLIENT1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SOCKETCLIENT1));
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SOCKETCLIENT1));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SOCKETCLIENT1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, 400, 250, NULL, NULL, hInstance, NULL);
if (!hWnd) return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
void Task1()
{ //
sprintf(m_mess, "%s %d \r\n",m_mess, vers);
SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)m_mess);
}
BOOL SetConnection(HWND hWnd)
{
cln_socket = socket(AF_INET, SOCK_STREAM, 0); //
if (cln_socket == INVALID_SOCKET)
{
MessageBox(hWnd, "Socket error", "Error", MB_OK | MB_ICONSTOP);
return FALSE;
}
phe = gethostbyname(szHostName); //
if(phe == NULL)
{
closesocket(cln_socket);
MessageBox(hWnd, " ", "Error", MB_OK | MB_ICONSTOP);
return FALSE;
}
dest_sin.sin_family = AF_INET; //
dest_sin.sin_port = htons(SERV_PORT); //
memcpy((char FAR*) &(dest_sin.sin_addr), phe->h_addr, phe->h_length);//
// <