Ñîçäàíèå ñåòåâîé èãðû
lbStat->Caption="Õîä ñîïåðíèêà...";
}
}
//--------------------------------------------------------------------------
void __fastcall
TMainForm::InterfaceIgraemDalshe(void)
{
NomPartia++;
MyWholeOch=0;
HisWholeOch=0;
MyCurOch=0;
NomHoda=0;
IsYaFixed=false;
IsHeFixed=false;
IsIgrayut=true;
lbNomPartia->Caption="Ïàðòèÿ
¹ "+IntToStr(NomPartia);
lbSchetPoPartiam->Caption=IntToStr(MyWinPoPartiam)+" :
"+IntToStr(HisWinPoPartiam);
listboxOchki->Clear();
listboxOchki->Width=218;
listboxOchki->Left=23;
lbMyWholeOch->Caption="0";
lbHisWholeOch->Caption="0";
lbMyWholeOch->Font->Color=clGreen;
lbHisWholeOch->Font->Color=clGreen;
btBrosit->Color=clCream;
btFix->Color=clCream;
shapeBrosil->Brush->Color=clSilver;
shapeFixed->Brush->Color=clSilver;
for (int
i=0;i<KolKub;i++)
{
Kubiki1[i]->Visible=false;
Kubiki2[i]->Visible=false;
}
if(IsServer)
{
ChangeInterfacePerehodHoda(1);
}
else
{
ChangeInterfacePerehodHoda(0);
}
}
//-----------------------Çàâåðøåíèå èãðû---------------------------------------
void __fastcall
TMainForm::menuStopClick(TObject *Sender)
{
IsVisitApplicationBox=true;
if(Application->MessageBoxA("Âû äåéñòâèòåëüíî õîòèòå
çàâåðøèòü èãðó?","Çàâåðøåíèå èãðû",MB_YESNO)==IDYES)
{
for (int
i=0;i<KolKub;i++)
{
delete Kubiki1[i];
delete Kubiki2[i];
}
switch(choosenProt)
{
case 0:
IPXSend("3;");
IPXCloseConnection();
break;
case 1:
TCPIPSend("3;");
TCPIPCloseConnection();
break;
case 2:
NetBiosSend("3;");
NetBiosCloseConnection();
break;
case 3:
MailSlotSend("3;");
MailSlotCloseConnection();
break;
case 4:
PipeSend("3;");
PipeCloseConnection();
break;
}
IsIgrayut=false;
InterfaceBezIgri();
}
IsVisitApplicationBox=false;
}
//---------------------------------------------------------------------------
void __fastcall
TMainForm::menuQuitClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall
TMainForm::FormCloseQuery(TObject *Sender, bool &CanClose)
{
if (IsIgrayut)
{
menuStopClick(this);
if(IsIgrayut)
CanClose=false;
}
}
//-------------------ñëóæåáíûå ôóíêöèè
ïðåîáðàçîâàíèÿ òèïîâ---------------------
char* __fastcall
TMainForm::DannieToChar(int Type, int MyCurOch, int MyWholeOch,
int ZnachKub1,int XKub1,int YKub1,
int ZnachKub2,int XKub2,int YKub2,
int ZnachKub3,int XKub3,int YKub3,
int ZnachKub4,int XKub4,int YKub4
)
{
AnsiString
strTmp=IntToStr(Type)+";"+IntToStr(MyCurOch)+";"+IntToStr(MyWholeOch)+";"+
IntToStr(ZnachKub1)+";"+IntToStr(XKub1)+";"+IntToStr(YKub1)+";"+
IntToStr(ZnachKub2)+";"+IntToStr(XKub2)+";"+IntToStr(YKub2)+";"+
IntToStr(ZnachKub3)+";"+IntToStr(XKub3)+";"+IntToStr(YKub3)+";"+
IntToStr(ZnachKub4)+";"+IntToStr(XKub4)+";"+IntToStr(YKub4)+";";
return strTmp.c_str();
}
//---------------------------------------------------------------------------
int* __fastcall
TMainForm::CharToMas(char* charPriem)
{
AnsiString
strPriem=charPriem;
MasPriem=new int[15];
int y=0;
int PrevDelim=0;
int CurDelim;
for(int
i=1;i<=strPriem.Length();i++)
{
if(IsDelimiter(";",strPriem,i))
{
MasPriem[y++]=StrToInt(MidStr(strPriem,PrevDelim+1,i-PrevDelim-1));
PrevDelim=i;
}
}
return MasPriem;
}
//Áðîñèòü---------------------------------------------------------------------------
//Çàêîãí÷èòü--------------------------------------------------------------------------
void __fastcall
TMainForm::btBrosit2Click(TObject *Sender)
{
Graphics::TBitmap *Bitmap1
= new Graphics::TBitmap();
char strSend[70];
AnsiString strCat;
if(IsServer)
{
NomHoda++;
strCat="";
}
else
{
strCat="_2";
}
if(KolKub==1)
{
int ZnachKub, XKub, YKub
;
randomize();
ZnachKub = random(6)+1;
XKub=random(126)+14;
YKub=random(126)+14;
Bitmap1->LoadFromResourceName((unsigned
int)HInstance,"KUB"+IntToStr(ZnachKub)+strCat);
Kubiki1[0]->Picture->Bitmap=Bitmap1;
Kubiki1[0]->Left=XKub;
Kubiki1[0]->Top=YKub;
Kubiki1[0]->Visible=true;
MyCurOch=ZnachKub;
MyWholeOch=MyWholeOch+MyCurOch;
strcpy(strSend,DannieToChar(1, MyCurOch, MyWholeOch, ZnachKub, XKub, YKub));
}
else if (KolKub==2)
{
int ZnachKub1, XKub1,
YKub1, ZnachKub2, XKub2, YKub2;
randomize();
ZnachKub1 = random(6)+1;
XKub1=random(126)+14;
YKub1=random(52)+14;
Bitmap1->LoadFromResourceName((unsigned
int)HInstance,"KUB"+IntToStr(ZnachKub1)+strCat);
Kubiki1[0]->Picture->Bitmap=Bitmap1;
Kubiki1[0]->Left=XKub1;
Kubiki1[0]->Top=YKub1;
Kubiki1[0]->Visible=true;
ZnachKub2 = random(6)+1;
XKub2=random(126)+14;
YKub2=random(52)+88;
Bitmap1->LoadFromResourceName((unsigned
int)HInstance,"KUB"+IntToStr(ZnachKub2)+strCat);
Kubiki1[1]->Picture->Bitmap=Bitmap1;
Kubiki1[1]->Left=XKub2;
Kubiki1[1]->Top=YKub2;
Kubiki1[1]->Visible=true;
MyCurOch=ZnachKub1+ZnachKub2;
MyWholeOch=MyWholeOch+MyCurOch;
strcpy(strSend,DannieToChar(1, MyCurOch, MyWholeOch, ZnachKub1, XKub1, YKub1,
ZnachKub2, XKub2, YKub2));
}
else if (KolKub==3)
{
int ZnachKub1, XKub1,
YKub1, ZnachKub2, XKub2, YKub2, ZnachKub3, XKub3, YKub3;
randomize();
ZnachKub1 = random(6)+1;
XKub1=random(126)+14;
YKub1=random(27)+14;
Bitmap1->LoadFromResourceName((unsigned
int)HInstance,"KUB"+IntToStr(ZnachKub1)+strCat);
Kubiki1[0]->Picture->Bitmap=Bitmap1;
Kubiki1[0]->Left=XKub1;
Kubiki1[0]->Top=YKub1;
Kubiki1[0]->Visible=true;
ZnachKub2 = random(6)+1;
XKub2=random(126)+14;
YKub2=random(27)+63;
Bitmap1->LoadFromResourceName((unsigned
int)HInstance,"KUB"+IntToStr(ZnachKub2)+strCat);
Kubiki1[1]->Picture->Bitmap=Bitmap1;
Kubiki1[1]->Left=XKub2;
Kubiki1[1]->Top=YKub2;
Kubiki1[1]->Visible=true;
ZnachKub3 = random(6)+1;
XKub3=random(126)+14;
YKub3=random(28)+112;
Bitmap1->LoadFromResourceName((unsigned
int)HInstance,"KUB"+IntToStr(ZnachKub3)+strCat);
Kubiki1[2]->Picture->Bitmap=Bitmap1;
Kubiki1[2]->Left=XKub3;
Kubiki1[2]->Top=YKub3;
Kubiki1[2]->Visible=true;
MyCurOch=ZnachKub1+ZnachKub2+ZnachKub3;
MyWholeOch=MyWholeOch+MyCurOch;
strcpy(strSend,DannieToChar(1, MyCurOch, MyWholeOch, ZnachKub1, XKub1, YKub1,
ZnachKub2, XKub2, YKub2, ZnachKub3, XKub3, YKub3));
}
else
{
int ZnachKub1, XKub1,
YKub1, ZnachKub2, XKub2, YKub2, ZnachKub3, XKub3, YKub3, ZnachKub4, XKub4,
YKub4;
randomize();
ZnachKub1 = random(6)+1;
XKub1=random(52)+14;
YKub1=random(52)+14;
Bitmap1->LoadFromResourceName((unsigned
int)HInstance,"KUB"+IntToStr(ZnachKub1)+strCat);
Kubiki1[0]->Picture->Bitmap=Bitmap1;
Kubiki1[0]->Left=XKub1;
Kubiki1[0]->Top=YKub1;
Kubiki1[0]->Visible=true;
ZnachKub2 = random(6)+1;
XKub2=random(52)+88;
YKub2=random(52)+14;
Bitmap1->LoadFromResourceName((unsigned
int)HInstance,"KUB"+IntToStr(ZnachKub2)+strCat);
Kubiki1[1]->Picture->Bitmap=Bitmap1;
Kubiki1[1]->Left=XKub2;
Kubiki1[1]->Top=YKub2;
Kubiki1[1]->Visible=true;
ZnachKub3 = random(6)+1;
XKub3=random(52)+14;
YKub3=random(52)+88;
Bitmap1->LoadFromResourceName((unsigned
int)HInstance,"KUB"+IntToStr(ZnachKub3)+strCat);
Kubiki1[2]->Picture->Bitmap=Bitmap1;
Kubiki1[2]->Left=XKub3;
Kubiki1[2]->Top=YKub3;
Kubiki1[2]->Visible=true;
ZnachKub4 = random(6)+1;
XKub4=random(52)+88;
YKub4=random(52)+88;
Bitmap1->LoadFromResourceName((unsigned
int)HInstance,"KUB"+IntToStr(ZnachKub4)+strCat);
Kubiki1[3]->Picture->Bitmap=Bitmap1;
Kubiki1[3]->Left=XKub4;
Kubiki1[3]->Top=YKub4;
Kubiki1[3]->Visible=true;
MyCurOch=ZnachKub1+ZnachKub2+ZnachKub3+ZnachKub4;
MyWholeOch=MyWholeOch+MyCurOch;
strcpy(strSend,DannieToChar(1, MyCurOch, MyWholeOch, ZnachKub1, XKub1, YKub1,
ZnachKub2, XKub2, YKub2,ZnachKub3, XKub3, YKub3, ZnachKub4, XKub4, YKub4));
}
switch(choosenProt)
{
case 0:
IPXSend(strSend);
break;
case 1:
TCPIPSend(AnsiString(strSend));
break;
case 2:
NetBiosSend(strSend);
break;
case 3:
MailSlotSend(strSend);
break;
case 4:
PipeSend(strSend);
break;
}
enum TypeDannihForTabl
DannieForTabl;
DannieForTabl=yabrosil;
ZapolnTabl(DannieForTabl);
lbMyWholeOch->Caption=MyWholeOch;
if (MyWholeOch>MaxOch)
{
lbMyWholeOch->Font->Color=clRed;
ViProigrali();
}
else if(IsHeFixed
&& MyWholeOch>HisWholeOch && MyWholeOch<=MaxOch)
{
ViViigrali();
}
else if(IsHeFixed
&& MyWholeOch<=HisWholeOch)
{
ChangeInterfacePerehodHoda(1);
}
else
{
ChangeInterfacePerehodHoda(0);
}
delete Bitmap1;
}
//---------------------------------------------------------------------------
void __fastcall
TMainForm::btFix2Click(TObject *Sender)
{
IsYaFixed=true;
if(IsServer)
{
NomHoda++;
}
btFix->Color=clRed;
btBrosit->Color=clCream;
lbMyWholeOch->Font->Color=clRed;
switch(choosenProt)
{
case 0:
IPXSend("2;");
break;
case 1:
TCPIPSend("2;");
break;
case 2:
NetBiosSend("2;");
break;
case 3:
MailSlotSend("2;");
break;
case 4:
PipeSend("2;");
break;
}
enum TypeDannihForTabl
DannieForTabl;
DannieForTabl=yafixed;
ZapolnTabl(DannieForTabl);
if(IsHeFixed &&
MyWholeOch==HisWholeOch)
{
Nichia();
}
else
if(MyWholeOch<HisWholeOch)
{
ViProigrali();
}
else
{
ChangeInterfacePerehodHoda(0);
}
}
//---------------------------------------------------------------------------
Ipx.cpp
//---------------------------------------------------------------------------
#pragma hdrstop
#include <winsock.h>
#include <wsipx.h>
#include "IPX.h"
#include
"Main.h"
#include
"Podkluch.h"
//---------------------------------------------------------------------------
#pragma
package(smart_init)
#define IPX_SERVER_SOCKET
0x4504
#define IPX_CLIENT_SOCKET
0x4514
#define WSA_NETEVENT
(WM_USER+2)
SOCKET IPXSocket;
sockaddr_ipx CallAddress;
sockaddr_ipx OurAddress;
extern bool IsServer;
extern int KolKub;
extern int MaxOch;
//-----------------------Ïîäêëþ÷åíèå--------------------------------------------
void __fastcall
TMainForm::IPXConnect(void)
{
WSADATA WSAData;
int rc, OptVal=1;
rc =
WSAStartup(MAKEWORD(2,0), &WSAData);
if (rc != 0)
{
Application->MessageBox("Îøèáêà èíèöèàëèçàöèè
WSAStartup","Îøèáêà",MB_OK+MB_ICONHAND);
Application->Terminate();
}
IPXSocket =
socket(AF_IPX, SOCK_DGRAM, NSPROTO_IPX);
if (IPXSocket ==
INVALID_SOCKET)
{
Application->MessageBox("Ïðîòîêîë IPX íå
óñòàíîâëåí","Îøèáêà",MB_OK+MB_ICONHAND);
Application->Terminate();
}
memset(&OurAddress, 0, sizeof(OurAddress));
OurAddress.sa_family =
AF_IPX;
if(IsServer)
OurAddress.sa_socket = htons((unsigned short)(IPX_SERVER_SOCKET));
else
OurAddress.sa_socket = htons((unsigned short)(IPX_CLIENT_SOCKET));
if (bind(IPXSocket,
(LPSOCKADDR)&OurAddress, sizeof(sockaddr_ipx)) == SOCKET_ERROR)
{
Application->MessageBox("Àäðåñíàÿ îøèáêà
IPX","Îøèáêà",MB_OK+MB_ICONHAND);
closesocket(IPXSocket);
IPXSocket =
INVALID_SOCKET;
Application->Terminate();
}
rc =
setsockopt(IPXSocket, SOL_SOCKET, SO_BROADCAST, (char *)&OptVal,
sizeof(OptVal));
if (rc ==
SOCKET_ERROR)
{
rc =
WSAGetLastError();
Application->MessageBox("Îøèáêà
setsockopt","Îøèáêà",MB_OK+MB_ICONHAND);
closesocket(IPXSocket);
IPXSocket =
INVALID_SOCKET;
Application->Terminate();
}
if(!IsServer)
{
memset(&CallAddress, 0, sizeof(CallAddress));
CallAddress.sa_family = AF_IPX;
CallAddress.sa_socket = htons(IPX_SERVER_SOCKET);
CallAddress.sa_nodenum[0] = 0xff;
CallAddress.sa_nodenum[1] = 0xff;
CallAddress.sa_nodenum[2] = 0xff;
CallAddress.sa_nodenum[3] = 0xff;
CallAddress.sa_nodenum[4] = 0xff;
CallAddress.sa_nodenum[5]
= 0xff;
rc =
sendto(IPXSocket, "connect", 8, 0, (PSOCKADDR)&CallAddress,
sizeof(SOCKADDR));
if (rc ==
SOCKET_ERROR)
{
rc =
WSAGetLastError();
Application->MessageBox("Îøèáêà sendto IPX","Îøèáêà",MB_OK+MB_ICONHAND);
closesocket(IPXSocket);
IPXSocket =
INVALID_SOCKET;
Application->Terminate();
}
}
rc =
WSAAsyncSelect(IPXSocket, Handle, WSA_NETEVENT, FD_READ);
if (rc != 0)
{
Application->MessageBox("Îøèáêà
WSAAsyncSelect","Îøèáêà",MB_OK+MB_ICONHAND);
closesocket(IPXSocket);
IPXSocket =
INVALID_SOCKET;
Application->Terminate();
}
WindowProc =
IPXWndProc;
}
//------------------------------ïðèåì---------------------------------------
void __fastcall
TMainForm::IPXWndProc(Messages::TMessage &Message)
{
int rc,
l=sizeof(CallAddress);
char Buf[80];
if (Message.Msg ==
WSA_NETEVENT)
{
if (Message.LParam
== FD_READ)
{
rc =
recvfrom((SOCKET)Message.WParam, Buf, sizeof(Buf)-1, 0,
(PSOCKADDR)&CallAddress, &l);
if (rc ==
SOCKET_ERROR)
{
rc =
WSAGetLastError();
Application->MessageBox("Îøèáêà recvfrom IPX","Îøèáêà",MB_OK+MB_ICONHAND);
closesocket(IPXSocket);
IPXSocket
= INVALID_SOCKET;
Application->Terminate();
}
if (rc >=
1)
{
if(!strcmp(Buf,"connect"))
{
char
*strPered=DannieToChar(0,KolKub,MaxOch);
IPXSend(strPered);
InterfaceBeginNewGame();
ChangeInterfacePerehodHoda(1);
}
else
{
if(IsVisitApplicationBox) return;
Priem(CharToMas(Buf));
}
}
}
}
WndProc(Message);
}
//-------------------------îòïðàâêà---------------------------------------------
void __fastcall
TMainForm::IPXSend (char* Buf)
{
int rc;
rc = sendto(IPXSocket,
Buf, strlen(Buf)+1, 0, (PSOCKADDR)&CallAddress, sizeof(SOCKADDR));
if (rc ==
SOCKET_ERROR)
{
rc =
WSAGetLastError();
Application->MessageBox("Îøèáêà sendto
IPX","Îøèáêà",MB_OK+MB_ICONHAND);
closesocket(IPXSocket);
IPXSocket =
INVALID_SOCKET;
Application->Terminate();
}
}
//-------------------------ðàçðûâ---------------------------------------------
void __fastcall
TMainForm::IPXCloseConnection(void)
{
closesocket(IPXSocket);
WSACleanup();
}
TCP.cpp
//---------------------------------------------------------------------------
#pragma hdrstop
#include
"TCPIP.h"
#include
"Main.h"
#include
"Podkluch.h"
//---------------------------------------------------------------------------
#pragma
package(smart_init)
extern bool IsServer;
extern bool IsHostName;
extern AnsiString
ServerHostName;
extern AnsiString
ServerHostIP;
extern int KolKub;
extern int MaxOch;
//----------Ïîäêëþ÷åíèå---------------------------------------------------
void __fastcall
TMainForm::TCPIPConnect(void)
{
if (IsServer==true)
{
ClientSocket1->Close();
ServerSocket1->Open();
}
else
{
ServerSocket1->Close();
if (IsHostName)
ClientSocket1->Host=ServerHostName;
else
ClientSocket1->Host=ServerHostIP;
ClientSocket1->Open();
}
}
//---------Accept---------------------------------------------------------------
void __fastcall
TMainForm::ServerSocket1Accept(TObject *Sender,
TCustomWinSocket
*Socket)
{
char
*strPered=DannieToChar(0,KolKub,MaxOch);
TCPIPSend(AnsiString(strPered));
InterfaceBeginNewGame();
ChangeInterfacePerehodHoda(1);
}
//----------Îòïðàâêà------------------------------------------------------------
void __fastcall
TMainForm::TCPIPSend(AnsiString strSend)
{
if(IsServer)
ServerSocket1->Socket->Connections[0]->SendText(strSend);
else
ClientSocket1->Socket->SendText(strSend);
}
//-------------Ïîëó÷åíèå--------------------------------------------------------
void __fastcall
TMainForm::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket
*Socket)
{
if(IsVisitApplicationBox) return;
AnsiString
strPriem=Socket->ReceiveText();
Priem(CharToMas(strPriem.c_str()));
}
//---------------------------------------------------------------------------
void __fastcall
TMainForm::ServerSocket1ClientConnect(TObject *Sender,
TCustomWinSocket
*Socket)
{
//
}
//---------------------------------------------------------------------------
void __fastcall
TMainForm::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket
*Socket)
{
//StatusBar1->Panels->Items[0]->Text
= "Connect to: " + Socket->RemoteHost;
}
//---------------------------------------------------------------------------
void __fastcall
TMainForm::ServerSocket1Listen(TObject *Sender,
TCustomWinSocket
*Socket)
{
//StatusBar1->Panels->Items[0]->Text="Listening...";
}
//--------------Ðàçðûâ
ñîåäèíåíèÿ-------------------------------------------
void __fastcall
TMainForm::TCPIPCloseConnection(void)
{
ServerSocket1->Close();
ClientSocket1->Close();
}
//-----------Îáðàáîòêà
îøèáîê---------------------------------------------------
void __fastcall
TMainForm::ClientSocket1Error(TObject *Sender,
TCustomWinSocket
*Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
if(ErrorEvent==eeConnect)
{
ErrorCode=0;
Application->MessageBox("Íå óäàëîñü îñóùåñòâèòü ñîåäèíåíèå ñ
ñåðâåðîì.\nÓáåäèòåñü, ÷òî ñåðâåð îæèäàåò çàïðîñà
êëèåíòà.","Îøèáêà",MB_OK+MB_ICONHAND);
}
else
{
Application->MessageBox("Îøèáêà
TCP/IP","Îøèáêà",MB_OK+MB_ICONHAND);
Application->Terminate();
}
}
void __fastcall
TMainForm::ServerSocket1ClientError(TObject *Sender,
TCustomWinSocket
*Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
//
}
//-----------------------------------------------------------------------------
Netbios.cpp
//---------------------------------------------------------------------------
#pragma hdrstop
#include <winsock.h>
#include <wsnetbs.h>
#include <stdio.h>
#include
"NetBios.h"
#include
"Main.h"
#include
"Podkluch.h"
#define
NETBIOS_SERVER_NAME "Kosti Server "
#define
NETBIOS_CLIENT_NAME "Kosti Client "
#define WSA_ACCEPT
(WM_USER + 1)
#define WSA_NETEVENT
(WM_USER+2)
//---------------------------------------------------------------------------
#pragma
package(smart_init)
SOCKET NetBiosSocket;
sockaddr_nb OurAddress;
sockaddr_nb CallAddress;
extern bool IsServer;
extern int KolKub;
extern int MaxOch;
//----------------------------Ïîäêëþ÷åíèå---------------------------------------
void __fastcall
TMainForm::NetBiosConnect(void)
{
WSADATA WSAData;
int rc;
rc =
WSAStartup(MAKEWORD(2,0), &WSAData);
if (rc != 0)
{
Application->MessageBox("Îøèáêà èíèöèàëèçàöèè
WSAStartup","Îøèáêà",MB_OK+MB_ICONHAND);
Application->Terminate();
}
int Lana;
for (Lana=0;Lana<1000;Lana++)
{
NetBiosSocket =
socket(AF_NETBIOS, SOCK_SEQPACKET, -Lana);
if (NetBiosSocket
== INVALID_SOCKET)
{
continue;
}
memset(&OurAddress, 0, sizeof(OurAddress));
OurAddress.snb_family = AF_NETBIOS;
OurAddress.snb_type = NETBIOS_UNIQUE_NAME;
if(IsServer)
strcpy(OurAddress.snb_name,NETBIOS_SERVER_NAME);
else
strcpy(OurAddress.snb_name,NETBIOS_CLIENT_NAME);
if
(bind(NetBiosSocket, (LPSOCKADDR)&OurAddress, sizeof(sockaddr_nb)) ==
SOCKET_ERROR)
{
closesocket(NetBiosSocket);
NetBiosSocket
= INVALID_SOCKET;
continue;
}
else break;
}
if(Lana==1000)
{
Application->MessageBox("Íå îïðåäåëåíà
Lana","Îøèáêà",MB_OK+MB_ICONHAND);
Application->Terminate();
}
if(IsServer)
{
if(listen(NetBiosSocket, 1) == SOCKET_ERROR )
{
Application->MessageBox("Listen
Error","Îøèáêà",MB_OK+MB_ICONHAND);
closesocket(NetBiosSocket);
Application->Terminate();
}
rc =
WSAAsyncSelect(NetBiosSocket, Handle, WSA_ACCEPT, FD_ACCEPT);
if (rc != 0)
{
Application->MessageBox("Îøèáêà
WSAAsyncSelect","Îøèáêà",MB_OK+MB_ICONHAND);
closesocket(NetBiosSocket);
NetBiosSocket = INVALID_SOCKET;
Application->Terminate();
}
}
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;
}
|