Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.IO;
- using System.Security.Cryptography;
- using System.Numerics;
- namespace Zajecia2serwer
- {
- class Program
- {
- public static BigInteger[] funk = new BigInteger[12];
- public struct Vector2
- {
- public BigInteger x;
- public BigInteger y;
- public Vector2(BigInteger x, BigInteger y)
- {
- this.x = x;
- this.y = y;
- }
- }
- private static byte[] buffer = new byte[1024];
- private static List<Socket> clients = new List<Socket>();
- private static Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- static void Main(string[] args)
- {
- SetupServer();
- Console.ReadLine();
- }
- private static void SetupServer()
- {
- server.Bind(new IPEndPoint(IPAddress.Any, 8080));
- server.Listen(1);
- server.BeginAccept(AcceptCallback, null);
- }
- private static void AcceptCallback(IAsyncResult ar)
- {
- Socket socket = server.EndAccept(ar);
- clients.Add(socket);
- Console.WriteLine("Client {0} connected", clients.FindIndex(s => s == socket));
- socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, socket);
- server.BeginAccept(AcceptCallback, null);
- }
- private static void ReceiveCallback(IAsyncResult ar)
- {
- Socket socket = (Socket)ar.AsyncState;
- int received;
- int w = 1;
- while (w < 11)
- {
- try
- {
- received = socket.EndReceive(ar);
- }
- catch (SocketException)
- {
- Console.WriteLine("Client {0} disconnected", clients.FindIndex(s => s == socket));
- socket.Close();
- clients.Remove(socket);
- return;
- }
- BigInteger[] funk = new BigInteger[12];
- byte[] data = new byte[received];
- funk[w] = new BigInteger(data);
- }
- BigInteger p = funk[1];
- Vector2[] contro = new Vector2[5];
- contro[0] = new Vector2(1, funk[1]);
- contro[1] = new Vector2(2, funk[2]);
- contro[2] = new Vector2(6, funk[6]);
- contro[3] = new Vector2(8, funk[8]);
- contro[4] = new Vector2(9, funk[9]);
- BigInteger v = GetValue(contro, 0);
- v = BigInteger.Abs(v);
- v = BigInteger.ModPow(v, 1, p);
- Console.WriteLine(v);
- System.Threading.Thread.Sleep(100000);
- }
- public static void SendCallback(IAsyncResult ar)
- {
- Socket socket = (Socket)ar.AsyncState;
- socket.EndSend(ar);
- }
- private static BigInteger Inner(Vector2[] controlPoints, BigInteger x, int j)
- {
- x = 0;
- BigInteger dividend = 1;
- BigInteger divisor = 1;
- for (var k = 0; k < controlPoints.Length; k++)
- {
- if (k == j)
- {
- continue;
- }
- dividend *= (x - controlPoints[k].x);
- divisor *= (controlPoints[j].x - controlPoints[k].x);
- }
- return (dividend / divisor) * controlPoints[j].y;
- }
- public static BigInteger GetValue(Vector2[] controlPoints, BigInteger x)
- {
- BigInteger result = 0;
- for (var j = 0; j < controlPoints.Length; j++)
- {
- result += Inner(controlPoints, x, j);
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement