Server

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.Threading;
using System.Net;

namespace Server
{
    public partial class Form1 : Form
    {

        private TcpListener tcpListener;
        private Thread listenThread;
        static NetworkStream clientStream;

        public Form1()
        {
            InitializeComponent();
            this.tcpListener = new TcpListener(IPAddress.Any, 3000);
            this.listenThread = new Thread(new ThreadStart(ListenForClients));
            this.listenThread.Start();
        }

        private void ListenForClients()
        {
            this.tcpListener.Start();

            while (true)
            {
                //blocks until a client has connected to the server
                TcpClient client = this.tcpListener.AcceptTcpClient();

                //create a thread to handle communication 
                //with connected client
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);
            }
        }

        private void HandleClientComm(object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();

            byte[] message = new byte[4096];
            int bytesRead;

            while (true)
            {
                bytesRead = 0;

                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, 4096);
                }
                catch
                {
                    //a socket error has occured
                    break;
                }

                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }

                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();
                System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
            }

            tcpClient.Close();
        }



        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] buffer = encoder.GetBytes(textBox1.Text);

            clientStream.Write(buffer, 0, buffer.Length);
            clientStream.Flush();
        }
    }
}

Client

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.Threading;
using System.Net;


namespace Client
{
    public partial class Form1 : Form
    {
            static TcpClient client;
            IPEndPoint serverEndPoint;
            NetworkStream clientStream;
            static String textFromServer;

            public Form1()
            {
                InitializeComponent();
                iniTCP();
            }

            public void iniTCP()
            {
                client = new TcpClient();
                serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000);
            }

            public void connectTCP()
            {
                client.Connect(serverEndPoint);
                clientStream = client.GetStream();

                // Create a thread to handle communication 
                // with connected client
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);

            }

            private static void HandleClientComm(object client)
            {
                TcpClient tcpClient = (TcpClient)client;
                NetworkStream clientStream = tcpClient.GetStream();

                byte[] message = new byte[4096];
                int bytesRead;

                do
                {
                    bytesRead = 0;

                    try
                    {
                        // Blocks until a client sends a message                    
                        bytesRead = clientStream.Read(message, 0, 4096);
                    }
                    catch (Exception)
                    {
                        // A socket error has occured
                        break;
                    }

                    if (bytesRead == 0)
                    {
                        // The client has disconnected from the server
                        break;
                    }

                    // Message has successfully been received
                    ASCIIEncoding encoder = new ASCIIEncoding();

                    // Output message
                    //textFromServer = tcpClient.Client.LocalEndPoint + " " + tcpClient.Client.RemoteEndPoint + encoder.GetString(message, 0, bytesRead);
                    textFromServer = tcpClient.Client.LocalEndPoint + " " + tcpClient.Client.RemoteEndPoint + encoder.GetString(message, 0, bytesRead);

                } while (clientStream.DataAvailable);
            }

            private void button1_Click(object sender, EventArgs e)
            {
                connectTCP();
            }

            private void button2_Click(object sender, EventArgs e)
            {
                //String textFromServer = (string)textFromServer;
                label1.Text = textFromServer;
            }
            private void Form1_Load(object sender, EventArgs e)
            {

            }
        }


}

And it doesnt work.
I am running, both server&client on my comp :,[

This article has been dead for over six months. Start a new discussion instead.