Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / MFC
Article

CSyncSocket

Rate me:
Please Sign up or sign in to vote.
2.95/5 (9 votes)
15 Jun 2005CPOL 43.1K   1.1K   16   8
A simple TCP class for multithreading usage.

Introduction

First of all, I am sorry for my bad English. CSyncSocket is a simple class that has the same members as CAsyncSocket class, but it can be used in multithreading mode. This class can be used in MFC and Win32 applications. Just include <WINSOCK2.h> and WS2_32.lib in your application and it will work properly.

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
goondoo2712-May-10 6:52
goondoo2712-May-10 6:52 
Mistyped includes, missing code [void CSyncSocket::SendFile(LPSTR FilePath)], no example.
GeneralExample Pin
sanpee16-May-06 21:37
sanpee16-May-06 21:37 
GeneralSource Pin
Anakin_Skywalker15-Jun-05 8:23
Anakin_Skywalker15-Jun-05 8:23 
GeneralRe: Source Pin
Mina W Alphonce20-Jun-05 22:14
Mina W Alphonce20-Jun-05 22:14 
GeneralYes,Please give the details Pin
SpringField15-Jun-05 3:00
SpringField15-Jun-05 3:00 
GeneralRe: Yes,Please give the details Pin
Mina W Alphonce20-Jun-05 22:11
Mina W Alphonce20-Jun-05 22:11 
GeneralDownload link not working Pin
V.M.Smith15-Jun-05 2:31
V.M.Smith15-Jun-05 2:31 
GeneralRe: Download link not working Pin
Mina W Alphonce20-Jun-05 22:10
Mina W Alphonce20-Jun-05 22:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.