⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myserver.cpp

📁 一个用于delphi多线程Socket.多线程Socket阻塞模式下通信的例子
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "ServerC.h"
#include "MyServer.h"
#pragma package(smart_init)
const int SMALLLEN = 255;
//---------------------------------------------------------------------------
__fastcall MyServer::MyServer(bool CreateSuspended,TServerClientWinSocket* ASocket,int ATimeOutMSec)
    : TServerClientThread(CreateSuspended,ASocket)
{
    TimeOutMSec=ATimeOutMSec;
    FreeOnTerminate=true;
}
//---------------------------------------------------------------------------
void __fastcall MyServer::ClientExecute()
{
    Connected();
    try{
        DataDisposal();
    }
    __finally{
        DisConnected();
    }
}
//---------------------------------------------------------------------------
void __fastcall MyServer::Connected()
{
     ActiveNum++;
     RemoteHostIP=ClientSocket->RemoteAddress;
     RemoteHostProt=ClientSocket->RemotePort;
     Message="客户机:"+RemoteHostIP+":"+RemoteHostProt+" 已连接...";
     Synchronize((TThreadMethod)&AddMessage);
     ServerFrm->Label1->Caption="当前连接数:"+IntToStr(ActiveNum);
}
//---------------------------------------------------------------------------
void __fastcall MyServer::DisConnected()
{
     ActiveNum--;
     ServerFrm->Label1->Caption="当前连接数:"+IntToStr(ActiveNum);
     Message="客户机:"+RemoteHostIP+":"+RemoteHostProt+" 断开连接...";
     Synchronize((TThreadMethod)&AddMessage);
}
//---------------------------------------------------------------------------
void __fastcall MyServer::DataDisposal(void)   //所有数据处理
{
  char GetStr;
  String RevStr;
  char buf[1024+1];

 try{
      while(ClientSocket->Connected==true)
      {
           memset(buf,0,sizeof(buf));
           if(ReceiveStr(buf,1024)>0)
           {
                RevStr=String(buf);
                Message=RemoteHostIP+"::"+RevStr;
                Synchronize((TThreadMethod)&AddMessage);
               TransmitStr=RemoteHostIP+"::"+RevStr;
            }
          RevStr="";
          //以下进行数据转发
          if (TransmitStr!="")
          {
              bmsg=TransmitStr;
              Synchronize((TThreadMethod)&BC);
              TransmitStr="";
          }
    }
 }
 catch(...)
 {
     return;
 }
}
//---------------------------------------------------------------------------
int __fastcall MyServer::ReceiveStr(char * _Buf,int length)  //接收数据
{
    if(length<=0) return 0;
    int aLen=0;
    int pos=0;
    char aBuf[255];

    int smalllen=0;

    if(length<=smalllen)   
        smalllen=length;
    else
        smalllen=SMALLLEN;

    TWinSocketStream *pStream;
    DWORD beginTick;

    beginTick=GetTickCount();
    while(!ClientSocket->Connected && GetTickCount()-beginTick<200);
    try{
        pStream = new TWinSocketStream(ClientSocket, 1000);
        try{
            if (pStream->WaitForData(TimeOutMSec)){
                while(pStream->WaitForData(100))
                 {
                    Application->ProcessMessages();
                    if((aLen=pStream->Read(aBuf,smalllen)) == 0)
                       {
                        ClientSocket->Close();
                        break;
                        }
                    else{
                        aBuf[aLen]=0;
                        memcpy(_Buf+pos,aBuf,aLen);
                        pos+=aLen;
                        if(pos>=length)
                            break;
                        if(length-pos<smalllen)    //promise get right byte no more no less
                            smalllen=length-pos;
                    }
                }
            }
            else ClientSocket->Close();
        }
        __finally{
            delete pStream;
        }
    }
    catch(Exception &E){
        Application->HandleException(this);
    }
    return pos;
}
//---------------------------------------------------------------------------
int __fastcall MyServer::SendStr(char * _Buf,int length)
{    //该函数未用到
    if(length<=0)
        return 0;
    int SendLen=0;
    TWinSocketStream *pStream;
    DWORD beginTick;

    beginTick=GetTickCount();
    while(!ClientSocket->Connected && GetTickCount()-beginTick<200);
    try{
        pStream = new TWinSocketStream(ClientSocket, 1000);
        try{
            SendLen=pStream->Write(_Buf,length);
        }
        catch(Exception &E){
            Application->HandleException(this);
        }
    }
    __finally{
        delete pStream;
    }
    return SendLen;
}
//---------------------------------------------------------------------------
void __fastcall MyServer::AddMessage() //将接收到的数据加入Memo1中
{
        ServerFrm->Memo1->Lines->Add(Message);
}
//---------------------------------------------------------------------------
void __fastcall MyServer::BC()   //数据转发
{
    ServerFrm->boardcast(bmsg);
    bmsg="";
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -