[C#] Problème client-serveur TCP
Bonjour à tous !
J'ai un petit problème lors du retour de réponse d'un serveur à un client.
J'ai deux fenêtres, une fenêtre qui symbolise le serveur et une le client.
Voilà le code du serveur et du client:
Le serveur:
Code:
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
|
namespace WindowsApplication1
{
public partial class serveur : Form
{
public string DEMANDE;
TcpClient client = null;
public serveur()
{
InitializeComponent();
}
private void StartServeur()
{
IPHostEntry host = new IPHostEntry();
IPAddress[] IPS = host.AddressList;
int PortEcoute = 9567;
TcpListener S = new TcpListener(PortEcoute);
S.Start();
bool End = false;
while (!End)
{
client=S.AcceptTcpClient();
Thread T1 = new Thread(new Service(client).Run);
T1.Start();
}
}
private void Throw_Serveur(object sender, EventArgs e)
{
Thread T0 = new Thread(new ThreadStart(StartServeur));
T0.Start();
}
}
} |
et a classe service:
Code:
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
|
namespace WindowsApplication1
{
class Service
{
TcpClient LiaisonClient;
public Service(TcpClient LiaisonClient)
{
this.LiaisonClient = LiaisonClient;
}
public void Run()
{
StreamReader str = new StreamReader(LiaisonClient.GetStream());
StreamWriter stw = new StreamWriter(LiaisonClient.GetStream());
string demande;
string reponse;
demande = str.ReadLine();
reponse = demande;
stw.WriteLine(reponse);
MessageBox.Show(demande);
}
}
} |
Et le client:
Code:
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
|
namespace WindowsApplication1
{
public partial class client : Form
{
public string demande;
public string reponse;
public bool fini = false;
public TcpClient C;
public StreamReader In;
public StreamWriter Out;
public client()
{
InitializeComponent();
this.C = new TcpClient("127.0.0.1",9567);
this.In = new StreamReader(C.GetStream());
this.Out = new StreamWriter(C.GetStream());
this.Out.AutoFlush = true;
}
public void Envoi(string Text)
{
this.demande = Text;
Out.WriteLine(demande);
reponse = In.ReadLine();
label1.Text = label1.Text + reponse.ToString() + "\n";
}
private void button1_Click(object sender, EventArgs e)
{
Envoi(this.textBox1.Text.ToString());
}
}
} |
Mon problème est le suivant:
- Lorsque je ne renvois pas de réponse au client (quand il m'envoit un message) (et donc lorsque je n'attends pas de réponse su côté client) Tout marche bien , pas de problème
- Mais lorsque j'attends une réponse du côté client, je suis complétement bloqué ! mes fenêtre se bloquent ...
Comment remédier à ce problème ? Faut il que j'attende la réponse du côté client dans un nouveau thread ???
Meric pour vos réponses !!!!