Jump to content

    
Sign in to follow this  
dimonbest

связь нескольких модемов с компьютером

Recommended Posts

Вот минимальный кусок для VS С++ ver.6 для понимания, проект с MFC создать самим. Основан на примере из MSDN, там есть рабочий проект. Проверить работу можно даже на одном компе, используя в качестве клиента стандартный HyperTerminal (Windows):

#include "stdafx.h"
#include <afxsock.h>

class CClientSocket : public CSocket
{
public:
    int no;//id
    bool isClosed;
    void close();
    CClientSocket(); //constructor
// Overridable callbacks
protected:
    virtual void OnReceive(int nErrorCode);
};

PutErr(){ //ñîîáù îá îø
    LPTSTR  lpMsgBuf;     DWORD dwError = GetLastError();
    if(dwError==0)return 0;
    FormatMessage(     FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,        dwError,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
        (LPTSTR) &lpMsgBuf,        0,        NULL     );
    char s[20];
    sprintf(s,"Îøèáêà:%u",dwError);
    MessageBox( NULL, lpMsgBuf, s, MB_OK|MB_ICONINFORMATION );
    LocalFree( lpMsgBuf );// Free the buffer.
    return dwError;
}

CClientSocket *pLastSocket, *pnSocket, *m_connectionList[MAXGPRS];

void CClientSocket ::close(){
        if(isClosed)return;
        isClosed=1;
        m_connectionList[no]=NULL;
        Send("Disconnect\n",11);
        ShutDown();
}
CClientSocket ::CClientSocket(){isClosed=0; no=0;} //constructor
void CClientSocket ::OnReceive(int nErrorCode){ //îáðàáîòêà ïîëó÷àåìûõ äàííûõ
        CSocket::OnReceive(nErrorCode);
        CClientSocket* pSocket=this;
        TCHAR buff[4096];
        int nRead;
        try{
        nRead = pSocket->Receive(buff, sizeof(buff)-1); 
        switch (nRead)
        {
            case 0:
              printf("Null Receive!");
              break;
            case SOCKET_ERROR:
              if (GetLastError() != WSAEWOULDBLOCK) PutErr();
              break;
            default:
              buff[nRead] = 0; //terminate the string
              CString szTemp(buff);
            pSocket->Send("yes!\n",5);
        }
        if(no>0){
            if(m_connectionList[no] && m_connectionList[no]!=this)printf("Change client %d!\a",no);
            m_connectionList[no]=this;
        }
        }catch(...){}
}

class CListeningSocket : public CSocket
{
// Overridable callbacks
protected:
    virtual void OnAccept(int nErrorCode){ //íîâûé êëèåíò
    CSocket::OnAccept(nErrorCode);

    CClientSocket* pSocket = new CClientSocket();
    if (Accept(*pSocket))
    {
        pLastSocket=pSocket;
        printf("Client in");
    }
    else
        delete pSocket;
    }
};
    CListeningSocket Sock;    
    CListeningSocket* m_pSocket;

    //êëàññ è ðåñóðñû äëÿ äèàëîãà ñîçäàòü ñàìèì, òóò ïðèìåð èíèöèàöèè
/////////////////////////////////////////////////////////////////////////////
// CChatDlg message handlers

BOOL CChatDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
    
    // TODO: Add extra initialization here

        m_pSocket = &Sock;
        //700 - ¹ ïîðòà, ÷åðåç êîòîðûé îáìåí
        if (!m_pSocket->Create(700)){PutErr(); return TRUE;}
        if (!m_pSocket->Listen()){PutErr(); return TRUE;}
        printf("Wait clients...");
    
    return TRUE;  // return TRUE  unless you set the focus to a control
}

 

//700 - № порта, через который обмен

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this