Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace CurrencyComprasionClient
- {
- public partial class Form1 : Form
- {
- const int SERV1_PORT = 2048;
- const int SERV2_PORT = 2049;
- const int SERV3_PORT = 2050;
- const string IP = "localhost";
- string message = "";
- public Form1()
- {
- InitializeComponent();
- }
- private async void button1_Click(object sender, EventArgs e)
- {
- int threadID;
- var currencyList = new List<string>();
- threadID = (int)AppDomain.GetCurrentThreadId();
- Console.WriteLine("1. " + threadID);
- foreach(string currency in checkedListBox1.CheckedItems)
- {
- currencyList.Add(currency);
- }
- this.message = string.Join(" ", currencyList.ToArray());
- try
- {
- threadID = (int)AppDomain.GetCurrentThreadId();
- Console.WriteLine("2. " + threadID);
- var task = ClientTask(SERV1_PORT);
- threadID = (int)AppDomain.GetCurrentThreadId();
- Console.WriteLine("2.a " + threadID);
- var task2 = ClientTask(SERV2_PORT);
- threadID = (int)AppDomain.GetCurrentThreadId();
- Console.WriteLine("2.a " + threadID);
- var task3 = ClientTask(SERV3_PORT);
- threadID = (int)AppDomain.GetCurrentThreadId();
- Console.WriteLine("2.a " + threadID);
- await Task.WhenAll(task, task2, task3);
- threadID = (int)AppDomain.GetCurrentThreadId();
- Console.WriteLine("3. " + threadID);
- }
- catch (Exception exception)
- {
- MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- threadID = (int)AppDomain.GetCurrentThreadId();
- Console.WriteLine("4. " + threadID);
- }
- async Task ClientTask(int port)
- {
- int threadID = (int)AppDomain.GetCurrentThreadId();
- Console.WriteLine("5. " + threadID);
- TcpClient client = new TcpClient();
- client.Connect(IP, port);
- var msgBuffer = Encoding.ASCII.GetBytes(this.message);
- var serverAnswer = new byte[1024];
- threadID = (int)AppDomain.GetCurrentThreadId();
- Console.WriteLine("5a. " + threadID);
- await client.GetStream().WriteAsync(msgBuffer, 0, this.message.Length).ContinueWith(
- async (tsk) =>
- {
- threadID = (int)AppDomain.GetCurrentThreadId();
- Console.WriteLine("6. " + threadID);
- int lnt = await client.GetStream().ReadAsync(serverAnswer, 0, 1024);
- var serverMessage = Encoding.Default.GetString(serverAnswer).Substring(0, lnt);
- var package = serverMessage.Split(' ').ToList();
- threadID = (int)AppDomain.GetCurrentThreadId();
- Console.WriteLine("6a. " + threadID);
- SetTextLabels(package, port);
- threadID = (int)AppDomain.GetCurrentThreadId();
- Console.WriteLine("7. " + threadID);
- });
- threadID = (int)AppDomain.GetCurrentThreadId();
- Console.WriteLine("8. " + threadID);
- }
- private void SetTextLabels(List<string> package, int port)
- {
- int threadID = (int)AppDomain.GetCurrentThreadId();
- Console.WriteLine("9. " + threadID);
- if (package.Count == 0)
- {
- MessageBox.Show("Couldn't load rates correctly. Try again.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- Label euroLabel, usdLabel, chfLabel;
- switch(port)
- {
- case SERV1_PORT:
- euroLabel = firstEuro;
- usdLabel = firstUsd;
- chfLabel = firstChf;
- break;
- case SERV2_PORT:
- euroLabel = secondEuro;
- usdLabel = secondUsd;
- chfLabel = secondChf;
- break;
- default:
- euroLabel = thirdEuro;
- usdLabel = thirdUsd;
- chfLabel = thirdChf;
- break;
- }
- if (package.Contains("EUR"))
- {
- int index = package.IndexOf("EUR");
- euroLabel.Invoke(new Action(() => euroLabel.Text = package[index + 1]));
- }
- if (package.Contains("USD"))
- {
- int index = package.IndexOf("USD");
- usdLabel.Invoke(new Action(() => usdLabel.Text = package[index + 1]));
- }
- if (package.Contains("CHF"))
- {
- int index = package.IndexOf("CHF");
- chfLabel.Invoke(new Action(() => chfLabel.Text = package[index + 1]));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement