Hi,

I have the code for client and server chat application...The client can connect to the server application using a localhost address as soon as the user clicks the
connect button..When i type a message in the textbox (client app) and click send, theres no message displayed in the server list box and also in the client list box...I already have applied threads in the controls since the program examples were used using .NET 1.0...

Hope you get my point on the logic error..Here are the codes:

client:

client Code:
  1. partial class AsyncronousTcpClient : Form
  2.   {
  3.       private byte[] data = new byte[1024];
  4.       private int size = 1024;
  5.       private Socket client;
  6.       private string stringData;
  7.  
  8.         public AsyncronousTcpClient()
  9.         {
  10.             InitializeComponent();
  11.             //delegate methods of buttons
  12.             sendit.Click += new EventHandler(ButtonSendOnClick);
  13.             connect.Click += new EventHandler(ButtonConnectOnClick);
  14.             discon.Click += new EventHandler(ButtonDisconOnClick);
  15.         }
  16.  
  17.          //connect
  18.       private void ButtonConnectOnClick(object obj, EventArgs ea)
  19.       {
  20.           this.conStatus.Text = "Connecting...";
  21.           //connection-oriented
  22.           Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
  23.                              ProtocolType.Tcp);
  24.           IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
  25.           newsock.BeginConnect(iep, new AsyncCallback(Connected), newsock);
  26.       }
  27.      
  28.       private void ButtonSendOnClick(object obj, EventArgs ea)
  29.       {
  30.           byte[] message = Encoding.ASCII.GetBytes(this.newText.Text);
  31.           this.newText.Clear();
  32.           client.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(SendData), client);
  33.       }
  34.  
  35.       private void ButtonDisconOnClick(object obj, EventArgs args)
  36.       {
  37.           //disconnect
  38.           client.Close();
  39.           this.conStatus.Text = "Disconnected";
  40.       }
  41.  
  42.       private void Connected(IAsyncResult iar)
  43.       {
  44.           //get the original socket object
  45.           client = (Socket)iar.AsyncState;//cast iar into a socket object
  46.  
  47.           try
  48.           {
  49.               client.EndConnect(iar);
  50.               //conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString();
  51.               Thread t = new Thread(new ThreadStart(RunConStatus));
  52.               t.Start();
  53.  
  54.               client.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), client);
  55.           }
  56.           catch (SocketException)
  57.           {
  58.               //conStatus.Text = "Error Connecting";
  59.               Thread t = new Thread(new ThreadStart(RunConStatus));
  60.               t.Start();
  61.           }
  62.       }
  63.  
  64.  
  65.       //to prevent error on cross thread operation on
  66.       //windows forms controls
  67.       private void RunConStatus()
  68.       {
  69.           if (!InvokeRequired)
  70.           {
  71.               conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString();
  72.           }
  73.           else
  74.           {              
  75.                Invoke(new ThreadStart(RunConStatus));
  76.           }
  77.       }
  78.  
  79.       private void RunConStatusError()
  80.       {
  81.           if (!InvokeRequired)
  82.           {
  83.               conStatus.Text = "Error Connecting";
  84.           }
  85.           else
  86.           {
  87.               Invoke(new ThreadStart(RunConStatusError));
  88.           }
  89.       }
  90.  
  91.       private void ReceiveData(IAsyncResult iar)
  92.       {
  93.           Socket remote = (Socket)iar.AsyncState;
  94.           int recv = remote.EndReceive(iar);
  95.           stringData = Encoding.ASCII.GetString(data, 0, recv);
  96.           //add data to listbox
  97.           //this.results.Items.Add(stringData);
  98.           Thread t = new Thread(new ThreadStart(RunAddItems));
  99.           t.Start();
  100.       }
  101.  
  102.       private void RunAddItems()
  103.       {
  104.           if (!InvokeRequired)
  105.           {
  106.               this.results.Items.Add(stringData);      
  107.           }
  108.           else
  109.           {
  110.               Invoke(new ThreadStart(RunAddItems));
  111.           }
  112.       }
  113.  
  114.       private void SendData(IAsyncResult iar)
  115.       {
  116.           Socket remote = (Socket)iar.AsyncState;
  117.           int sent = remote.EndSend(iar);
  118.           remote.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), remote);
  119.       }
  120.        
  121.     }//end class


server:


server Code:
  1. public partial class AsynchroTcpServer : Form
  2.     {
  3.  
  4.         private byte[] data = new byte[1024];
  5.         private Socket server;
  6.         private int size = 1024;
  7.         string messageConnection;
  8.         private string receivedData;
  9.  
  10.         public AsynchroTcpServer()
  11.         {
  12.             InitializeComponent();
  13.             stopServer.Click += new EventHandler(ButtonStopOnClick);
  14.             server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  15.             IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
  16.             server.Bind(ipep);
  17.             server.Listen(10);
  18.             server.BeginAccept(new AsyncCallback(AcceptConn), server);
  19.            
  20.         }
  21.  
  22.         private void ButtonStopOnClick(object obj, EventArgs ea)
  23.         {
  24.             Close();
  25.         }
  26.  
  27.         private void AcceptConn(IAsyncResult iar)
  28.         {
  29.             Socket oldserver = (Socket)iar.AsyncState;
  30.             Socket client = oldserver.EndAccept(iar);
  31.             //conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString();
  32.             messageConnection = "Connected to: " + client.RemoteEndPoint.ToString();
  33.             Thread t = new Thread(new ThreadStart(RunConStatus));
  34.             t.Start();
  35.             string stringData = "Welcome to my server";
  36.             byte[] message1 = Encoding.ASCII.GetBytes(stringData);
  37.             client.BeginSend(message1, 0, message1.Length, SocketFlags.None, new AsyncCallback(SendData), client);
  38.         }
  39.  
  40.         //to prevent error on cross thread operation on
  41.         //windows forms controls
  42.      
  43.         private void RunConStatus()
  44.         {
  45.             if (!InvokeRequired)
  46.             {
  47.                 conStatus.Text = messageConnection;
  48.             }
  49.             else
  50.             {
  51.                 Invoke(new ThreadStart(RunConStatus));
  52.             }
  53.         }
  54.  
  55.  
  56.         private void SendData(IAsyncResult iar)
  57.         {
  58.             Socket client = (Socket)iar.AsyncState;
  59.             int sent = client.EndSend(iar);
  60.             client.BeginSend(data, 0, size, SocketFlags.None,
  61.                        new AsyncCallback(ReceiveData), client);
  62.         }
  63.  
  64.         private void ReceiveData(IAsyncResult iar)
  65.         {
  66.             Socket client = (Socket)iar.AsyncState;
  67.             int recv;
  68.  
  69.             try
  70.             {
  71.                recv = client.EndReceive(iar);
  72.                if (recv == 0)//if no data received
  73.                {
  74.                    client.Close();
  75.                    this.conStatus.Text = "Waiting for client...";
  76.                    server.BeginAccept(new AsyncCallback(AcceptConn), server);
  77.                    return;
  78.                }
  79.  
  80.                receivedData = Encoding.ASCII.GetString(data, 0, recv);
  81.             }
  82.             catch(Exception ex)
  83.             {
  84.                 MessageBox.Show(ex.ToString());
  85.             }
  86.            
  87.             //results.Items.Add(receivedData);  //add items to listbox
  88.             Thread t = new Thread(new ThreadStart(RunAddItems));
  89.             t.Start();
  90.             byte[] message2 = Encoding.ASCII.GetBytes(receivedData);
  91.             client.BeginSend(message2, 0, message2.Length, SocketFlags.None, new AsyncCallback(SendData), client);
  92.        }
  93.  
  94.         private void RunAddItems()
  95.         {
  96.             if (!InvokeRequired)
  97.             {
  98.                 results.Items.Add(receivedData);                
  99.             }
  100.             else
  101.             {
  102.                 Invoke(new ThreadStart(RunAddItems));
  103.             }
  104.         }


Greg