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:
partial class AsyncronousTcpClient : Form { private byte[] data = new byte[1024]; private int size = 1024; private Socket client; private string stringData; public AsyncronousTcpClient() { InitializeComponent(); //delegate methods of buttons sendit.Click += new EventHandler(ButtonSendOnClick); connect.Click += new EventHandler(ButtonConnectOnClick); discon.Click += new EventHandler(ButtonDisconOnClick); } //connect private void ButtonConnectOnClick(object obj, EventArgs ea) { this.conStatus.Text = "Connecting..."; //connection-oriented Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050); newsock.BeginConnect(iep, new AsyncCallback(Connected), newsock); } private void ButtonSendOnClick(object obj, EventArgs ea) { byte[] message = Encoding.ASCII.GetBytes(this.newText.Text); this.newText.Clear(); client.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(SendData), client); } private void ButtonDisconOnClick(object obj, EventArgs args) { //disconnect client.Close(); this.conStatus.Text = "Disconnected"; } private void Connected(IAsyncResult iar) { //get the original socket object client = (Socket)iar.AsyncState;//cast iar into a socket object try { client.EndConnect(iar); //conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString(); Thread t = new Thread(new ThreadStart(RunConStatus)); t.Start(); client.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), client); } catch (SocketException) { //conStatus.Text = "Error Connecting"; Thread t = new Thread(new ThreadStart(RunConStatus)); t.Start(); } } //to prevent error on cross thread operation on //windows forms controls private void RunConStatus() { if (!InvokeRequired) { conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString(); } else { Invoke(new ThreadStart(RunConStatus)); } } private void RunConStatusError() { if (!InvokeRequired) { conStatus.Text = "Error Connecting"; } else { Invoke(new ThreadStart(RunConStatusError)); } } private void ReceiveData(IAsyncResult iar) { Socket remote = (Socket)iar.AsyncState; int recv = remote.EndReceive(iar); stringData = Encoding.ASCII.GetString(data, 0, recv); //add data to listbox //this.results.Items.Add(stringData); Thread t = new Thread(new ThreadStart(RunAddItems)); t.Start(); } private void RunAddItems() { if (!InvokeRequired) { this.results.Items.Add(stringData); } else { Invoke(new ThreadStart(RunAddItems)); } } private void SendData(IAsyncResult iar) { Socket remote = (Socket)iar.AsyncState; int sent = remote.EndSend(iar); remote.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), remote); } }//end class
server:
server Code:
public partial class AsynchroTcpServer : Form { private byte[] data = new byte[1024]; private Socket server; private int size = 1024; string messageConnection; private string receivedData; public AsynchroTcpServer() { InitializeComponent(); stopServer.Click += new EventHandler(ButtonStopOnClick); server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050); server.Bind(ipep); server.Listen(10); server.BeginAccept(new AsyncCallback(AcceptConn), server); } private void ButtonStopOnClick(object obj, EventArgs ea) { Close(); } private void AcceptConn(IAsyncResult iar) { Socket oldserver = (Socket)iar.AsyncState; Socket client = oldserver.EndAccept(iar); //conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString(); messageConnection = "Connected to: " + client.RemoteEndPoint.ToString(); Thread t = new Thread(new ThreadStart(RunConStatus)); t.Start(); string stringData = "Welcome to my server"; byte[] message1 = Encoding.ASCII.GetBytes(stringData); client.BeginSend(message1, 0, message1.Length, SocketFlags.None, new AsyncCallback(SendData), client); } //to prevent error on cross thread operation on //windows forms controls private void RunConStatus() { if (!InvokeRequired) { conStatus.Text = messageConnection; } else { Invoke(new ThreadStart(RunConStatus)); } } private void SendData(IAsyncResult iar) { Socket client = (Socket)iar.AsyncState; int sent = client.EndSend(iar); client.BeginSend(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), client); } private void ReceiveData(IAsyncResult iar) { Socket client = (Socket)iar.AsyncState; int recv; try { recv = client.EndReceive(iar); if (recv == 0)//if no data received { client.Close(); this.conStatus.Text = "Waiting for client..."; server.BeginAccept(new AsyncCallback(AcceptConn), server); return; } receivedData = Encoding.ASCII.GetString(data, 0, recv); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } //results.Items.Add(receivedData); //add items to listbox Thread t = new Thread(new ThreadStart(RunAddItems)); t.Start(); byte[] message2 = Encoding.ASCII.GetBytes(receivedData); client.BeginSend(message2, 0, message2.Length, SocketFlags.None, new AsyncCallback(SendData), client); } private void RunAddItems() { if (!InvokeRequired) { results.Items.Add(receivedData); } else { Invoke(new ThreadStart(RunAddItems)); } }
Greg





Reply With Quote