, FD_READ);
if (rc! = 0)
{
Application-> MessageBox ("Помилка WSAAsyncSelect "," Помилка ", MB_OK + MB_ICONHAND);
closesocket (NetBiosSocket);
NetBiosSocket = INVALID_SOCKET;
Application-> Terminate ();
}
}
}
WndProc (Message);
}
// ------------------- відправка ------------------------- --------------------------
void __ fastcall TMainForm :: NetBiosSend (char * Buf)
{
int rc;
rc = send (NetBiosSocket, Buf, strlen (Buf) +1, 0);
if (rc == SOCKET_ERROR)
{
rc = WSAGetLastError ();
Application-> MessageBox ("Помилка sendto NetBios "," Помилка ", MB_OK + MB_ICONHAND);
closesocket (NetBiosSocket);
NetBiosSocket = INVALID_SOCKET;
Application-> Terminate ();
}
}
// ---------------- розрив ---------------------------- ---------------------------
void __ fastcall TMainForm :: NetBiosCloseConnection (void)
{
closesocket (NetBiosSocket);
WSACleanup ();
}
// --------------------------------------------- ------------------------------
Pipe.cpp
// --------------------------------------------- ------------------------------
# pragma hdrstop
# include
# include
# include
# include "Pipe.h"
# include "Main.h"
# include "Podkluch.h"
// --------------------------------------------- ------------------------------
# pragma package (smart_init)
HANDLE PipeReceiveHandle, PipeSendHandle;
extern bool IsServer;
extern AnsiString ServerHostName;
extern int KolKub;
extern int MaxOch;
// ----------------------- підключення --------------------- -----------------------
void __ fastcall TMainForm :: PipeConnect (void)
{
if (IsServer)
{
PipeReceiveHandle = CreateNamedPipe (" . pipe pipe_s", PIPE_ACCESS_INBOUND,
PIPE_TYPE_BYTE | PIPE_NOWAIT, PIPE_UNLIMITED_INSTANCES,
1000, 1000, 5000, NULL);
if (PipeReceiveHandle == INVALID_HANDLE_VALUE)
{
Application-> MessageBox ("Помилка при створенні pipe на сервері "," Помилка ", MB_OK + MB_ICONHAND);
TimerPipe-> Enabled = false;
Application-> Terminate ();
}
TimerPipe-> Enabled = true;
}
else
{
PipeReceiveHandle = CreateNamedPipe (" . Pipe pipe_c", PIPE_ACCESS_INBOUND,
PIPE_TYPE_BYTE | PIPE_NOWAIT, PIPE_UNLIMITED_INSTANCES,
1000, 1000, 5000, NULL);
if (PipeReceiveHandle == INVALID_HANDLE_VALUE)
{
Application-> MessageBox ("Помилка при створенні pipe на клієнті "," Помилка ", MB_OK + MB_ICONHAND);
TimerPipe-> Enabled = false;
Application-> Terminate ();
}
TimerPipe-> Enabled = true;
char Buf [100];
sprintf (Buf, " % S pipe pipe_s", ServerHostName.c_str ());
PipeSendHandle = CreateFile (Buf, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (PipeSendHandle == INVALID_HANDLE_VALUE)
{
int rc;
rc = GetLastError ();// 5 51
Application-> MessageBox ("Помилка при підключенні до pipe на сервері "," Помилка ", MB_OK + MB_ICONHAND);
Application-> Terminate ();
}
char CompName [50];
unsigned long cnl = 50;
GetComputerName (CompName, & Cnl);
int i = StrLen (CompName);
if (i> = 50)
{
CompName [49] = ' 0';
}
unsigned long cbWritten;
char BufExchange [100];
strcpy (BufExchange, "connect;");
strcat (BufExchange, CompName);
WriteFile (PipeSendHandle, BufExchange, i +9, & cbWritten, NULL);
}
}
// --------------- прийом і accept --------------------------------------
void __ fastcall TMainForm :: TimerPipeTimer (TObject * Sender)
{
int Rc;
DWORD cbRead;
DWORD cbWritten;
char Buf [100];
ConnectNamedPipe (PipeReceiveHandle, NULL);
Rc = GetLastError ();
if (Rc == ERROR_PIPE_CONNECTED)// 535
{
if (ReadFile (PipeReceiveHandle, Buf, sizeof (Buf) -1, & cbRead, NULL))
{
if (LeftStr (AnsiString (Buf), 7) == "connect")
{
char * Buftmp;
char BufConnect [150];
Buftmp = AnsiStrScan (Buf, ';');
Buftmp + +;
sprintf (BufConnect, " % S pipe pipe_c", Buftmp);
PipeSendHandle = CreateFile (BufConnect, GENERIC_WRITE, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL);
if (PipeSendHandle == INVALID_HANDLE_VALUE)
{
Application-> MessageBox ("Помилка при підключенні...