using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace FillingColumn
{
///
/// Логика взаимодействия для StartWorkPage.xaml
///
public partial class StartWorkPage : Page
{
public StartWorkPage()
{
InitializeComponent();
}
public async void ConnectToServer(string IDGasStation)
{
try
{
if (IDGasStation.Length == 1)
{
IDGasStation = IDGasStation.Insert(0, "0");
}
string port = "102" + IDGasStation;
IPEndPoint IpPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), Convert.ToInt32(port));
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(IpPoint);
//Отправка запроса на сервер АЗС
string request = "GetDataOfGasStation";
byte[] data = Encoding.UTF8.GetBytes(request);
socket.Send(data);
//Получение ответа от сервера АЗС
data = new byte[256]; // буфер для ответа
StringBuilder builder = new StringBuilder();
int bytes = 0; // количество полученных байт
do
{
bytes = socket.Receive(data, data.Length, 0);
builder.Append(Encoding.UTF8.GetString(data, 0, bytes));
}
while (socket.Available > 0);
Helper.CurrentGasStation = JsonConvert.DeserializeObject(builder.ToString());
// закрываем сокет
socket.Shutdown(SocketShutdown.Both);
socket.Close();
ManagerFrame.MainFrame.Navigate(new RefuelingPage());
}
catch (Exception)
{
MessageBox.Show("Нет ответа от АЗС", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void BtnConnectToStation_Click(object sender, RoutedEventArgs e)
{
ConnectToServer(TxtIDStation.Text);
}
}
}