# 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...