}
}
else
{
memset(&CallAddress, 0, sizeof(CallAddress));
CallAddress.snb_family = AF_NETBIOS;
CallAddress.snb_type = NETBIOS_UNIQUE_NAME;
strcpy(CallAddress.snb_name,NETBIOS_SERVER_NAME);
if(connect(NetBiosSocket , (PSOCKADDR )&CallAddress, sizeof(CallAddress)) < 0)
{
Application->MessageBox("Connect Error","Ошибка",MB_OK+MB_ICONHAND);
closesocket(NetBiosSocket);
Application->Terminate();
}
rc = WSAAsyncSelect(NetBiosSocket, Handle, WSA_NETEVENT, FD_READ);
if (rc != 0)
{
Application->MessageBox("Ошибка WSAAsyncSelect","Ошибка",MB_OK+MB_ICONHAND);
closesocket(NetBiosSocket);
NetBiosSocket = INVALID_SOCKET;
Application->Terminate();
}
}
WindowProc = NetBiosWndProc;
}
//-------------------Прием и accept---------------------------------------------
void __fastcall TMainForm::NetBiosWndProc(Messages::TMessage &Message)
{
int rc;
char Buf[80];
if (Message.Msg == WSA_NETEVENT)
{
if (Message.LParam == FD_READ)
{
rc = recv((SOCKET)Message.WParam, Buf, sizeof(Buf)-1, 0);
if (rc == SOCKET_ERROR)
{
rc = WSAGetLastError();
Application->MessageBox("Ошибка recv NetBios","Ошибка",MB_OK+MB_ICONHAND);
closesocket(NetBiosSocket);
NetBiosSocket = INVALID_SOCKET;
Application->Terminate();
}
if (rc >= 1)
{
if(IsVisitApplicationBox) return;
Priem(CharToMas(Buf));
}
}
}
else if(Message.Msg == WSA_ACCEPT)
{
if(Message.LParam == FD_ACCEPT)
{
int sizeCallAddress = sizeof(CallAddress);
NetBiosSocket = accept (NetBiosSocket, (LPSOCKADDR )&CallAddress, (int FAR *)&sizeCallAddress);
if(NetBiosSocket == INVALID_SOCKET)
{
Application->MessageBox("Ошибка Accept","Ошибка",MB_OK+MB_ICONHAND);
Application->Terminate();
}
char *strPered=DannieToChar(0,KolKub,MaxOch);
NetBiosSend(strPered);
InterfaceBeginNewGame();
ChangeInterfacePerehodHoda(1);
rc = WSAAsyncSelect(NetBiosSocket, Handle, WSA_NETEVENT, 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 <stdio.h>
#include <stdlib.h>
#include <StrUtils.hpp>
#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("Ошибка при подключении к pipe на клиенте","Ошибка",MB_OK+MB_ICONHAND);
Application->Terminate();
}
char *strPered=DannieToChar(0,KolKub,MaxOch);
PipeSend(strPered);
InterfaceBeginNewGame();
ChangeInterfacePerehodHoda(1);
}
else
{
if(IsVisitApplicationBox) return;
Priem(CharToMas(Buf));
}
}
}
}
//-----------------отправка-----------------------------------------------------
void __fastcall TMainForm::PipeSend (char* Buf)
{
unsigned long cbWritten=0;
WriteFile(PipeSendHandle, Buf, strlen(Buf)+1, &cbWritten,NULL);
}
//-------------------разрыв соединения-----------------------------------------
void __fastcall TMainForm::PipeCloseConnection(void)
{
CloseHandle(PipeSendHandle);
CloseHandle(PipeReceiveHandle);
TimerPipe->Enabled = false;
}
Mailslot.cpp
//---------------------------------------------------------------------------
#pragma hdrstop
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MailSlot.h"
#include "Podkluch.h"
#include "Main.h"
//------------------------------------------------------------------------------
#pragma package(smart_init)
HANDLE ReceiveMailslotHandle, SendMailslotHandle;
int HisPrevNomMessage=0;
int MyCurrNomMessage=0;
extern bool IsServer;
extern AnsiString ServerHostName;
extern int KolKub;
extern int MaxOch;
//-----------------------подключение--------------------------------------------
void __fastcall TMainForm::MailSlotConnect(void)
{
if(IsServer)
{
ReceiveMailslotHandle = CreateMailslot("\\\\.\\mailslot\\msserver", 0, MAILSLOT_WAIT_FOREVER, NULL);
if (ReceiveMailslotHandle == INVALID_HANDLE_VALUE)
{
Application->MessageBox("Ошибка при создании mailslot","Ошибка",MB_OK+MB_ICONHAND);
Application->Terminate();
}
TimerMailSlot->Enabled = true;
}
else
{
ReceiveMailslotHandle = CreateMailslot("\\\\.\\mailslot\\msclient", 0, MAILSLOT_WAIT_FOREVER, NULL);
if (ReceiveMailslotHandle == INVALID_HANDLE_VALUE)
{
Application->MessageBox("Ошибка при создании mailslot","Ошибка",MB_OK+MB_ICONHAND);
Application->Terminate();
}
TimerMailSlot->Enabled = true;
char BufConnect[70];
sprintf(BufConnect, "\\\\%s\\mailslot\\msserver" , ServerHostName.c_str());
SendMailslotHandle = CreateFile(BufConnect, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (SendMailslotHandle == INVALID_HANDLE_VALUE)
{
Application->MessageBox("Ошибка при открытии mailslot","Ошибка",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[50];
MyCurrNomMessage=1;
strcpy(BufExchange,"1;");
strcat(BufExchange,CompName);
WriteFile(SendMailslotHandle,BufExchange, strlen(BufExchange)+1, &cbWritten, NULL);
}
}
//-----------------------прием и accept-----------------------------------------
void __fastcall TMainForm::TimerMailSlotTimer(TObject *Sender)
{
if(IsVisitApplicationBox) return;
int rc;
unsigned long cbMessages, cbMsgNumber, cbRead;
char *BufExchange;
rc = GetMailslotInfo(ReceiveMailslotHandle, NULL, &cbMessages, &cbMsgNumber, NULL);
if (!rc)
{
Application->MessageBox("Ошибка GetMailslotInfo","Ошибка",MB_OK+MB_ICONHAND);
Application->Terminate();
}
if (cbMsgNumber == 0) return;
BufExchange = new char[cbMessages];
ZeroMemory(BufExchange, cbMessages);
if (ReadFile(ReceiveMailslotHandle, BufExchange, cbMessages, &cbRead, NULL))
{
int HisCurrNomMessage;
char Buftmp[150];
strcpy(Buftmp,BufExchange);
HisCurrNomMessage=atoi(strtok(BufExchange,";"));
if(HisCurrNomMessage>HisPrevNomMessage)
{
HisPrevNomMessage=HisCurrNomMessage;
char *Buf;
Buf=AnsiStrScan(Buftmp,';');
Buf++;
if(IsServer && HisCurrNomMessage==1)
{
char BufConnect[100];
sprintf(BufConnect, "\\\\%s\\mailslot\\msclient" , Buf);
SendMailslotHandle = CreateFile(BufConnect, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (SendMailslotHandle == INVALID_HANDLE_VALUE)
{
Application->MessageBox("Ошибка при открытии mailslot","Ошибка",MB_OK+MB_ICONHAND);
Application->Terminate();
}
char *strPered=DannieToChar(0,KolKub,MaxOch);
MailSlotSend(strPered);
InterfaceBeginNewGame();
ChangeInterfacePerehodHoda(1);
}
else
{
Priem(CharToMas(Buf));
}
}
}
delete [] BufExchange;
}
//------------------------отправка----------------------------------------------
void __fastcall TMainForm::MailSlotSend(char *Buf)
{
unsigned long cbWritten;
char BufExchange[100];
MyCurrNomMessage++;
itoa(MyCurrNomMessage,BufExchange,10);
strcat(BufExchange,";");
strcat(BufExchange,Buf);
WriteFile(SendMailslotHandle, BufExchange, strlen(BufExchange)+1, &cbWritten, NULL);
}
//----------------------разрыв соединения---------------------------------------
void __fastcall TMainForm::MailSlotCloseConnection(void)
{
CloseHandle(SendMailslotHandle);
CloseHandle(ReceiveMailslotHandle);
TimerMailSlot->Enabled = false;
MyCurrNomMessage=0;
HisPrevNomMessage=0;
}