débu24
Messages postés12Date d'inscriptionjeudi 24 avril 2014StatutMembreDernière intervention11 juin 2014
-
Modifié par Whismeril le 13/05/2014 à 14:27
Whismeril
Messages postés19145Date d'inscriptionmardi 11 mars 2003StatutContributeurDernière intervention 3 octobre 2024
-
13 mai 2014 à 14:27
Bonjour,
j'ai crée 2 projet en visual studio 2012 client/serveur.
lorsque j'écrie client.send() il envoie un text comme un chat est lorsque j'écrie client.sendFile()
le clienat déconnecte
code sevreur:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using NetworksApi.TCP.SERVER;
using System.Data.SqlClient;
using System.IO;
using System.Security;
namespace serveur
{
public partial class Form1 : Form
{
OpenFileDialog dlg = new OpenFileDialog();
System.Diagnostics.EventLog appLog = new System.Diagnostics.EventLog() ;
public delegate void updatechat(string text);
public delegate void updateliste(ListBox box, string value,bool remouve);
Server serveur;
DataTable kes = new DataTable();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
serveur = new Server("127.0.0.1", "90");
serveur.OnClientConnected +=new OnConnectedDelegate( serveur_OnClientConnected);
serveur.OnClientDisconnected +=new OnDisconnectedDelegate( serveur_OnClientDisconnected);
serveur.OnDataReceived += new OnReceivedDelegate( serveur_OnDataReceived);
serveur.OnServerError += new OnErrorDelegate( serveur_OnServerError);
serveur.Start();
try
{
ConnexionServeur.ouvrir();
MessageBox.Show(ConnexionServeur.Cn.State.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
kes = ConexServeur.listefichier(); ;
}
private void changertext(string text)
{
if (textBox1.InvokeRequired)
{
Invoke(new updatechat(changertext), new object[] { text });
}
else
{
textBox1.Text += text + "\r\n";
}
}
private void changelistbox(ListBox box, string value, bool remouve)
{
if (box.InvokeRequired)
{
Invoke(new updateliste(changelistbox), new object[] { box, value,remouve });
}
else
{
if (remouve)
{
box.Items.Remove(value);
}
else
{
box.Items.Add(value);
}
}
}
void serveur_OnServerError(object Sender, ErrorArguments R)
{
MessageBox.Show(R.ErrorMessage);
}
void serveur_OnDataReceived(object Sender, ReceivedArguments R)
{
changertext(R.ReceivedData);
serveur.BroadCast(R.Name+"a envoyer : "+ R.ReceivedData);
}
void serveur_OnClientDisconnected(object Sender, DisconnectedArguments R)
{
serveur.BroadCast(R.Name + "est déconnecté");
changelistbox(listBox1, R.Name, true);
changelistbox(listBox2, R.Ip, true);
}
void serveur_OnClientConnected(object Sender, ConnectedArguments R)
{
serveur.BroadCast(R.Name + "est connecté");
changelistbox(listBox1, R.Name, false);
changelistbox(listBox2, R.Ip, false);
}
private void button1_Click(object sender, EventArgs e)
{
serveur.SendTo((string)listBox1.SelectedItem, textBox2.Text);
}
private void button2_Click(object sender, EventArgs e)
{
serveur.BroadCast(textBox2.Text);
}
private void button3_Click(object sender, EventArgs e)
{
serveur.DisconnectClient((string)listBox1.SelectedItem);
}
private void button4_Click(object sender, EventArgs e)
{
try
{
ConexServeur re = new ConexServeur(textBox3.Text);
re.ajouter();
kes = ConexServeur.listefichier();
String lines = (File.ReadAllText(dlg.FileName.ToString()));
textBox3.Text = lines;
textBox1.Clear();
MessageBox.Show("insertion de fichier avec succé");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox2.SelectedIndex = listBox1.SelectedIndex;
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.SelectedIndex = listBox2.SelectedIndex;
}
}
}
code client :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using NetworksApi.TCP.CLIENT;
namespace client
{
public partial class Form1 : Form
{
OpenFileDialog dlg = new OpenFileDialog();
public delegate void updatetext(string text);
Client client;
private void changetext(string text)
{
if (textBox3.InvokeRequired)
{
Invoke(new updatetext(changetext), new object[] { text });
}
else
{
textBox3.Text += text + "\r\n";
}
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text!="" && textBox2.Text!="" && textBox5.Text!="")
{
client = new Client();
client.ClientName = textBox1.Text;
client.ServerIp = textBox2.Text;
client.ServerPort = textBox5.Text;
client.OnClientConnected +=new OnClientConnectedDelegate( client_OnClientConnected);
client.OnClientConnecting +=new OnClientConnectingDelegate (client_OnClientConnecting);
client.OnClientDisconnected += new OnClientDisconnectedDelegate( client_OnClientDisconnected);
client.OnClientError += new OnClientErrorDelegate( client_OnClientError);
client.OnClientFileSending +=new OnClientFileSendingDelegate( client_OnClientFileSending);
client.OnDataReceived +=new OnClientReceivedDelegate( client_OnDataReceived);
client.Connect();
}
else
{
MessageBox.Show("il faut remplir toutes les 3 permiéres colone");
}
}
void client_OnDataReceived(object Sender, ClientReceivedArguments R)
{
changetext(R.ReceivedData);
}
void client_OnClientFileSending(object Sender, ClientFileSendingArguments R)
{
}
void client_OnClientError(object Sender, ClientErrorArguments R)
{
changetext(R.ErrorMessage);
}
void client_OnClientDisconnected(object Sender, ClientDisconnectedArguments R)
{
changetext(R.EventMessage);
}
void client_OnClientConnecting(object Sender, ClientConnectingArguments R)
{
changetext(R.EventMessage);
}
void client_OnClientConnected(object Sender, ClientConnectedArguments R)
{
changetext(R.EventMessage);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
}
private void button1_Click(object sender, EventArgs e)
{
if (client !=null && client.IsConnected)
{
client.SendFile(textBox4.Text);
textBox4.Clear();
}
MessageBox.Show("envoyé");
}
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button5_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "fichier text (*.txt)|*.txt";
dlg.RestoreDirectory = true;
dlg.Title = "document a envoyer ";
if (dlg.ShowDialog() == DialogResult.OK)
{
textBox4.Text = dlg.FileName;
}
else
{
textBox4.Text = "chemain incorect";
return;
}
}
}
}