Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net;
- using System.Net.Sockets;
- namespace Bus
- {
- class Program
- {
- static string path = @"C:\Users\Кирилл\source\repos\Bus\Bus\graph.txt";
- static float[,] g = new float[5, 5];
- static void read_graph()
- {
- using (StreamReader st = new StreamReader(path, System.Text.Encoding.Default))
- {
- string line;
- while ((line = st.ReadLine()) != null)
- {
- int i = 0;
- int u = get_int(line, ref i);
- int v = get_int(line, ref i);
- g[u, v] = get_int(line, ref i);
- }
- }
- }
- static void update(int[] way,int N,ref int pos,DateTime sdt)
- {
- float v = 0.2F;
- DateTime ndt = DateTime.Now;
- int t = Convert.ToInt32(ndt - sdt);
- float S = t * v;
- int i = 1;
- while(S > 0)
- {
- S -= g[way[1], way[0]];
- for (int j = 1; j < N; j++)
- way[j - 1] = way[j];
- }
- pos = way[0];
- }//Обновляет путь
- static int get_int(string str,ref int pos)
- {
- string res = "";
- while (str[pos] != ' ')
- res += str[pos++];
- pos++;
- return Convert.ToInt32(res);
- }
- static void Main(string[] args)
- {
- read_graph();
- int[] way = new int[5];
- while(true)
- {
- IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8049);
- Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- Random rnd = new Random();
- Console.WriteLine("Введите номер автобуса: ");
- int N = 0;
- int pos = rnd.Next(0, 4);
- string message = Console.ReadLine() + Convert.ToString(pos);
- try
- {
- sock.Connect(ip);
- sock.Send(Encoding.Unicode.GetBytes(message));
- int bytes = 0;
- byte[] buff = new byte[256];
- do
- {
- bytes = sock.Receive(buff);
- message = Encoding.Unicode.GetString(buff, 0, bytes);
- } while (sock.Available > 0);
- if (message[0] == 'g')
- {
- DateTime n = DateTime.Now;
- update(way, N, ref pos, n);
- Console.WriteLine("Местоположение обновленно");
- sock.Send(Encoding.Unicode.GetBytes(Convert.ToString(pos)));
- Console.WriteLine("Местоположение отправленно на сервер");
- }
- else
- {
- int i = 0;
- N = 0;
- while (i < message.Length)
- way[N++] = get_int(message, ref i);
- }
- }
- catch(Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement