Place the controls (as shown in the form below) on the form and change the required properties.
For Combobox use Items property to enter currency symbols that we used in CurrencyServer.
Write the following code to connect to server in the constructor and code for click events of buttons.
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;
namespace CurrencyClient
{
public partial class Form1 : Form
{
TcpClient c = null;
NetworkStream ns;
public CurrencyClient()
{
InitializeComponent();
// get connection to server
c = new TcpClient("localhost", 2000);
ns = c.GetStream();
}
private void button1_Click(object sender, EventArgs e)
{
string symbol = ddlSymbols.SelectedItem.ToString(); // get selected symbol from combo box
byte[] buf = System.Text.Encoding.ASCII.GetBytes(symbol);
ns.Write(buf, 0, symbol.Length); // send symbol to server
byte[] rate = new byte[100];
ns.Read(rate, 0, 100); // read data from server
lblRate.Text = System.Text.Encoding.ASCII.GetString(rate); // place rate or error message into Label
}
private void button2_Click(object sender, EventArgs e)
{
// send "end" to server so that connection to client is closed
byte[] buf = System.Text.Encoding.ASCII.GetBytes("end");
ns.Write(buf, 0, 3);
this.Dispose(); // close form and application
}
}
}
Run the client application using F5 from Visual Studio. Select a symbol from drop down list and click on Get Rate button. Once done, click on Exit button to close client and connect to server.