Создание сетевой игры

        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();

Страницы: 1, 2, 3, 4, 5, 6, 7



Реклама
В соцсетях
рефераты скачать рефераты скачать рефераты скачать рефераты скачать рефераты скачать рефераты скачать рефераты скачать