Mr Burns Skrevet 21. mars 2007 Del Skrevet 21. mars 2007 (endret) Heisann! Problemet er at jeg får sendt data i fra klient,men ikke fra server til klient. Her er koden: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; using System.IO; namespace TCPServerClient { public partial class Form1 : Form { TcpListener listener; TcpClient sWorker; NetworkStream serverStream; byte[] ServerReceiveBuffer; TcpClient client; NetworkStream clientStream; byte[] clientReceiveBuffer; public Form1() { InitializeComponent(); listener = new TcpListener(IPAddress.Any, 1234); client = new TcpClient();; } #region Server - side private void onAccept(IAsyncResult ar) { try { sWorker = listener.EndAcceptTcpClient(ar); ServerReceiveBuffer = new byte[sWorker.ReceiveBufferSize]; this.Invoke(new EventHandler(delegate { textBox1.Text += "Connection established!\r\n"; } )); serverStream = sWorker.GetStream(); serverStream.BeginRead(ServerReceiveBuffer, 0, sWorker.ReceiveBufferSize, readServerRcvBuffer, null); } catch (ObjectDisposedException ode) { } } private void readServerRcvBuffer(IAsyncResult ar) { try { int iRx = 0; iRx = serverStream.EndRead(ar); this.Invoke(new EventHandler(delegate { textBox1.Text += System.Text.Encoding.Default.GetString(ServerReceiveBuffer, 0, iRx) + "\r\n"; // ServerReceiveBuffer.ToString() + "\r\n"; })); serverStream.BeginRead(ServerReceiveBuffer, 0, sWorker.ReceiveBufferSize, readServerRcvBuffer, null); } catch (ObjectDisposedException ode) { } catch (IOException ioe) { } } private void startServerButton_Click(object sender, EventArgs e) { listener.Start(); listener.BeginAcceptTcpClient(onAccept, null); textBox1.Text += "Server started!\r\n"; } private void stopServerButton_Click(object sender, EventArgs e) { if (serverStream != null) { serverStream.Flush(); serverStream.Close(); } if (sWorker != null) sWorker.Close(); listener.Stop(); } private void sendServerButton_Click(object sender, EventArgs e) { if (serverStream != null) { byte[] test = new byte[textBox4.Text.Length]; test = System.Text.Encoding.Default.GetBytes(textBox4.Text); serverStream.Write(test, 0, textBox4.Text.Length); } } #endregion #region Client - side private void connectButton_Click(object sender, EventArgs e) { textBox2.Text += "Connecting to server...\r\n"; try { client.Connect("localhost", 1234); textBox2.Text += "Connected to server!\r\n"; clientStream = client.GetStream(); clientReceiveBuffer = new byte[client.ReceiveBufferSize]; clientStream.BeginRead(clientReceiveBuffer, 0, client.ReceiveBufferSize, readClientRcvBuffer, clientStream); } catch (SocketException se) { textBox2.Text += "Connection failed!\r\n"; } } private void disconnectButton_Click(object sender, EventArgs e) { if (clientStream != null) clientStream.Close(); client.Close(); } private void sendClientButton_Click(object sender, EventArgs e) { if (clientStream != null) { byte[] test = new byte[textBox5.Text.Length]; test = System.Text.Encoding.Default.GetBytes(textBox5.Text); clientStream.Write(test, 0, textBox5.Text.Length); } } private void readClientRcvBuffer(IAsyncResult ar) { if (clientStream != null) { int iRx = 0; iRx = clientStream.EndRead(ar); this.Invoke(new EventHandler(delegate { textBox2.Text += System.Text.Encoding.Default.GetString(clientReceiveBuffer, 0, iRx) + "\r\n"; // ServerReceiveBuffer.ToString() + "\r\n"; })); clientStream.BeginRead(clientReceiveBuffer, 0, client.ReceiveBufferSize, readClientRcvBuffer, null); } } private void textBox1_TextChanged(object sender, EventArgs e) { textBox1.SelectionStart = textBox1.Text.Length; textBox1.ScrollToCaret(); } #endregion } } Endret 21. mars 2007 av Mr Burns Lenke til kommentar
Mr Burns Skrevet 21. mars 2007 Forfatter Del Skrevet 21. mars 2007 Nevermind... Jeg hadde rota det til med å lime inn noe fra serversiden i klientkoden. Fikset nå. Forsåvidt er jeg usikker på hvordan jeg skal håndtere at server eller klient kobler ned forbindelsen. Lenke til kommentar
Anbefalte innlegg
Opprett en konto eller logg inn for å kommentere
Du må være et medlem for å kunne skrive en kommentar
Opprett konto
Det er enkelt å melde seg inn for å starte en ny konto!
Start en kontoLogg inn
Har du allerede en konto? Logg inn her.
Logg inn nå