IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

MFC Discussion :

exemple mfcsoc mais en mode UDP


Sujet :

MFC

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    134
    Détails du profil
    Informations personnelles :
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations forums :
    Inscription : Mars 2006
    Messages : 134
    Par défaut exemple mfcsoc mais en mode UDP
    L'exemple MFCSocs est une application MFC minimale qui indique comment utiliser les classes CSocket et CAsyncSocket pour communiquer dans une connexion TCP. Cet exemple est une application basée sur les boîtes de dialogue. Selon la sélection dans l'interface utilisateur, l'application peut s'exécuter à un serveur sur le même port TCP comme un serveur TCP à l'écoute sur certain port TCP ou un client qui se connecte.Lorsqu' être exécutée sous un client, l'application utilise la classe CSocket pour communiquer avec le serveur. Lorsqu' exécuter comme un serveur, l'application utilise CAsyncSocket pour écouter des demandes de connexion et pour communiquer après l'établissement de la connexion avec le client.



    J'ai pour but de faire exactement la meme chose mais en utilisant le protocole UDP et comme j'ai un peu du mal en C++ je sollicite votre aide.

    Dans l'application d'origine cad en TCP on fait un CONNECT en mode client et un ACCEPT en mode SERVEUR. Tandis quand mode UDP on ne fait pas tout sa il faut faire des BIND des 2 cotés.

    De plus je pense qu'il faut remplacer tout les send (resp recv) par des senTo (resp recvFrom) or ces methodes demande plus de parametres.

    Que dois je changer pour obtenir exactement la meme programme mais en utilisant le protocole UDP.

    Vous trouverai le code source du programme sur msdn :
    http://download.microsoft.com/downlo...us/mfcsocs.exe

    sinon voici les morceau les plus important

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
     
    // Asynlstn.cpp : implementation file
    //
     
    #include "stdafx.h"
    #include "msocs.h"
     
    #include "ClntSock.h"
    #include "AsynLstn.h"
    #include "AsynSvSk.h"
    #include "msocsDlg.h"
     
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
     
     
    /////////////////////////////////////////////////////////////////////////////
    // CAsynlstn
     
    CAsynlstn::CAsynlstn(CDialog* pDlg)
    {
    	m_pDlg = pDlg;
    }
     
    CAsynlstn::~CAsynlstn()
    {
    }
     
     
    // Do not edit the following lines, which are needed by ClassWizard.
    #if 0
    BEGIN_MESSAGE_MAP(CAsynlstn, CAsyncSocket)
    	//{{AFX_MSG_MAP(CAsynlstn)
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    #endif	// 0
     
    /////////////////////////////////////////////////////////////////////////////
    // CAsynlstn member functions
     
    void CAsynlstn::OnAccept(int nErrorCode) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	if (((CMsocsDlg*)m_pDlg)->m_pSrvrSock) 
    	{
    		AfxMessageBox ("Only allow one message socket at a time");
    		CAsynSvSk tempSvSk(NULL);
    		Accept(tempSvSk);
    		tempSvSk.Close();
    	}
    	else
    	{
    		if (IDYES ==AfxMessageBox ("Do you want to accept the incoming connection", MB_YESNO))
    		{
    			if ((((CMsocsDlg*)m_pDlg)->m_pSrvrSock = new CAsynSvSk(m_pDlg)) == NULL)
    			{
    				AfxMessageBox("Failed to allocate server socket");
    				return;
    			}
    			((CMsocsDlg*)m_pDlg)->m_pSrvrSock->m_sendBuffer = "";   //for async send
    			((CMsocsDlg*)m_pDlg)->m_pSrvrSock->m_nBytesSent = 0;
    			((CMsocsDlg*)m_pDlg)->m_pSrvrSock->m_nBytesBufferSize = 0;
    			((CMsocsDlg*)m_pDlg)->GetDlgItem(IDC_CLOSESOCK)->EnableWindow(TRUE);
     
    			Accept(*((CMsocsDlg*)m_pDlg)->m_pSrvrSock);
    		}
    		else
    		{
    			CAsynSvSk tempSvSk(NULL);
    			Accept(tempSvSk);
    			tempSvSk.Close();
    		}
    	}
    	CAsyncSocket::OnAccept(nErrorCode);
    }
     
    void CAsynlstn::OnClose(int nErrorCode) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	((CMsocsDlg*)m_pDlg)->m_pLstnSock = NULL;		
    	CAsyncSocket::OnClose(nErrorCode);
    }
     
    void CAsynlstn::OnConnect(int nErrorCode) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	CAsyncSocket::OnConnect(nErrorCode);
    }
     
    void CAsynlstn::OnOutOfBandData(int nErrorCode) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	CAsyncSocket::OnOutOfBandData(nErrorCode);
    }
     
    void CAsynlstn::OnReceive(int nErrorCode) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	CAsyncSocket::OnReceive(nErrorCode);
    }
     
    void CAsynlstn::OnSend(int nErrorCode) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	CAsyncSocket::OnSend(nErrorCode);
    }
     
    int CAsynlstn::Receive(void* lpBuf, int nBufLen, int nFlags) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	return CAsyncSocket::Receive(lpBuf, nBufLen, nFlags);
    }
     
    int CAsynlstn::Send(const void* lpBuf, int nBufLen, int nFlags) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	return CAsyncSocket::Send(lpBuf, nBufLen, nFlags);
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
     
    // Asynlstn.cpp : implementation file
    //
     
    #include "stdafx.h"
    #include "msocs.h"
     
    #include "ClntSock.h"
    #include "AsynLstn.h"
    #include "AsynSvSk.h"
    #include "msocsDlg.h"
     
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
     
     
    /////////////////////////////////////////////////////////////////////////////
    // CAsynlstn
     
    CAsynlstn::CAsynlstn(CDialog* pDlg)
    {
    	m_pDlg = pDlg;
    }
     
    CAsynlstn::~CAsynlstn()
    {
    }
     
     
    // Do not edit the following lines, which are needed by ClassWizard.
    #if 0
    BEGIN_MESSAGE_MAP(CAsynlstn, CAsyncSocket)
    	//{{AFX_MSG_MAP(CAsynlstn)
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    #endif	// 0
     
    /////////////////////////////////////////////////////////////////////////////
    // CAsynlstn member functions
     
    void CAsynlstn::OnAccept(int nErrorCode) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	if (((CMsocsDlg*)m_pDlg)->m_pSrvrSock) 
    	{
    		AfxMessageBox ("Only allow one message socket at a time");
    		CAsynSvSk tempSvSk(NULL);
    		Accept(tempSvSk);
    		tempSvSk.Close();
    	}
    	else
    	{
    		if (IDYES ==AfxMessageBox ("Do you want to accept the incoming connection", MB_YESNO))
    		{
    			if ((((CMsocsDlg*)m_pDlg)->m_pSrvrSock = new CAsynSvSk(m_pDlg)) == NULL)
    			{
    				AfxMessageBox("Failed to allocate server socket");
    				return;
    			}
    			((CMsocsDlg*)m_pDlg)->m_pSrvrSock->m_sendBuffer = "";   //for async send
    			((CMsocsDlg*)m_pDlg)->m_pSrvrSock->m_nBytesSent = 0;
    			((CMsocsDlg*)m_pDlg)->m_pSrvrSock->m_nBytesBufferSize = 0;
    			((CMsocsDlg*)m_pDlg)->GetDlgItem(IDC_CLOSESOCK)->EnableWindow(TRUE);
     
    			Accept(*((CMsocsDlg*)m_pDlg)->m_pSrvrSock);
    		}
    		else
    		{
    			CAsynSvSk tempSvSk(NULL);
    			Accept(tempSvSk);
    			tempSvSk.Close();
    		}
    	}
    	CAsyncSocket::OnAccept(nErrorCode);
    }
     
    void CAsynlstn::OnClose(int nErrorCode) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	((CMsocsDlg*)m_pDlg)->m_pLstnSock = NULL;		
    	CAsyncSocket::OnClose(nErrorCode);
    }
     
    void CAsynlstn::OnConnect(int nErrorCode) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	CAsyncSocket::OnConnect(nErrorCode);
    }
     
    void CAsynlstn::OnOutOfBandData(int nErrorCode) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	CAsyncSocket::OnOutOfBandData(nErrorCode);
    }
     
    void CAsynlstn::OnReceive(int nErrorCode) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	CAsyncSocket::OnReceive(nErrorCode);
    }
     
    void CAsynlstn::OnSend(int nErrorCode) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	CAsyncSocket::OnSend(nErrorCode);
    }
     
    int CAsynlstn::Receive(void* lpBuf, int nBufLen, int nFlags) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	return CAsyncSocket::Receive(lpBuf, nBufLen, nFlags);
    }
     
    int CAsynlstn::Send(const void* lpBuf, int nBufLen, int nFlags) 
    {
    	// TODO: Add your specialized code here and/or call the base class
    	return CAsyncSocket::Send(lpBuf, nBufLen, nFlags);
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
     
    // ClntSock.cpp : implementation file
    //
     
    #include "stdafx.h"
    #include "msocs.h"
     
    #include "ClntSock.h"
    #include "AsynLstn.h"
    #include "AsynSvSk.h"
    #include "msocsDlg.h"
     
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
     
    /////////////////////////////////////////////////////////////////////////////
    // CClntSock
     
    CClntSock::CClntSock(CDialog* pDlg)
    {
    	m_pDlg = pDlg;
    }
     
    CClntSock::~CClntSock()
    {
    }
     
     
    // Do not edit the following lines, which are needed by ClassWizard.
    #if 0
    BEGIN_MESSAGE_MAP(CClntSock, CSocket)
    	//{{AFX_MSG_MAP(CClntSock)
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    #endif	// 0
     
    /////////////////////////////////////////////////////////////////////////////
    // CClntSock member functions
     
    void CClntSock::OnAccept(int nErrorCode) 
    {
    	CSocket::OnAccept(nErrorCode);
    }
     
    void CClntSock::OnClose(int nErrorCode) 
    {
    	((CMsocsDlg*)m_pDlg)->m_pClientSock = NULL;
    	((CMsocsDlg*)m_pDlg)->m_strRecv = "";
    	((CMsocsDlg*)m_pDlg)->m_strSend = "";
    	((CMsocsDlg*)m_pDlg)->UpdateData(FALSE);
    	((CMsocsDlg*)m_pDlg)->GetDlgItem(IDC_CLOSESOCK)->EnableWindow(FALSE);
    	CSocket::OnClose(nErrorCode);
    	delete this;
    }
     
    void CClntSock::OnConnect(int nErrorCode) 
    {
    	if (!nErrorCode)
    	{
    		TCHAR szError[256];
    		wsprintf(szError, "OnConnect error: %d", nErrorCode);
    		AfxMessageBox(szError);
    		AfxMessageBox("Please close the application");
    	}
    	CSocket::OnConnect(nErrorCode);
    }
     
    BOOL CClntSock::OnMessagePending() 
    {
    	return CSocket::OnMessagePending();
    }
     
    void CClntSock::OnOutOfBandData(int nErrorCode) 
    {
    	CSocket::OnOutOfBandData(nErrorCode);
    }
     
    void CClntSock::OnReceive(int nErrorCode) 
    {
    	TCHAR buff[4096];
    	int nRead;
    	nRead = Receive(buff, 4096); 
    	if (nRead != SOCKET_ERROR)
    	{
    		if (nRead)
    		{
    			buff[nRead] = 0; 
    			CString szTemp(buff);
    			((CMsocsDlg*)m_pDlg)->m_strRecv += szTemp;
    			((CMsocsDlg*)m_pDlg)->UpdateData(FALSE);
    			if (szTemp.CompareNoCase("bye") == 0 ) 	ShutDown();
    		}
    		else Close();
    	}
    	else
    	{
    		wsprintf (buff, "Error number is %d", GetLastError());
    		AfxMessageBox (buff);
    	}
     
    	CSocket::OnReceive(nErrorCode);
    }
     
    void CClntSock::OnSend(int nErrorCode) 
    {
    	CSocket::OnSend(nErrorCode);
    }
     
    int CClntSock::Receive(void* lpBuf, int nBufLen, int nFlags) 
    {
    	return CSocket::Receive(lpBuf, nBufLen, nFlags);
    }
     
    int CClntSock::Send(const void* lpBuf, int nBufLen, int nFlags) 
    {
    	return CSocket::Send(lpBuf, nBufLen, nFlags);
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
     
    // msocsDlg.cpp : implementation file
    //
     
    #include "stdafx.h"
    #include "msocs.h"
     
    #include "ClntSock.h"
    #include "AsynLstn.h"
    #include "AsynSvSk.h"
    #include "msocsDlg.h"
     
     
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
     
    /////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App About
     
    class CAboutDlg : public CDialog
    {
    public:
    	CAboutDlg();
     
    // Dialog Data
    	//{{AFX_DATA(CAboutDlg)
    	enum { IDD = IDD_ABOUTBOX };
    	//}}AFX_DATA
     
    	// ClassWizard generated virtual function overrides
    	//{{AFX_VIRTUAL(CAboutDlg)
    	protected:
    	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    	//}}AFX_VIRTUAL
     
    // Implementation
    protected:
    	//{{AFX_MSG(CAboutDlg)
    	//}}AFX_MSG
    	DECLARE_MESSAGE_MAP()
    };
     
    CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    	//{{AFX_DATA_INIT(CAboutDlg)
    	//}}AFX_DATA_INIT
    }
     
    void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	//{{AFX_DATA_MAP(CAboutDlg)
    	//}}AFX_DATA_MAP
    }
     
    BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    	//{{AFX_MSG_MAP(CAboutDlg)
    		// No message handlers
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
     
    /////////////////////////////////////////////////////////////////////////////
    // CMsocsDlg dialog
     
    CMsocsDlg::CMsocsDlg(CWnd* pParent /*=NULL*/)
    	: CDialog(CMsocsDlg::IDD, pParent)
    {
    	//{{AFX_DATA_INIT(CMsocsDlg)
    	m_port = 5000;
    	m_boolClient = FALSE;
    	m_strRecv = _T("");
    	m_strServer = _T("");
    	m_strSend = _T("");
    	m_pClientSock = NULL;
    	m_pLstnSock = NULL;
    	m_pSrvrSock = NULL;
    	//}}AFX_DATA_INIT
    	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
     
    void CMsocsDlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	//{{AFX_DATA_MAP(CMsocsDlg)
    	DDX_Text(pDX, IDC_PORTNO, m_port);
    	DDV_MinMaxUInt(pDX, m_port, 1024, 10000);
    	DDX_Text(pDX, IDC_RECEIVE, m_strRecv);
    	DDX_Text(pDX, IDC_RSVRNAME, m_strServer);
    	DDX_Text(pDX, IDC_SEND, m_strSend);
    	//}}AFX_DATA_MAP
    }
     
    BEGIN_MESSAGE_MAP(CMsocsDlg, CDialog)
    	//{{AFX_MSG_MAP(CMsocsDlg)
    	ON_WM_SYSCOMMAND()
    	ON_WM_PAINT()
    	ON_WM_QUERYDRAGICON()
    	ON_BN_CLICKED(IDC_SENDBTN, OnSendbtn)
    	ON_BN_CLICKED(IDC_CKCLIENT, OnCkclient)
    	ON_BN_CLICKED(IDC_CHKSERVER, OnChkserver)
    	ON_WM_DESTROY()
    	ON_BN_CLICKED(IDC_CLOSESOCK, OnClosesock)
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
     
    /////////////////////////////////////////////////////////////////////////////
    // CMsocsDlg message handlers
     
    BOOL CMsocsDlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
     
    	// Add "About..." menu item to system menu.
     
    	// IDM_ABOUTBOX must be in the system command range.
    	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    	ASSERT(IDM_ABOUTBOX < 0xF000);
     
    	CMenu* pSysMenu = GetSystemMenu(FALSE);
    	if (pSysMenu != NULL)
    	{
    		CString strAboutMenu;
    		strAboutMenu.LoadString(IDS_ABOUTBOX);
    		if (!strAboutMenu.IsEmpty())
    		{
    			pSysMenu->AppendMenu(MF_SEPARATOR);
    			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    		}
    	}
     
    	// 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
     
    	GetDlgItem(IDC_CLOSESOCK)->EnableWindow(FALSE);
     
    	// TODO: Add extra initialization here
     
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
     
    void CMsocsDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    	{
    		CAboutDlg dlgAbout;
    		dlgAbout.DoModal();
    	}
    	else
    	{
    		CDialog::OnSysCommand(nID, lParam);
    	}
    }
     
    // If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.
     
    void CMsocsDlg::OnPaint() 
    {
    	if (IsIconic())
    	{
    		CPaintDC dc(this); // device context for painting
     
    		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
     
    		// Center icon in client rectangle
    		int cxIcon = GetSystemMetrics(SM_CXICON);
    		int cyIcon = GetSystemMetrics(SM_CYICON);
    		CRect rect;
    		GetClientRect(&rect);
    		int x = (rect.Width() - cxIcon + 1) / 2;
    		int y = (rect.Height() - cyIcon + 1) / 2;
     
    		// Draw the icon
    		dc.DrawIcon(x, y, m_hIcon);
    	}
    	else
    	{
    		CDialog::OnPaint();
    	}
    }
     
    // The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CMsocsDlg::OnQueryDragIcon()
    {
    	return (HCURSOR) m_hIcon;
    }
     
    void CMsocsDlg::OnSendbtn() 
    {
    	UpdateData(TRUE);
     
    	if (m_strSend.GetLength() == 0) 
    	{
    		AfxMessageBox ("Please Type the Strings to Send");
    		return;
    	}
     
    	if (m_boolClient)
    	{
    		if ( m_pClientSock) 
    		{
     
    			if (m_pClientSock->Send((LPCTSTR)m_strSend, m_strSend.GetLength() +1) == SOCKET_ERROR)
    			{
    				wsprintf(m_szError, "Failed to send on client socket: %d", m_pClientSock->GetLastError());
    				m_pClientSock->Close();
    				delete m_pClientSock;
    				m_pClientSock = NULL;
    				AfxMessageBox (m_szError);
    				return;
    			}
    			m_strSend = "";
    			UpdateData(FALSE);
    		}
    		else
    		{
    			if ((m_pClientSock = new CClntSock(this)) == NULL) 
    			{
    				AfxMessageBox ("Failed to allocate client socket! Close and restart app.");
    				return;
    			}
     
    			if (!m_pClientSock->Create())
    			{
    				wsprintf(m_szError, "Failed to create client socket: %d! Close and restart app.", m_pClientSock->GetLastError());
    				AfxMessageBox (m_szError);
    				return;
    			}
     
    			if (!m_pClientSock->Connect(m_strServer, m_port))
    			{
    				wsprintf(m_szError, "Failed to connect: %d.", m_pClientSock->GetLastError());
    				AfxMessageBox (m_szError);
    				m_pClientSock->Close();
    				delete m_pClientSock;
    				m_pClientSock = NULL;
    				return;
    			}
     
    			GetDlgItem(IDC_CLOSESOCK)->EnableWindow(TRUE);
     
    			if (m_pClientSock->Send((LPCTSTR)m_strSend, m_strSend.GetLength() +1) == SOCKET_ERROR)
    			{
    				wsprintf(m_szError, "Failed to send on client socket: %d", m_pClientSock->GetLastError());
    				m_pClientSock->Close();
    				delete m_pClientSock;
    				m_pClientSock = NULL;
    				AfxMessageBox (m_szError);
    				return;
    			}
     
    			m_strSend = "";
    			UpdateData(FALSE);
    		}
    	}
    	else
    	{
    		if ( m_pSrvrSock) 
    		{
    			m_pSrvrSock->m_sendBuffer = m_strSend;
    			m_pSrvrSock->m_nBytesSent = 0;
    			m_pSrvrSock->m_nBytesBufferSize = m_strSend.GetLength() + 1;
    			m_pSrvrSock->DoAsyncSendBuff();
    			m_strSend = "";
    			UpdateData(FALSE);
    		}
    		else
    		{
    			AfxMessageBox ("Message socket not established yet");
    		}
    	}
    }
     
    void CMsocsDlg::OnCkclient() 
    {
    	m_boolClient= TRUE;
    	GetDlgItem(IDC_CHKSERVER)->EnableWindow(FALSE);
    }
     
    void CMsocsDlg::OnChkserver() 
    {
    	UpdateData(TRUE);
    	m_boolClient= FALSE;
    	GetDlgItem(IDC_CKCLIENT)->EnableWindow(FALSE);
    	GetDlgItem(IDC_RSVRNAME)->EnableWindow(FALSE);
     
    	if (m_pLstnSock == NULL) 
    	{
    		if ((m_pLstnSock = new CAsynlstn(this)) == NULL) 
    		{
    			AfxMessageBox ("Failed to allocate listen socket! Close and restart app.");
    			return;
    		}
     
    		if (!m_pLstnSock->Create(m_port) )
    		{
    			wsprintf(m_szError, "Failed to create listen socket: %d! Close and restart app.", m_pLstnSock->GetLastError());
    			AfxMessageBox (m_szError);
    			return;
    		}
     
    		if (!m_pLstnSock->Listen())
    		{
    			wsprintf(m_szError, "Failed to listen: %d! Close and restart app.", m_pLstnSock->GetLastError());
    			AfxMessageBox (m_szError);
    			return;
    		}
     
    	}
    }
     
     
    void CMsocsDlg::OnDestroy() 
    {
    	CDialog::OnDestroy();
     
    	if (m_pLstnSock) delete m_pLstnSock;	
    	if (m_pClientSock) delete m_pClientSock;	
    	if (m_pSrvrSock) delete m_pSrvrSock;	
    }
     
     
    void CMsocsDlg::OnClosesock() 
    {
    	if ( m_boolClient && m_pClientSock)
    	{
    		m_pClientSock->ShutDown();
    		m_pClientSock->Close();
    		delete m_pClientSock;
    		m_pClientSock = NULL;
    		m_strRecv = "";
    		m_strSend = "";
    		UpdateData(FALSE);
    		GetDlgItem(IDC_CLOSESOCK)->EnableWindow(FALSE);
    	}
     
    	if (!m_boolClient && m_pSrvrSock)
    	{
    		m_pSrvrSock->ShutDown();
    		m_pSrvrSock->Close();
    		delete m_pSrvrSock;
    		m_pSrvrSock = NULL;
    		m_strRecv = "";
    		m_strSend = "";
    		UpdateData(FALSE);	
    		GetDlgItem(IDC_CLOSESOCK)->EnableWindow(FALSE);
    	}
    }

    merci d'avance pour votre aide on ne vous le dira jamais assez

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    134
    Détails du profil
    Informations personnelles :
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations forums :
    Inscription : Mars 2006
    Messages : 134
    Par défaut
    salut j'ai trouver se que je cherche sur le net ya un super exemple de se que je voulait faire a cette adresse

    http://www.codeproject.com/internet/serversocket.asp

    c'est un super exmple d'appli utilisant au choix soit tcp soit udp

    jespere que sa servira a dautre @++

Discussions similaires

  1. Transmission d'un fichier en ftp mais en mode binary
    Par megamario dans le forum VB.NET
    Réponses: 17
    Dernier message: 13/03/2014, 15h02
  2. mode udp ou tcp problème
    Par john93 dans le forum C++
    Réponses: 4
    Dernier message: 22/06/2008, 15h45
  3. Réponses: 3
    Dernier message: 13/12/2007, 16h21
  4. Réponses: 3
    Dernier message: 24/03/2006, 20h46
  5. Réponses: 4
    Dernier message: 18/04/2005, 09h06

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo