000
23.02.2006, 16:33 Uhr
~hallo
Gast
|
hallo!
bin gerade dran n chat zu programmieren. bis jetzt kann der server empfangen und der client senden. ich habs jetzt lange versucht, dass der server auch senden kann, aber es funktioniert nicht hier zeige ich euch mal die codes:
mfg
Serverseitiger Code
C++: |
public class Form1 : System.Windows.Forms.Form { TcpListener ls = new TcpListener(4711); ...........
|
C++: |
static void Main() { Application.Run(new Form1()); }
private void chat() { string lese = "x"; string nick = "leer"; int a = 0; ls.Start(); Socket su = ls.AcceptSocket();
while(true) { NetworkStream ns = new NetworkStream(su); StreamWriter sw = new StreamWriter(ns); StreamReader sr = new StreamReader(ns); if (su.Connected) { if (a == 0) { gotmsg.Text = "Client verbunden" + "\n" + "\n"; a++; } if (a == 1) { string ipclient = " "; ipclient = ((IPEndPoint)su.RemoteEndPoint).Address.ToString(); lese = sr.ReadLine(); nick = lese; user.Text = nick + " " +ipclient; a++; } sw.WriteLine("Server hat Connection erlaubt - Sie sind verbunden"); lese = sr.ReadLine(); gotmsg.Text += lese +"\n"; } } } private void button1_Click(object sender, System.EventArgs e) { new Thread(new ThreadStart(chat)).Start(); } private void menuItem10_Click(object sender, System.EventArgs e) { StreamWriter ws = File.CreateText("C:\\log.txt"); ws.WriteLine(gotmsg.Text); ws.Close(); }
private void menuItem2_Click(object sender, System.EventArgs e) { string strLine = " "; string lesen = " "; try { StreamReader stream = new StreamReader("C:\\log.txt"); while ((strLine = stream.ReadLine()) != null) { lesen += strLine + "\n"; } stream.Close(); gotmsg.Text = lesen; } catch { MessageBox.Show("Es wurde kein Verlauf gefunden", "Fehler"); } } public void sendens() { ls.Start(); Socket soki = ls.AcceptSocket(); NetworkStream ntwk = new NetworkStream(soki); StreamWriter sw2 = new StreamWriter(ntwk);
sw2.WriteLine("H a l l o"); sw2.Flush(); } private void send_Click(object sender, System.EventArgs e) { new Thread(new ThreadStart(sendens)).Start(); }
private void menuItem11_Click(object sender, System.EventArgs e) { string Host = Dns.GetHostName(); string IP = Dns.GetHostByName(Host).AddressList[0].ToString(); MessageBox.Show("IP: " + IP + " Host: " + Host); }
|
|