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

VC++ .NET Discussion :

Socket en C++


Sujet :

VC++ .NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre habitué
    Profil pro
    Étudiant
    Inscrit en
    Décembre 2006
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2006
    Messages : 9
    Par défaut Socket en C++
    Bonjour à tous

    Voila j'essaie de créer une messagerie intranet. J'ai créé en ligne de commande un serveur qui recoit des informations et envoie un espece d'accusé de réception. La partie client marche aussi en ligne de commande mais lorsque j'ai voulu passer en version IHM. J'utilise Visual studio 2003. J'ai réussi à envoyer des informations (ce que j'ai écrit dans un textBox) vers le serveur mais je ne recoit pas l'information de la part du serveur. Il m'affiche une erreur d'envoie d'information.

    Je voudrais savoir comment afficher l'information venant du serveur dans un textBox!

    je vous remercie pour votre aide.

    Voici le code pour la partie client.

    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
     
    #pragma once
    #include<string>
    #include <winsock2.h> 
    #pragma comment(lib, "ws2_32.lib")
     
     
     
    namespace Test
    {
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
     
    	/// <summary> 
    	/// Description résumée de Form1
    	///
    	/// AVERTISSEMENT : Si vous modifiez le nom de cette classe, vous devrez également modifier la 
    	///          propriété 'Nom du fichier de ressources' de l'outil de compilation de ressource managée 
    	///          pour tous les fichiers .resx dont dépend cette classe. Dans le cas contraire,
    	///          les concepteurs ne pourront pas interagir correctement avec les ressources
    	///          localisées associées à ce formulaire.
    	/// </summary>
    	public __gc class Form1 : public System::Windows::Forms::Form
    	{	
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    		}
     
    		SOCKET sock;
     
    	protected:
    		void Dispose(Boolean disposing)
    		{
    			if (disposing && components)
    			{
    				components->Dispose();
    			}
    			__super::Dispose(disposing);
    		}
     
    	private: System::Windows::Forms::TextBox *  textBox1;
    	private: System::Windows::Forms::MainMenu *  mainMenu1;
     
    	private: System::Windows::Forms::TextBox *  textBox2;
    	private: System::Windows::Forms::TextBox *  textBox3;
    	private: System::Windows::Forms::Button *  button3;
    	private: System::Windows::Forms::TextBox *  textBox4;
     
    	private: System::Windows::Forms::TextBox *  textBox5;
     
    	private: System::Windows::Forms::Button *  button2;
    	private: System::Windows::Forms::Button *  button1;
     
    	private: System::String * historic;
    	private: System::Windows::Forms::TextBox *  textBox6;
    	private: System::Windows::Forms::MenuItem *  menuItem1;
    	private: System::Windows::Forms::MenuItem *  menuItem2;
     
    			 //	string ps2;
     
    	private:
    		/// <summary>
    		/// Variable nécessaire au concepteur.
    		/// </summary>
    		System::ComponentModel::Container * components;
     
    		/// <summary>
    		/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
    		/// le contenu de cette méthode avec l'éditeur de code.
    		/// </summary>
     
    		void InitializeComponent(void)
    		{
    			this->button1 = new System::Windows::Forms::Button();
    			this->textBox1 = new System::Windows::Forms::TextBox();
    			this->mainMenu1 = new System::Windows::Forms::MainMenu();
    			this->menuItem1 = new System::Windows::Forms::MenuItem();
    			this->menuItem2 = new System::Windows::Forms::MenuItem();
    			this->button2 = new System::Windows::Forms::Button();
    			this->textBox2 = new System::Windows::Forms::TextBox();
    			this->textBox3 = new System::Windows::Forms::TextBox();
    			this->button3 = new System::Windows::Forms::Button();
    			this->textBox4 = new System::Windows::Forms::TextBox();
    			this->textBox5 = new System::Windows::Forms::TextBox();
    			this->textBox6 = new System::Windows::Forms::TextBox();
    			this->SuspendLayout();
    			// 
    			// button1
    			// 
    			this->button1->Cursor = System::Windows::Forms::Cursors::Default;
    			this->button1->ForeColor = System::Drawing::Color::Red;
    			this->button1->Location = System::Drawing::Point(480, 424);
    			this->button1->Name = S"button1";
    			this->button1->Size = System::Drawing::Size(80, 48);
    			this->button1->TabIndex = 0;
    			this->button1->Text = S"Quitter";
    			this->button1->Click += new System::EventHandler(this, button1_Click);
    			// 
    			// textBox1
    			// 
    			this->textBox1->Location = System::Drawing::Point(8, 384);
    			this->textBox1->Multiline = true;
    			this->textBox1->Name = S"textBox1";
    			this->textBox1->Size = System::Drawing::Size(288, 88);
    			this->textBox1->TabIndex = 1;
    			this->textBox1->Text = S"";
    			this->textBox1->TextChanged += new System::EventHandler(this, textBox1_TextChanged);
    			// 
    			// mainMenu1
    			// 
    			System::Windows::Forms::MenuItem* __mcTemp__1[] = new System::Windows::Forms::MenuItem*[1];
    			__mcTemp__1[0] = this->menuItem1;
    			this->mainMenu1->MenuItems->AddRange(__mcTemp__1);
    			// 
    			// menuItem1
    			// 
    			this->menuItem1->Index = 0;
    			System::Windows::Forms::MenuItem* __mcTemp__2[] = new System::Windows::Forms::MenuItem*[1];
    			__mcTemp__2[0] = this->menuItem2;
    			this->menuItem1->MenuItems->AddRange(__mcTemp__2);
    			this->menuItem1->Text = S"Fichier";
    			// 
    			// menuItem2
    			// 
    			this->menuItem2->Index = 0;
    			this->menuItem2->Text = S"Quitter";
    			this->menuItem2->Click += new System::EventHandler(this, menuItem2_Click);
    			// 
    			// button2
    			// 
    			this->button2->ForeColor = System::Drawing::Color::Green;
    			this->button2->Location = System::Drawing::Point(320, 424);
    			this->button2->Name = S"button2";
    			this->button2->Size = System::Drawing::Size(112, 48);
    			this->button2->TabIndex = 2;
    			this->button2->Text = S"Envoyer";
    			this->button2->Click += new System::EventHandler(this, button2_Click);
    			// 
    			// textBox2
    			// 
    			this->textBox2->Location = System::Drawing::Point(320, 56);
    			this->textBox2->Name = S"textBox2";
    			this->textBox2->Size = System::Drawing::Size(240, 20);
    			this->textBox2->TabIndex = 3;
    			this->textBox2->Text = S"127.0.0.1";
    			// 
    			// textBox3
    			// 
    			this->textBox3->Location = System::Drawing::Point(8, 8);
    			this->textBox3->Multiline = true;
    			this->textBox3->Name = S"textBox3";
    			this->textBox3->ReadOnly = true;
    			this->textBox3->Size = System::Drawing::Size(288, 360);
    			this->textBox3->TabIndex = 4;
    			this->textBox3->Text = S"";
    			this->textBox3->TextChanged += new System::EventHandler(this, textBox3_TextChanged);
    			// 
    			// button3
    			// 
    			this->button3->ForeColor = System::Drawing::Color::Blue;
    			this->button3->Location = System::Drawing::Point(320, 80);
    			this->button3->Name = S"button3";
    			this->button3->Size = System::Drawing::Size(104, 40);
    			this->button3->TabIndex = 5;
    			this->button3->Text = S"Connection";
    			this->button3->Click += new System::EventHandler(this, button3_Click);
    			// 
    			// textBox4
    			// 
    			this->textBox4->Location = System::Drawing::Point(320, 8);
    			this->textBox4->Name = S"textBox4";
    			this->textBox4->Size = System::Drawing::Size(240, 20);
    			this->textBox4->TabIndex = 6;
    			this->textBox4->Text = S"pseudo";
    			this->textBox4->TextChanged += new System::EventHandler(this, textBox4_TextChanged);
    			// 
    			// textBox5
    			// 
    			this->textBox5->Location = System::Drawing::Point(432, 88);
    			this->textBox5->Name = S"textBox5";
    			this->textBox5->Size = System::Drawing::Size(128, 20);
    			this->textBox5->TabIndex = 8;
    			this->textBox5->Text = S"connection ...";
    			// 
    			// textBox6
    			// 
    			this->textBox6->Location = System::Drawing::Point(320, 152);
    			this->textBox6->Multiline = true;
    			this->textBox6->Name = S"textBox6";
    			this->textBox6->Size = System::Drawing::Size(240, 232);
    			this->textBox6->TabIndex = 9;
    			this->textBox6->Text = S"Clients Connectés :";
    			// 
    			// Form1
    			// 
    			this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
    			this->ClientSize = System::Drawing::Size(576, 486);
    			this->Controls->Add(this->textBox6);
    			this->Controls->Add(this->textBox5);
    			this->Controls->Add(this->textBox4);
    			this->Controls->Add(this->textBox3);
    			this->Controls->Add(this->textBox2);
    			this->Controls->Add(this->textBox1);
    			this->Controls->Add(this->button3);
    			this->Controls->Add(this->button2);
    			this->Controls->Add(this->button1);
    			this->Cursor = System::Windows::Forms::Cursors::Default;
    			this->Menu = this->mainMenu1;
    			this->Name = S"Form1";
    			this->Text = S"Form1";
    			this->Load += new System::EventHandler(this, Form1_Load);
    			this->ResumeLayout(false);
     
    		}	
    	private: System::Void pictureBox1_Click(System::Object *  sender, System::EventArgs *  e)
    			 {
     
    			 }
     
    	private: System::Void textBox1_TextChanged(System::Object *  sender, System::EventArgs *  e)//textBox ou on ecrit le message
    			 {
     
    			 }
     
    private: System::Void textBox3_TextChanged(System::Object *  sender, System::EventArgs *  e)
    		 {
     
    		 }
     
     
    private: System::Void button2_Click(System::Object *  sender, System::EventArgs *  e)//button envoyer
    		 {	
    			WSADATA WSAData;
    			WSAStartup(MAKEWORD(2,0), &WSAData);
    			SOCKET sock;
    			SOCKADDR_IN sin;
    			sock = socket(AF_INET, SOCK_STREAM, 0);
     
    			System::String *ip=textBox2->get_Text();// on met dans le textboxIP l'adresse IP
    			CString strip(ip);//transformation d'un string en char
     
    			sin.sin_addr.s_addr			= inet_addr(strip);//mettre l'adrese IP du serveur
    			sin.sin_family				= AF_INET;
    			sin.sin_port				= htons(5555);//mettre le port correspondant
     
    			System::String *str=textBox1->get_Text ();
     
    			CString str1(str);	//conversion string "managé" en char
    			if(connect(sock, (SOCKADDR *)&sin, sizeof(sin))<0)
    			{
    				textBox3->AppendText ("Veuillez vérifier l'adresse ip du serveur!");	
    			}
    			else
     
    			if(send(sock, str1, 255, 0)<0)
    			{
    				textBox3->AppendText ("Erreur envoie");
    			}
    			else
    			{
    				send(sock, str1, 255, 0);
    				textBox3->AppendText (textBox4->get_Text ());//pour faire passer une saisie d'un texte a un autre
    				textBox3->AppendText (" : ");				 //
    				textBox3->AppendText (textBox1->get_Text ());//
    				textBox3->AppendText ("\n");				 //
    			}
     
    			textBox1->Clear();//efface la saisie apres envoie
    			WSACleanup();
    		 }
     
    private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)//button exit
    		 {
    			 exit(-1);
    		 }
     
    private: System::Void textBox4_TextChanged(System::Object *  sender, System::EventArgs *  e)
    		 {
     
    		 }
     
    private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
    		 {
     
    		 }
     
    	private: System::Void button4_Click(System::Object *  sender, System::EventArgs *  e)
    			 {
     
    			 }
     
     
    private: System::Void button3_Click(System::Object *  sender, System::EventArgs *  e)  //button conexion
    		 {
    			WSADATA WSAData;
    			WSAStartup(MAKEWORD(2,0), &WSAData);
    			sock = socket(AF_INET, SOCK_STREAM, 0);
     
    			System::String *ip=textBox2->get_Text();
    			CString strip(ip);
    			SOCKADDR_IN sin;
     
    			sin.sin_addr.s_addr			= inet_addr(strip);//mettre l'adrese IP du serveur
    			sin.sin_family				= AF_INET;
    			sin.sin_port				= htons(5555);//mettre le port correspondant
    			if(connect(sock, (SOCKADDR*)&sin, sizeof(sin))<0)
    			{
    				textBox5->Clear ();
    				textBox5->AppendText ("ERREUR DE CONNEXION");
    			}
    			else
    			{
    				textBox5->Clear ();
    				textBox5->AppendText ("CONNEXION ACEPTE");
    			}
     
     
    			WSACleanup();
    		 }
    private: System::Void menuItem2_Click(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 exit(-1);
    		 }
     
    };
    }

  2. #2
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Par défaut
    c'est quoi le pb ? ca n'affiche pas le résultat escompté ou bien il y a une erreur ?
    es-tu en unicode ?

  3. #3
    Membre habitué
    Profil pro
    Étudiant
    Inscrit en
    Décembre 2006
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2006
    Messages : 9
    Par défaut
    Le problème est que je n'arrive pas recevoir l'information envoyé par mon serveur. Je ne sais pas où mettre mon code pour qu'il écoute le port en question et qu'il m'affiche le message recu.

    Et sur le serveur qui est en ligne de commande j'ai un message de erreur d'envoie.

  4. #4
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Par défaut
    en général, l'écoute se fait dans un thread à part
    mais pourquoi ne pas utiliser les socket de .net ? et pourquoi pas vs2005 qui est vraiment mieux pour le C++.Net

  5. #5
    Membre habitué
    Profil pro
    Étudiant
    Inscrit en
    Décembre 2006
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2006
    Messages : 9
    Par défaut
    Le problème c'est qu'on me demande de le faire avec le visual studio 2003. Ce projet est dans le cadre scolaire(B.T.S. IRIS).

    En ce qui concerne le Thread je le mets où? Dans le constructeur ou dans le textbox?

  6. #6
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Par défaut
    ben, ca dépend le principe de ta messagerie, mais si tu veux recevoir à un moment précis (genre, clic sur un bouton bloquant), tu n'as pas besoin de thread, mais si tu veux que ce soit en continue et non bloquant l'ihm, il te faut un thread, qui peut etre crée dans le constructeur ou dans le form_load

Discussions similaires

  1. executer une application a distance : Sockets ? RPC ? CORBA?
    Par a_hic dans le forum Développement
    Réponses: 5
    Dernier message: 30/05/2006, 13h02
  2. raw socket et langage c
    Par SlayDave dans le forum Développement
    Réponses: 2
    Dernier message: 29/08/2002, 19h09
  3. socket
    Par ben91 dans le forum Développement
    Réponses: 5
    Dernier message: 13/08/2002, 11h04
  4. Socket:Envoyer du texte d'un serveur vers tout les clients
    Par cedm78 dans le forum Web & réseau
    Réponses: 7
    Dernier message: 01/08/2002, 16h40
  5. transfert d'un fichier bitmap en socket tcp
    Par localhost dans le forum C++Builder
    Réponses: 5
    Dernier message: 29/07/2002, 00h40

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