65.9K
CodeProject 正在变化。 阅读更多。
Home

CSyncSocket

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.95/5 (9投票s)

2005 年 6 月 15 日

CPOL
viewsIcon

44101

downloadIcon

1119

一个用于多线程使用的简单 TCP 类。

引言

首先,我对我的英语不好表示歉意。CSyncSocket 是一个简单的类,它具有与 CAsyncSocket 类相同的成员,但可以在多线程模式下使用。 此类可用于 MFC 和 Win32 应用程序。只需在您的应用程序中包含 <WINSOCK2.h>WS2_32.lib,它就可以正常工作。

class CSyncSocket 
{
    public:
    /* close the connection , see ShutDown Method 
    in CAsyncSocket.*/
    void ShutDown(int HOW);
    /* delete any related components for this socket, 
    see CloseSocket Method in CAsynSocket*/
    void Close();
    /* suspend the thread until the queue of the 
    socket get a connection */
    void Accept(CSyncSocket*);
    
    /******************************************************/
    /* attributes */
    /******************************************************/
    /* actes like the Methods in CAsynSocket */
    BOOL GetSockOpt(int nOptionName,void* lpOptionValue,
                  int* lpOptionLen,int nLevel = SOL_SOCKET);
    BOOL SetSockOpt(int nOptionName,const void* lpOptionValue,
                     int nOptionLen,int nLevel = SOL_SOCKET ); 
    /******************************************************/
    /* Operations */
    /******************************************************/
    
    // bind the socket to a port 
    bool Bind(UINT Port);
    // turn the socket into the listen state
    bool Listen(UINT Port);
    // receive an array of bytes of the of size (Length) 
    // and return the actuel data size in the array 
    int Recieve(BYTE*buff, int Length);
    // send an array of bytes of size (Length)
    bool Send(BYTE* buff, int length);
    // connect to a specific host and port 
    bool Connect(LPSTR Host, UINT port);
    
    
    
    /*******************************************************/
    /*// initialization */
    /*******************************************************/
    // create the socket ( for each socket you 
    // must call create anfter the constructor
    BOOL Create();
    CSyncSocket();
    
    
    // operators
    void operator = (CSyncSocket& dest);
    operator SOCKET();
    virtual ~CSyncSocket();
    
    
    //data member 
    SOCKET m_Socket;
    protected:
    virtual bool OnAccept(sockaddr*,int*);
    bool m_IsBound;
    virtual u_long GetCompatibleHost(CString Host);
};
© . All rights reserved.