je realise une application de type chet,client/serveur j'ai fait une librairie qui contient toutes les methodes ,j'ai suivi l'exemple du site.
http://www.supinfo-projects.com/fr/2004/socket%5Fdotnet/4/
J'ai deux formes,chaque contient deux riches textbox ,un bouton send et un bouton receive
Voici le code de la librairie.
public
class Class1
{
public Class1(
int port,String adressserveur)
{
//
// TODO: Add constructor logic here
//
_port=port;
_ipserveur=IPAddress.Parse(adressserveur);
_buffer=
new
byte[_buffersize];
_data=
new
byte[_databuffersize];
_endpoint=
new IPEndPoint(_ipserveur,port);
}
private Socket _Socketserveur;
private Socket _SocketClient;
private IPEndPoint _endpoint;
private IPAddress _ipserveur;
/// <summary>
/// numero de port de la communication
/// </summary>
private
int _port;
/// <summary>
/// buffer pour l'envoi et la reception
/// </summary>
/// <summary>
/// size du buffer
/// </summary>
///
private
int _databuffersize=512;
private
byte[] _data;
private
int _buffersize=256;
/// <summary>
/// autre buffer pour la serialisation
/// </summary>
/// <summary>
/// <summary>
/// buffer convertie en String
/// </summary>
private
byte[] _buffer;
public
byte[] Buffer
{
get
{
return _buffer;
}
set
{
_buffer=
value;
}
}
/// <summary>
/// buffer contenat le string d'un send ou d'un receive
/// </summary>
public
byte[] DataBuffer
{
get
{
return _data;
}
set
{
_data=
value;
}
}
/// <summary>
/// initialiser le socket serveur pour attendre chaque client
/// </summary>
public Socket SockPassive
{
get
{
return _Socketserveur;
}
set
{
_Socketserveur=
value;
}
}
/// <summary>
/// unique socket utiliser par le client pour dialoguer avec le serveur
/// </summary>
public Socket SockAct
{
get
{
return _SocketClient;
}
set
{
_SocketClient=
value;
}
}
/// <summary>
/// methode permettant au client de se connecter au Serveur
/// </summary>
public
void ClientConnect()
{
try
{
SockAct.Connect(_endpoint);
}
catch(SocketException ex){Erreur("SocketException",ex); }
}
/// <summary>
/// realisation de l'end Point
/// </summary>
public
void ServerBind()
{
try
{
SockPassive.Bind(_endpoint);
}
catch(SocketException ex){Erreur("SocketException",ex);}
}
public
void ServerListen()
{
try
{
this._Socketserveur.Listen(1);
}
catch(SocketException ex){Erreur("SocketException",ex);}
}
public
void ServerAccept()
{
try
{ SockPassive.Bind(_endpoint);
SockAct=SockPassive.Accept();
}
catch(SocketException ex){Erreur("SocketException",ex);}
}
/// <summary>
/// Fonctions Communes Creation de la Socket
/// </summary>
public
void CreateSocket(
bool active)
{
try
{
if(active)
{
_SocketClient=
new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
}
else
{
_Socketserveur=
new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
}
}
catch(SocketException ex){ Erreur("SocketException",ex);}
}
/// <summary>
/// Fonction Commune Envoi de Messages
/// </summary>
public
void SendMessage(
string message)
{
Buffer=ConversionByte(message);
SockAct.Send(Buffer,0,Buffer.Length,SocketFlags.None);
Buffer=
new
byte[_buffersize];
}
public
void ReceiveMessage()
{ Buffer=
new
byte[_buffersize];
SockAct.Receive(Buffer,0,Buffer.Length,SocketFlags.None);
}
public
void Send()
{
SockAct.Send(_data,0,_data.Length,SocketFlags.None);
_data=
new
byte[_databuffersize];
}
public
void Receive()
{
_data=
new
byte[_databuffersize];
SockAct.Receive(_data,0,_data.Length,SocketFlags.None);
}
public
void CloseSocket(Socket soc)
{ soc.Close();
if(soc.Connected){
Console.WriteLine("Socket OPen");
}
}
public
byte[] ConversionByte(
string message)
{
return System.Text.Encoding.ASCII.GetBytes(message);
}
public
string ConversionString(
byte[] message)
{
return System.Text.ASCIIEncoding.ASCII.GetString(message);
}
public
int DataBufferSize
{
get
{
return _databuffersize;
}
set
{
_databuffersize=
value;
}
}
/// <summary>
/// Reinitialisation de la taille du buffer
/// </summary>
public
int BufferSize
{
get
{
return _buffersize;
}
set
{
_buffersize=
value;
}
}
public
void Erreur(
string type, Exception e)
{
Console.WriteLine(e.Message);
}
}
Le code de la fenetre du serveur
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Button button1;
Class1 b;
bool act=
false;
private System.Windows.Forms.Button receive;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components =
null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
Class1 b=
new Class1(8000,"196.168.2.106");
b.CreateSocket(act);
//b.ServerBind();
b.ServerListen();
b.ServerAccept();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected
override
void Dispose(
bool disposing )
{
if( disposing )
{
if (components !=
null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private
void InitializeComponent()
{
this.groupBox1 =
new System.Windows.Forms.GroupBox();
this.richTextBox1 =
new System.Windows.Forms.RichTextBox();
this.richTextBox2 =
new System.Windows.Forms.RichTextBox();
this.button1 =
new System.Windows.Forms.Button();
this.receive =
new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(
this.richTextBox2);
this.groupBox1.Controls.Add(
this.richTextBox1);
this.groupBox1.Location =
new System.Drawing.Point(16, 24);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size =
new System.Drawing.Size(448, 248);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop =
false;
this.groupBox1.Text = "Envoi de Message";
//
// richTextBox1
//
this.richTextBox1.Location =
new System.Drawing.Point(8, 24);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size =
new System.Drawing.Size(424, 80);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// richTextBox2
//
this.richTextBox2.Location =
new System.Drawing.Point(8, 152);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size =
new System.Drawing.Size(424, 80);
this.richTextBox2.TabIndex = 1;
this.richTextBox2.Text = "";
//
// button1
//
this.button1.Location =
new System.Drawing.Point(480, 40);
this.button1.Name = "button1";
this.button1.Size =
new System.Drawing.Size(56, 48);
this.button1.TabIndex = 1;
this.button1.Text = "Send";
this.button1.Click +=
new System.EventHandler(
this.button1_Click);
//
// receive
//
this.receive.Location =
new System.Drawing.Point(472, 200);
this.receive.Name = "receive";
this.receive.Size =
new System.Drawing.Size(64, 40);
this.receive.TabIndex = 2;
this.receive.Text = "Receive";
this.receive.Click +=
new System.EventHandler(
this.receive_Click);
//
// Form1
//
this.AutoScaleBaseSize =
new System.Drawing.Size(5, 13);
this.ClientSize =
new System.Drawing.Size(544, 293);
this.Controls.Add(
this.receive);
this.Controls.Add(
this.button1);
this.Controls.Add(
this.groupBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load +=
new System.EventHandler(
this.Form1_Load);
this.groupBox1.ResumeLayout(
false);
this.ResumeLayout(
false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static
void Main()
{
Application.Run(
new Form1());
}
private
void Form1_Load(
object sender, System.EventArgs e)
{
}
private
void button1_Click(
object sender, System.EventArgs e)
{
b.SendMessage(richTextBox1.Text);
b.Send();
}
private
void receive_Click(
object sender, System.EventArgs e)
{
b.ReceiveMessage();
b.Receive();
}
}
Le code du client
private System.Windows.Forms.Button receive;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.RichTextBox richTextBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components =
null;
bool act=
true;
private System.Windows.Forms.Button button2;
Class1 b;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
Class1 b=
new Class1(8000,"192.168.2.106");
b.CreateSocket(act);
b.ClientConnect();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected
override
void Dispose(
bool disposing )
{
if( disposing )
{
if (components !=
null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private
void InitializeComponent()
{
this.receive =
new System.Windows.Forms.Button();
this.button1 =
new System.Windows.Forms.Button();
this.groupBox1 =
new System.Windows.Forms.GroupBox();
this.richTextBox2 =
new System.Windows.Forms.RichTextBox();
this.richTextBox1 =
new System.Windows.Forms.RichTextBox();
this.button2 =
new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// receive
//
this.receive.Location =
new System.Drawing.Point(464, 136);
this.receive.Name = "receive";
this.receive.Size =
new System.Drawing.Size(64, 40);
this.receive.TabIndex = 5;
this.receive.Text = "Receive";
this.receive.Click +=
new System.EventHandler(
this.receive_Click);
//
// button1
//
this.button1.Location =
new System.Drawing.Point(464, 32);
this.button1.Name = "button1";
this.button1.Size =
new System.Drawing.Size(56, 48);
this.button1.TabIndex = 4;
this.button1.Text = "Send";
this.button1.Click +=
new System.EventHandler(
this.button1_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(
this.richTextBox2);
this.groupBox1.Controls.Add(
this.richTextBox1);
this.groupBox1.Location =
new System.Drawing.Point(8, 16);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size =
new System.Drawing.Size(448, 248);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop =
false;
this.groupBox1.Text = "Envoi de Message";
//
// richTextBox2
//
this.richTextBox2.Location =
new System.Drawing.Point(8, 152);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size =
new System.Drawing.Size(424, 80);
this.richTextBox2.TabIndex = 1;
this.richTextBox2.Text = "";
//
// richTextBox1
//
this.richTextBox1.Location =
new System.Drawing.Point(8, 24);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size =
new System.Drawing.Size(424, 80);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// button2
//
this.button2.Location =
new System.Drawing.Point(464, 216);
this.button2.Name = "button2";
this.button2.Size =
new System.Drawing.Size(64, 40);
this.button2.TabIndex = 6;
this.button2.Text = "Fermer";
this.button2.Click +=
new System.EventHandler(
this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize =
new System.Drawing.Size(5, 13);
this.ClientSize =
new System.Drawing.Size(536, 301);
this.Controls.Add(
this.button2);
this.Controls.Add(
this.receive);
this.Controls.Add(
this.button1);
this.Controls.Add(
this.groupBox1);
this.Name = "Form1";
this.Text = "Client";
this.Load +=
new System.EventHandler(
this.Form1_Load);
this.groupBox1.ResumeLayout(
false);
this.ResumeLayout(
false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static
void Main()
{
Application.Run(
new Form1());
}
private
void button1_Click(
object sender, System.EventArgs e)
{
b.SendMessage(richTextBox1.Text);
b.Send();
}
private
void Form1_Load(
object sender, System.EventArgs e)
{
}
private
void receive_Click(
object sender, System.EventArgs e)
{
b.ReceiveMessage();
b.Receive();
}
private
void button2_Click(
object sender, System.EventArgs e)
{
b.CloseSocket();
}
}
FD
Afficher la suite